本篇文章纯属干货,包含了笔者在实际项目开发中经常用到的一些公共通用方法,希望可以帮助大家。
部分方法适用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]'
}
}