-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getType, isArray, isBoolean, isDate, isNull, isNumber, isOb…
…ject, isRegExp, isString, isUndefined
- Loading branch information
Showing
12 changed files
with
243 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* 检测 value 值的类型。 | ||
* | ||
* @export | ||
* @param value 要检测的值 | ||
* @returns 检测值的类型 | ||
*/ | ||
export default function getType(value: any): string { | ||
return Object.prototype.toString.call(value).split(' ')[1].replace(']', '') | ||
} |
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 @@ | ||
/** | ||
* 检查 value 是否是一个数组。 | ||
* | ||
* @param value 要检查的值 | ||
* @returns 是(true)或否(false) | ||
*/ | ||
export default function isArray(value: any): value is any[] { | ||
return Array.isArray(value) | ||
} |
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 @@ | ||
/** | ||
* 检查 value 是否是一个布尔值。 | ||
* | ||
* @param value 要检查的值 | ||
* @returns 是(true)或否(false) | ||
*/ | ||
export default function isBoolean(value: any): value is boolean { | ||
return typeof value === 'boolean' | ||
} |
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 @@ | ||
import getType from './getType' | ||
|
||
/** | ||
* 检查 value 是否是一个日期。 | ||
* | ||
* @param value 要检查的值 | ||
* @returns 是(true)或否(false) | ||
*/ | ||
export default function isDate(value: any): value is Date { | ||
return getType(value) === 'Date' | ||
} |
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 @@ | ||
/** | ||
* 检查 value 是否等于 null。 | ||
* | ||
* @param value 要检查的值 | ||
* @returns 是(true)或否(false) | ||
*/ | ||
export default function isNull(value: any): value is null { | ||
return value === 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,9 @@ | ||
/** | ||
* 检查 value 是否是一个数值。 | ||
* | ||
* @param value 要检查的值 | ||
* @returns 是(true)或否(false) | ||
*/ | ||
export default function isNumber(value: any): value is number { | ||
return typeof value === 'number' | ||
} |
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 @@ | ||
/** | ||
* 检查 value 是否是一个对象。 | ||
* | ||
* @param value 要检查的值 | ||
* @returns 是(true)或否(false) | ||
*/ | ||
export default function isObject(value: any): value is object { | ||
const type = typeof value | ||
return value != null && (type === 'object' || type === '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,11 @@ | ||
import getType from './getType' | ||
|
||
/** | ||
* 检查 value 是否是一个正则表达式。 | ||
* | ||
* @param value 要检查的值 | ||
* @returns 是(true)或否(false) | ||
*/ | ||
export default function isRegExp(value: any): value is RegExp { | ||
return getType(value) === 'RegExp' | ||
} |
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 @@ | ||
/** | ||
* 检查 value 是否是一个字符串。 | ||
* | ||
* @param value 要检查的值 | ||
* @returns 是(true)或否(false) | ||
*/ | ||
export default function isString(value: any): value is string { | ||
return typeof value === 'string' | ||
} |
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 @@ | ||
/** | ||
* 检查 value 是否等于 undefined。 | ||
* | ||
* @param value 要检查的值 | ||
* @returns 是(true)或否(false) | ||
*/ | ||
export default function isUndefined(value: any): value is undefined { | ||
return value === void 0 | ||
} |
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