-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 增加isArray isEmpty isFunction isNuil isNull isObject 方法
- Loading branch information
Showing
7 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import isNuil from './isNuil' | ||
/** | ||
* 判断传入的对象是否为数组。 | ||
* @param obj 任意类型的对象,需要判断是否为数组。 | ||
* @return 返回一个布尔值,表示传入的对象是否为数组。如果是数组,则返回true;否则返回false。 | ||
*/ | ||
export default function isArray(obj: unknown) { | ||
return isNuil(obj) ? false : Array.isArray(obj) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { isNuil, isArray, isObject } from './index' | ||
export default function isEmpty(value: any): boolean { | ||
if (isNuil(value)) { | ||
return true; | ||
} | ||
if (isArray(value) || isObject(value)) { | ||
return !Object.keys(value).length; | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import isNuil from './isNuil' | ||
/** | ||
* 检查一个对象是否为函数。 | ||
* @param obj 未知类型的对象,需要被检查是否为函数。 | ||
* @returns 返回一个布尔值,如果对象是函数则为true,否则为false。 | ||
*/ | ||
export default function isFunction(obj: unknown) { | ||
return isNuil(obj) ? false : typeof obj === 'function'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function isNuil(obj: unknown) { | ||
return obj === null || obj === undefined | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* 检查一个值是否为 null | ||
* 该函数接受一个未知类型的参数,并返回一个布尔值, | ||
* 表示该参数是否为 null | ||
* @param obj 一个未知类型的值,可以是任何类型。 | ||
* @returns 布尔值。如果参数为 null,则返回 true; | ||
* 否则,返回 false。 | ||
*/ | ||
export default function isNull(obj: unknown): boolean { | ||
return obj === null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* 检查一个值是否为对象。 | ||
* @param obj 未知类型的值,待检查是否为对象。 | ||
* @returns 返回一个布尔值,表示该值是否为对象。如果是对象则返回true,否则返回false。 | ||
*/ | ||
export default function isObject(obj: unknown): boolean { | ||
return typeof obj === 'object' | ||
} |