mini-js-utils
v1.0.7
Published
一个简洁的Javascript工具库
Downloads
5
Readme
Functions
now() ⇒ number
返回当前时间戳
Kind: global function
Returns: number - 时间戳
Example
const timestamp = now(); // 1602490153920
dateFormat(timeStamp, fmt) ⇒ string | number
时间格式化
Kind: global function
Returns: string | number - 格式化后的字符串
| Param | Type | Description | | --- | --- | --- | | timeStamp | number | 时间戳 | | fmt | string | 格式化字符串类型 |
Example
const currentDate = dateFormat(now())
const currentDate = dateFormat(now(), 'yyyy-MM-dd')
getTimeStamp(day) ⇒ number
获取N天前的时间戳, 默认0天,即当前时间戳
Kind: global function
Returns: number - N天前的时间戳
| Param | Type | Description | | --- | --- | --- | | day | number | N天,不传默认取当前时间戳 |
Example
const timestamp = getTimeStamp(); // 获取当前时间戳
const timestamp = getTimeStamp(10); // 获取十天前的时间戳
isSecondsTimeStamp(timeStamp) ⇒ boolean
是否为秒级时间戳
Kind: global function
Returns: boolean - true:秒级,false:非秒级
| Param | Type | | --- | --- | | timeStamp | number |
Example
const timestampType = isSecondsTimeStamp(now()); // false
isMillisecondsTimeStamp(timeStamp) ⇒ boolean
是否为毫秒级时间戳
Kind: global function
Returns: boolean - true:毫秒级,false:非毫秒级
| Param | Type | | --- | --- | | timeStamp | number |
Example
const timestampType = isMillisecondsTimeStamp(now()); // true
isTimeStamp(timestamp) ⇒ boolean
判断是否为时间戳
Kind: global function
Returns: boolean - true:是时间戳,false:不是时间戳
| Param | Type | Description | | --- | --- | --- | | timestamp | number | 时间戳 |
Example
const isTimeStamp = isTimeStamp(now()); // true
invariant(condition, format)
校验并输出错误
Kind: global function
| Param | Type | Description | | --- | --- | --- | | condition | boolean | 是否输出,当条件为false时输出 | | format | string | 待输出文案 |
Example
const a = 1;
invariant(typeof a === 'object', 'a不是object'); // 'a不是object'
isEffectiveArray(arr) ⇒ boolean
判断是否为一个数值的数组
Kind: global function
Returns: boolean - true:数组有值,false:空数组,数组没有有效值
| Param | Type | Description | | --- | --- | --- | | arr | array | 数组 |
Example
isEffectiveArray([]); // false
isEffectiveArray([1, 3]); // true
isArrayEqual(arr, other) ⇒ boolean
判断两个数组是否相等
Kind: global function
Returns: boolean - true:两个数组相等,false:两个数组不相等
| Param | Type | Description | | --- | --- | --- | | arr | array | 基准数组 | | other | array | 待对比数组 |
Example
isArrayEqual([1, 2], [1, 2]); // true
isArrayEqual([1, 2], [2, 1]); // false
isArrayEqual([1, 2], ['1', 2]); // false
isEqual(value, other) ⇒ boolean
判断两个变量是否相等
Kind: global function
Returns: boolean - true:相等,false:不相等
| Param | Type | Description | | --- | --- | --- | | value | any | 基准变量 | | other | any | 待比较变量 |
Example
isEqual(1, 1); // true
isEqual(1, '1'); // true
isEqual('1', 1); // true
isEqual([1, 2], [1, 2]); // true
isEqual([2, 1], [1, 2]); // false
isEqual({ age: 12 }, { age: 12 }); // true
isEqual({ age: 12 }, { age: '12' }); // false
isEqual(null, null); // true
isEqual(null, undefined); // true
isEqual(undefined, undefined); // true
getLength(value) ⇒ number
判断数据的长度
Kind: global function
Returns: number - 传入数据的长度,如果是对象,则为其包含key-value键值对的数量
| Param | Type | | --- | --- | | value | number | string | object | array |
Example
getLength(12345); // 5
getLength('1234'); // 4
getLength([1, 2, 3]); // 3
getLength({ name: 'andy', age: 12 }); // 2
isEffectiveNumber(value) ⇒ boolean
是否为一个有效数字
Kind: global function
Returns: boolean - true:有效数字,false:无效数字
| Param | Type | Description | | --- | --- | --- | | value | number | string | 参数 |
Example
isEffectiveNumber(123); // true
isEffectiveNumber('123'); // true
isEffectiveNumber('123a'); // false
isEffectiveNumber(abc); // false
isNumberEqual(num, other) ⇒ boolean
判断两个数字是否相等
Kind: global function
Returns: boolean - true:相等,false;不相等
| Param | Type | Description | | --- | --- | --- | | num | number | 基准数字 | | other | number | string | 待对比数字 |
Example
isNumberEqual(12, 12); // true
isNumberEqual(12, '12'); // true
isNumberEqual(12, '12a'); // false
isEffectiveObject(obj) ⇒ boolean
判断是否为一个有实际值的对象
Kind: global function
Returns: boolean - true:一个有值的对象,false:一个空对象
| Param | Type | Description | | --- | --- | --- | | obj | object | 待判断对象 |
Example
isEffectiveObject({}); // false
isEffectiveObject({ age: 12 }); true
isEffectiveValue(value) ⇒ boolean
判断是否为一个有效值
Kind: global function
Returns: boolean - true:一个有效值,false:一个无效值
| Param | Type | Description | | --- | --- | --- | | value | string | number | object | boolean | array | 待判断变量 |
Example
isEffectiveValue(12); // true
isEffectiveValue('12'); // true
isEffectiveValue(''); // false
isEffectiveValue([]); // false
isEffectiveValue({}); // false
deleteKeys(obj, keys)
删除对象属性
Kind: global function
| Param | Type | Description | | --- | --- | --- | | obj | object | 对象 | | keys | string | array | 待删除属性 |
Example
const person = { name: 'andy' };
deleteKeys(person, 'name'); // {}
deleteInvalidateKeys(obj)
删除无用的key,多用于过滤请求条件
Kind: global function
| Param | Type | Description | | --- | --- | --- | | obj | object | 待过滤object |
Example
const person = {
name: 'andy'
money: null,
};
deleteInvalidateKeys(person); // { name: 'andy' }
isObjectEqual(obj, other)
判断两个object对象是否相等
Kind: global function
| Param | Type | Description | | --- | --- | --- | | obj | object | 基准对象 | | other | object | 待对比对象 |
Example
isObjectEqual({ age: 12 }, { age: 12 }); // true
isObjectEqual({ age: 12 }, { age: '12' }); // true
isStringEqual(str, other) ⇒ boolean
判断两个字符串是否相等
Kind: global function
Returns: boolean - true:相等,false:不相等
| Param | Type | Description | | --- | --- | --- | | str | string | 基准字符串 | | other | string | number | 其他类型值 |
Example
isStringEqual(12, '12'); // true
isStringEqual('12', '12'); // true
getTypeString(value) ⇒ string
获取数据类型
Kind: global function
Returns: string - 参数类型
| Param | Type | Description | | --- | --- | --- | | value | any | 参数 |
Example
getTypeString('123'); // string
getTypeString(123); // number
getTypeString(true); // boolean
getTypeString(undefined); // undefined
getTypeString(null); // null
getTypeString(function() {}); // function
getTypeString(Symbol()); // symbol
getTypeString([1, 2, 3]); // array
getTypeString({ age: 12 }); // object
getTypeString(new Date()); // date
getTypeString(new Error('nothing')); // error
getTypeString(new RegExp(/\w/)); // regexp
getTypeString(document); // document
getTypeString(global); // global node环境下
getTypeString(window); // window 浏览器环境下
isNumber(value) ⇒ boolean
是否为数字
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isNumber(123); // true
isNumber('123'); // false
isString(value) ⇒ boolean
是否为字符串
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isString(123); // false
isString('123'); // true
isArray(value) ⇒ boolean
是否为数组
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isArray([]); // true
isArray([1, 2]); // true
isArray({ age: 12 }); // false
isObject(value) ⇒ boolean
是否为对象
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isObject({}); // true
isObject({ age: 12 }); // true
isObject([1, 2, 3]); // false
isFunction(value) ⇒ boolean
是否为函数
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isFunction(function(){}); // true
isFunction({ age: 12 }); // false
isBoolean(value) ⇒ boolean
是否为布尔值
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isBoolean(false); // true
isBoolean('false'); // false
isUndefined(value) ⇒ boolean
是否为undefined
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isUndefined(undefined); // true
isUndefined(null); // false
isNull(value) ⇒ boolean
是否为null
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isNull(null); // true
isNull(undefined); false
isSymbol(value) ⇒ boolean
是否为Symbol
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isSymbol(12); // false
isSymbol(Symbol(12)); // true
isDate(value) ⇒ boolean
是否为日期
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isDate(new Date()); // true
isDate(123); false
isError(value) ⇒ boolean
是否为错误
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isError(new Error('nothing')); // true
isRegexp(value) ⇒ boolean
是否为正则
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isRegexp(new RegExp(/\w/)); // true
isDocument(value) ⇒ boolean
是否为document对象
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isDocument(document); // true
isGlobal(value) ⇒ boolean
是否为global对象
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isGlobal(global); // true
isWindow(value) ⇒ boolean
是否为Window对象
Kind: global function
Returns: boolean - true:是,false,否
| Param | Description | | --- | --- | | value | 参数 |
Example
isWindow(window); // true