javascript vue项目开发中的常用公共方法

本篇文章纯属干货,包含了笔者在实际项目开发中经常用到的一些公共通用方法,希望可以帮助大家。

部分方法适用ReactNative

import {
    PixelRatio,
    Dimensions,
    Platform,
    findNodeHandle,
    UIManager
} from 'react-native';

/**
 * 日期格式化函数
 * @param {} date 
 */

function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}

export function formatDate(date, type) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()

    if (type = 'YYYY-MM-DD') {
        return [year, month, day].map(formatNumber).join('-')
    }
    else {
        return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
    }
}



//获取设备屏幕的实际宽度

export const deviceW = Dimensions.get('window').width;
export const deviceH = Dimensions.get('window').height;


// 设置自适应字体,以iphone6为例
const basePx = 375

export function px2dp(px) {
    return px * deviceW / basePx
}

/**
 * 判断是否为iphoneX
 * @returns {boolean}
 */

const X_WIDTH = 375;
const X_HEIGHT = 812;

export function isIphoneX() {
    return (
        Platform.OS === 'ios' &&
        ((deviceH === X_HEIGHT && deviceW === X_WIDTH) ||
            (deviceH === X_WIDTH && deviceW === X_HEIGHT))
    )
}


/**
 * 根据是否是iPhoneX返回不同的样式
 * @param iphoneXStyle
 * @param iosStyle
 * @param androidStyle
 * @returns {*}
 */

export function ifIphoneX(iphoneXStyle, iosStyle, androidStyle) {
    if (isIphoneX()) {
        return iphoneXStyle;
    } else if (Platform.OS === 'ios') {
        return iosStyle
    } else {
        if (androidStyle) return androidStyle;
        return iosStyle
    }
}


//判断设备是否是ios平台
export const isIphone = Platform.OS == 'ios' ? true : false

/** 
 * 返回精确的n位小数数值
 * @param num:number
 * @param dig:number
*/

export function digitToFixed(num, digit = 2) {
    // NaN,undefined,空值
    if (typeof (num) == 'undefined') {
        return '0.00'
    }
    else {
        return parseFloat(num).toFixed(digit)
    }
}

/** 
 * 返回账户金额,每隔三位数字添加一个逗号
 * @param str:string

*/

export function threeNumberAPointer(str) {
    return parseFloat(str).toFixed(2).replace(/\d(?=(?:\d{3})+\b)/g, `$&,`)
}

/**
 * 将银行卡末尾n位数字截取出来
 *  @param cardCode:string 银行卡号
 *  @param digit:num  截取长度
*/

export function bankCardLastNum(cardCode, digit = 4) {
    return cardCode.substring(cardCode.length - digit, cardCode.length)
}

/**
*获取react-native-element元素的宽度和高度等信息
* @param ref:ref 组件ref
*/

export function getRnElementInfo(ref) {
    const handle = findNodeHandle(ref);
    return new Promise((resolve) => {
        UIManager.measure(handle, (x, y, width, height, pageX, pageY) => {
            resolve({
                x,
                y,
                width,
                height,
                pageX,
                pageY
            });
        });
    });
}

/** 
 * 返回银行卡号,每隔四个数字添加一个空格
 * @param str:string

*/
export function formatBankCard(str) {
    let newStr = str.replace(/\d(?=(?:\d{4})+\b)/g, `$& `);
    // let subStr = String(newStr).subStr(0,len-4)
    return String(newStr)
}

//判断参数的类型

const Types = {

  isPrototype( data ) {
    return Object.prototype.toString.call(data).toLowerCase()
  },

  isArray( data ) {
    return this.isPrototype( data ) === '[object array]'
  },

  isJSON( data ) {
    return this.isPrototype( data ) === '[object object]'
  },

  isFunction( data ) {
    return this.isPrototype( data ) === '[object function]'
  },

  isString( data ) {
    return this.isPrototype( data ) === '[object string]'
  },

  isNumber( data ) {
    return this.isPrototype( data ) === '[object number]'
  },

  isUndefined( data ) {
    return this.isPrototype( data ) === '[object undefined]'
  },

  isNull( data ) {
    return this.isPrototype( data ) === '[object null]'
  }

}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值