-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 新增函数的jsdoc注释 * fix: 修复isPromise可能带来的异常风险 * perf: 优化isPromise的实现方式
- Loading branch information
Showing
27 changed files
with
132 additions
and
21 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
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是array类型 | ||
* @param data 某个数据 | ||
* @returns 是否是array类型 | ||
*/ | ||
export function isArray<T>(data: unknown): data is T[] { | ||
return isType(data, 'Array') | ||
} |
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是bigint类型 | ||
* @param data 某个数据 | ||
* @returns 是否是bigint类型 | ||
*/ | ||
export function isBigInt(data: unknown): data is bigint { | ||
return isType(data, 'BigInt') | ||
} |
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是boolean类型 | ||
* @param data 某个数据 | ||
* @returns 是否是boolean类型 | ||
*/ | ||
export function isBoolean(data: unknown): data is boolean { | ||
return isType(data, '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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是Date类型 | ||
* @param data 某个数据 | ||
* @returns 是否是Date类型 | ||
*/ | ||
export function isDate(data: unknown): data is Date { | ||
return isType(data, '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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/** | ||
* 某个数据是否为false | ||
* @param data 某个数据 | ||
* @returns 是否为false | ||
*/ | ||
export function isFalse(data: unknown): data is false { | ||
return data === 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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
export function isFunction<T extends Function>(data: unknown): data is T { | ||
/** | ||
* 某个数据/方法是否是function类型 | ||
* @param data 某个数据/方法 | ||
* @returns 是否是function类型 | ||
*/ | ||
export function isFunction<T>(data: unknown): data is T extends Function ? T : Function { | ||
return isType(data, '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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是Map类型 | ||
* @param data 某个数据 | ||
* @returns 是否是Map类型 | ||
*/ | ||
export function isMap<K, V>(data: unknown): data is Map<K, V> { | ||
return isType(data, 'Map') | ||
} |
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/** | ||
* 某个数据是否是NaN | ||
* @param data 某个数据 | ||
* @returns 是否是NaN | ||
*/ | ||
export function isNaN(data: unknown): data is number { | ||
return Number.isNaN(data) | ||
} |
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是null类型 | ||
* @param data 某个数据 | ||
* @returns 是否是null类型 | ||
*/ | ||
export function isNull(data: unknown): data is null { | ||
return isType(data, '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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是number类型 | ||
* @param data 某个数据 | ||
* @returns 是否是number类型 | ||
*/ | ||
export function isNumber(data: unknown): data is number { | ||
return isType(data, '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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是object类型 | ||
* @param data 某个数据 | ||
* @returns 是否是object类型 | ||
*/ | ||
export function isObject<T>(data: unknown): data is T { | ||
return isType(data, 'Object') | ||
} |
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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import { isFunction } from './isFunction' | ||
import { isObject } from './isObject' | ||
|
||
export function isPromise<T = any>(data: unknown): data is Promise<T> { | ||
/** | ||
* 某个数据/方法是否是Promise | ||
* @param data 某个数据/方法 | ||
* @returns 是否是Promise | ||
*/ | ||
export function isPromise<T>(data: unknown): data is Promise<T> { | ||
return ( | ||
!!data | ||
&& isFunction((data as any).then) | ||
&& isFunction((data as any).catch) | ||
isObject<{ then: Function, catch: Function }>(data) | ||
&& isFunction(data.then) | ||
&& isFunction(data.catch) | ||
) | ||
} |
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是RegExp类型 | ||
* @param data 某个数据 | ||
* @returns 是否是RegExp类型 | ||
*/ | ||
export function isRegExp(data: unknown): data is RegExp { | ||
return isType(data, '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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是Set类型 | ||
* @param data 某个数据 | ||
* @returns 是否是Set类型 | ||
*/ | ||
export function isSet<T>(data: unknown): data is Set<T> { | ||
return isType(data, 'Set') | ||
} |
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是string类型 | ||
* @param data 某个数据 | ||
* @returns 是否是string类型 | ||
*/ | ||
export function isString(data: unknown): data is string { | ||
return isType(data, '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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是symbol类型 | ||
* @param data 某个数据 | ||
* @returns 是否是symbol类型 | ||
*/ | ||
export function isSymbol(data: unknown): data is symbol { | ||
return isType(data, 'Symbol') | ||
} |
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/** | ||
* 某个数据是否为true | ||
* @param data 某个数据 | ||
* @returns 是否为true | ||
*/ | ||
export function isTrue(data: unknown): data is true { | ||
return data === true | ||
} |
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { isType } from './isType' | ||
|
||
/** | ||
* 某个数据是否是undefined类型 | ||
* @param data 某个数据 | ||
* @returns 是否是undefined类型 | ||
*/ | ||
export function isUndefined(data: unknown): data is undefined { | ||
return isType(data, '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
File renamed without changes.
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './src' | ||
export * from './src/toSlash' |
This file was deleted.
Oops, something went wrong.
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 str 字符串 | ||
* @returns 转换后的字符串 | ||
*/ | ||
export function toSlash(str: string): string { | ||
return str.replace(/\\/g, '/') | ||
} |
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/** | ||
* 获取某个数据的原始类型 | ||
* @param data 某个数据 | ||
* @returns 原始类型 | ||
*/ | ||
export function toRawType(data: unknown): string { | ||
return Object.prototype.toString.call(data).slice(8, -1) | ||
} |