Skip to content

Commit

Permalink
perf(type): 优化 isNull,isUndefined,isNil
Browse files Browse the repository at this point in the history
  • Loading branch information
vfiee committed Apr 2, 2022
1 parent c094e1f commit a58d3db
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions packages/type/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: vyron
* @Date: 2022-01-10 17:38:09
* @LastEditTime: 2022-03-02 17:15:31
* @LastEditTime: 2022-04-01 14:44:04
* @LastEditors: vyron
* @Description: 判断数据类型
* @FilePath: /v-utils/packages/type/src/index.ts
Expand Down Expand Up @@ -32,12 +32,11 @@ export const isSymbol = (value: unknown): value is symbol =>
typeof value === 'symbol'

// 是否为Null
export const isNull = (value: unknown): value is null =>
toRawType(value) === 'Null'
export const isNull = (value: unknown): value is null => value === null

// 是否为Null
export const isUndefined = (value: unknown): value is null =>
toRawType(value) === 'Undefined'
value === undefined

// 是否为 Array
export const isArray = Array.isArray
Expand All @@ -59,7 +58,8 @@ export const isDate = (value: unknown): value is Date =>
toRawType(value) === 'Date'

// 是否为 Arguments
export const isArguments = (value: unknown): boolean => toRawType(value) === "Arguments"
export const isArguments = (value: unknown): boolean =>
toRawType(value) === 'Arguments'

// 是否为 Map
export const isMap = (value: unknown): value is Map<any, any> =>
Expand All @@ -81,44 +81,48 @@ export const isWeakSet = (value: unknown): value is WeakSet<object> =>
export const isNaN = (value: unknown): boolean => value !== value

// 是否为null或undefined
export const isNil = (value: unknown): boolean => {
const rawType = toRawType(value)
return rawType === 'Null' || rawType === 'Undefined'
}
export const isNil = (value: unknown): boolean =>
value === null || value === undefined

// 判断是否为有效的数组长度
const MAX_SAFE_INTEGER = 9007199254740991
export const isLength = (value: unknown): boolean => isNumber(value) && value >= 0 && value <= MAX_SAFE_INTEGER
export const isLength = (value: unknown): boolean =>
isNumber(value) && value >= 0 && value <= MAX_SAFE_INTEGER

// 判断是否为类数组
export const isArrayLike = (value: unknown): boolean => value != null && !isFunction(value) && isLength((value as Array<any>).length)
export const isArrayLike = (value: unknown): boolean =>
value != null && !isFunction(value) && isLength((value as Array<any>).length)

// 是否为原型
export const isPrototype = (value: unknown): boolean => {
const Constructor = value && (value as any).constructor
const proto = (typeof Constructor === 'function' && Constructor.prototype) || Object.prototype
const proto =
(typeof Constructor === 'function' && Constructor.prototype) ||
Object.prototype
return value === proto
}

/**
* 判断 value 是否为空对象,数组,map 或 set
* @param value
* @param value
*/
export const isEmpty = (value: unknown): boolean => {
if (value == null) return true
const rawType = toRawType(value)
if (rawType === "Set" || rawType === "Map") {
return !(value as (Set<any> | Map<any, any>)).size
} else if (isArrayLike(value) && (isArray(value) || isArguments(value) || typeof value === "string")) {
return !(value as (string | Array<unknown>)).length
if (rawType === 'Set' || rawType === 'Map') {
return !(value as Set<any> | Map<any, any>).size
} else if (
isArrayLike(value) &&
(isArray(value) || isArguments(value) || typeof value === 'string')
) {
return !(value as string | Array<unknown>).length
} else if (isPrototype(value)) {
return !Object.keys(value as object).length
}
for (const key in (value as object)) {
for (const key in value as object) {
if (!Object.prototype.hasOwnProperty.call(value, key)) {
return false
}
}
return true
}

0 comments on commit a58d3db

Please sign in to comment.