more 更多方法 🔥🔥🔥
描述
更多判断类型方法
# 示例
# 1 isDate(val) 判断是否是 日期 对象
import { isDate } from 'sf-utils2'
isDate('2017-12-20') // false
isDate(1514096716800) // false
isDate(new Date('abc')) // true
isDate(new Date()) // true
1
2
3
4
5
6
2
3
4
5
6
# 2 isValidDate(val) 判断是否是 有效日期 对象
import { isValidDate } from 'sf-utils2'
isValidDate('2017-12-20') // false
isValidDate(1514096716800) // false
isValidDate(new Date('abc')) // false
isValidDate(new Date()) // true
1
2
3
4
5
6
2
3
4
5
6
# 3 isError(val) 判断是否 Error 对象
import { isError } from 'sf-utils2'
isError(null) // false
isError({}) // false
isError(new TypeError('error')) // true
isError(new Error('error')) // true
1
2
3
4
5
6
2
3
4
5
6
# 4 isSymbol(val) 判断是否 Symbol 对象
import { isSymbol } from 'sf-utils2'
isSymbol('a') // false
isSymbol(Symbol('a')) // true
1
2
3
4
2
3
4
# 5 isArguments(val) 判断是否 Arguments 对象
import { isSymbol } from 'sf-utils2'
isArguments([]) // false
isArguments(arguments) // true
1
2
3
4
2
3
4
# 6 isFormData(val) 判断是否 FormData 对象
import { isSymbol } from 'sf-utils2'
isFormData({}) // false
isFormData(new FormData()) // true
1
2
3
4
2
3
4
# 7 isMap(val) 判断是否 Map 对象
import { isMap } from 'sf-utils2'
isMap({}) // false
isMap(new Map()) // true
1
2
3
4
2
3
4
# 8 isWeakMap(val) 判断是否 Map 对象
import { isWeakMap } from 'sf-utils2'
isWeakMap({}) // false
isWeakMap(new WeakMap()) // true
1
2
3
4
2
3
4
# 9 isSet(val) 判断是否 Set 对象
import { isSet } from 'sf-utils2'
isSet({}) // false
isSet(new Set()) // true
1
2
3
4
2
3
4
# 10 isWeakSet(val) 判断是否 WeakSet 对象
import { isWeakSet } from 'sf-utils2'
isWeakSet({}) // false
isWeakSet(new WeakSet()) // true
1
2
3
4
2
3
4
# 11 formatSizeUnit(arg) 格式化尺寸
import { formatSizeUnit } from 'sf-utils2'
formatSizeUnit('100vh') // 100
formatSizeUnit('90vw') // 90
formatSizeUnit('100px') // 100
formatSizeUnit('80.1rem') // 80.1
1
2
3
4
5
6
2
3
4
5
6
# 12 isNative(Ctor) 检测当前方法是否是原生 js 提供的
import { isNative } from 'sf-utils2'
isNative(Array.prototype.slice) => true
isNative(() => {}) => false
1
2
3
4
5
2
3
4
5
# 13 stringifyURL(url, query, isDeepClose) 加工 url,将 query 参数合并到 url 上
stringifyURL('http://www.baidu.com?id=123', { id: '22', name: 'haha' })
// 输出 http://www.baidu.com?id=22&haha
1
2
2
# 14 stringifyCircularJSON(obj) 将包含循环引用的 JSON 对象序列化为 JSON 格式。
let a = { name: 'a' }
let b = { name: 'b' }
a.b = b
b.a = a
console.log(a)
// 输出 {"name":"a","b":{"name":"b"}}
const obj = { n: 42 }
obj.obj = obj
stringifyCircularJSON(obj) // '{"n": 42}'
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 15 isValidJSON(str) 判断是否正确的 json 数据
isValidJSON('{"name":"Adam","age":20}') // true
isValidJSON('{"name":"Adam",age:"20"}') // false
isValidJSON(null) // true
1
2
3
2
3
# 16 isArrayLike(obj) 判断是否是伪数组
isArrayLike([1, 2, 3]); // true
isArrayLike(document.querySelectorAll('.className')); // true
isArrayLike('abc'); // true
isArrayLike(null); // false
1
2
3
4
2
3
4
# 17 getSize(obj) 返回不同类型的的长度(对象/数组/Map/Set/String) v3.3+
getSize([1, 2, 3]); // 3
getSize({id: 1, name: '小明'}); // 2
getSize(new Map([['1', 2], [2, '3']])); // 2
getSize('abc'); // 3
getSize(new Set([1, 2, 3, 4, 4])); // 4
1
2
3
4
5
2
3
4
5
# 18 unserialize(obj) 反转序列化查询参数 v3.3+
unserialize('id=123&name=test1') => {id: '123', name: 'test1'}
1
# 19 serialize(obj) 序列化查询参数 v3.3+
serialize({id: 123, name: 'test1'} => id=123&name=test1
1
上次更新: 2024/01/08, 21:47:25