|
| 1 | +export const isSymbol = (value: any): value is symbol => { |
| 2 | + return Boolean(value) && value.constructor === Symbol; |
| 3 | +}; |
| 4 | + |
| 5 | +export const isArray = Array.isArray; |
| 6 | + |
| 7 | +export const isObject = (value: any): value is object => { |
| 8 | + return Boolean(value) && value.constructor === Object; |
| 9 | +}; |
| 10 | + |
| 11 | +/** |
| 12 | + * Checks if the given value is primitive. |
| 13 | + * |
| 14 | + * Primitive Types: number , string , boolean , symbol, bigint, undefined, null |
| 15 | + * |
| 16 | + * @param {*} value value to check |
| 17 | + * @returns {boolean} result |
| 18 | + */ |
| 19 | +export const isPrimitive = (value: any): boolean => { |
| 20 | + return value === undefined || value === null || (typeof value !== 'object' && typeof value !== 'function'); |
| 21 | +}; |
| 22 | + |
| 23 | +// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type |
| 24 | +export const isFunction = (value: any): value is Function => { |
| 25 | + return Boolean(value && value.constructor && value.call && value.apply); |
| 26 | +}; |
| 27 | + |
| 28 | +export const isString = (value: any): value is string => { |
| 29 | + return typeof value === 'string' || value instanceof String; |
| 30 | +}; |
| 31 | + |
| 32 | +export const isNumber = (value: any): value is number => { |
| 33 | + try { |
| 34 | + return Number(value) === value; |
| 35 | + } catch { |
| 36 | + return false; |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +export const isInt = (value: any): value is number => { |
| 41 | + return isNumber(value) && value % 1 === 0; |
| 42 | +}; |
| 43 | + |
| 44 | +export const isFloat = (value: any): value is number => { |
| 45 | + return isNumber(value) && value % 1 !== 0; |
| 46 | +}; |
| 47 | + |
| 48 | +export const isDate = (value: any): value is Date => { |
| 49 | + return Object.prototype.toString.call(value) === '[object Date]'; |
| 50 | +}; |
| 51 | + |
| 52 | +/** |
| 53 | + * This is really a _best guess_ promise checking. You |
| 54 | + * should probably use Promise.resolve(value) to be 100% |
| 55 | + * sure you're handling it correctly. |
| 56 | + */ |
| 57 | +export const isPromise = (value: any): value is Promise<any> => { |
| 58 | + if (!value) return false; |
| 59 | + if (!value.then) return false; |
| 60 | + if (!isFunction(value.then)) return false; |
| 61 | + return true; |
| 62 | +}; |
| 63 | + |
| 64 | +export const isEmpty = (value: any) => { |
| 65 | + if (value === true || value === false) return true; |
| 66 | + if (value === null || value === undefined) return true; |
| 67 | + if (isNumber(value)) return value === 0; |
| 68 | + if (isDate(value)) return Number.isNaN(value.getTime()); |
| 69 | + if (isFunction(value)) return false; |
| 70 | + if (isSymbol(value)) return false; |
| 71 | + const length = (value as any).length; |
| 72 | + if (isNumber(length)) return length === 0; |
| 73 | + const size = (value as any).size; |
| 74 | + if (isNumber(size)) return size === 0; |
| 75 | + const keys = Object.keys(value).length; |
| 76 | + return keys === 0; |
| 77 | +}; |
| 78 | + |
| 79 | +export const isEqual = <TType>(x: TType, y: TType): boolean => { |
| 80 | + if (Object.is(x, y)) return true; |
| 81 | + if (x instanceof Date && y instanceof Date) { |
| 82 | + return x.getTime() === y.getTime(); |
| 83 | + } |
| 84 | + if (x instanceof RegExp && y instanceof RegExp) { |
| 85 | + return x.toString() === y.toString(); |
| 86 | + } |
| 87 | + if (typeof x !== 'object' || x === null || typeof y !== 'object' || y === null) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + const keysX = Reflect.ownKeys(x as unknown as object) as (keyof typeof x)[]; |
| 91 | + const keysY = Reflect.ownKeys(y as unknown as object); |
| 92 | + if (keysX.length !== keysY.length) return false; |
| 93 | + for (let i = 0; i < keysX.length; i += 1) { |
| 94 | + if (!Reflect.has(y as unknown as object, keysX[i])) return false; |
| 95 | + if (!isEqual(x[keysX[i]], y[keysX[i]])) return false; |
| 96 | + } |
| 97 | + return true; |
| 98 | +}; |
0 commit comments