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