Skip to content

Commit 1ecaac3

Browse files
committed
refactor(shared): Use @@toStringTag rather than instance
1 parent d7e406f commit 1ecaac3

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

packages/shared/src/general.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ export const hasOwn = (
3737
): key is keyof typeof val => hasOwnProperty.call(val, key)
3838

3939
export const isArray: typeof Array.isArray = Array.isArray
40-
export const isMap = (val: unknown): val is Map<any, any> => val instanceof Map
41-
export const isSet = (val: unknown): val is Set<any> => val instanceof Set
42-
43-
export const isDate = (val: unknown): val is Date => val instanceof Date
44-
export const isRegExp = (val: unknown): val is RegExp => val instanceof RegExp
40+
export const isMap = (val: unknown): val is Map<any, any> =>
41+
!!val && (val as any)[Symbol.toStringTag] === 'Map'
42+
export const isSet = (val: unknown): val is Set<any> =>
43+
!!val && (val as any)[Symbol.toStringTag] === 'Set'
44+
45+
export const isDate = (val: unknown): val is Date =>
46+
toTypeString(val) === '[object Date]'
47+
export const isRegExp = (val: unknown): val is RegExp =>
48+
// refer https://262.ecma-international.org/15.0/index.html?_gl=1*tsu3al*_ga*ODYzNDU4MjA5LjE3MjQ3Nzc5NDY.*_ga_TDCK4DWEPP*MTczMTg0NTY5MS41LjAuMTczMTg0NTY5MS4wLjAuMA..#sec-isregexp
49+
!!val && !!(val as any)[Symbol.match]
4550
export const isFunction = (val: unknown): val is Function =>
4651
typeof val === 'function'
4752
export const isString = (val: unknown): val is string => typeof val === 'string'
@@ -50,7 +55,7 @@ export const isObject = (val: unknown): val is Record<any, any> =>
5055
val !== null && typeof val === 'object'
5156

5257
export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
53-
return val instanceof Promise
58+
return !!val && (val as any)[Symbol.toStringTag] === 'Promise'
5459
}
5560

5661
export const objectToString: typeof Object.prototype.toString =

0 commit comments

Comments
 (0)