UNPKG

ts-std-lib

Version:
233 lines 7.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Type = void 0; const util_1 = require("util"); /** * For runtime type operations */ class Type { /** * Improved `typeof value` function, accounts for null and arrays. * @param value The value to test. */ static of(value) { if (value === null) { return 'null'; } if (Array.isArray(value)) { return 'array'; } return typeof value; } // /** // * Check for the same javascript type, for primitives and for custom types (using ) // * @param object // * @param other // */ // public static isSame<T>(object: unknown, other: T): object is T { // if (object === null || other === null) { // return Object.is(other, object); // } // if (typeof object !== 'object' || typeof other !== 'object') { // return typeof other === typeof object; // } // return Reflect.getPrototypeOf(object) === Reflect.getPrototypeOf(other as any); // } /** * Check if a value is an instance of a type * @param constructor The type * @param value The value */ static isInstanceOf(constructor, value) { return value instanceof constructor; } /** * Check if a value is null * @param value The value */ static isNull(value) { return value === null; } /** * Check if a value is undefined * @param value The value */ static isUndefined(value) { return value === undefined; } /** * Check if a value is a boolean * @param value The value */ static isBoolean(value) { return typeof value === 'boolean'; } /** * Check if a value is a number * @param value The value */ static isNumber(value) { return typeof value === 'number'; } /** * Check if a value is a BigInt * @param value The value */ static isBigInt(value) { return typeof value === 'bigint'; } /** * Check if a value is a string * @param value The object */ static isString(value) { return typeof value === 'string'; } /** * Check if a value is a function * @param value The value */ // tslint:disable-next-line:ban-types static isFunction(value) { return typeof value === 'function'; } /** * Check if a value is a symbol * @param value The value */ static isSymbol(value) { return typeof value === 'symbol'; } /** * Check if a value is a generator function * @param value The value */ static isGeneratorFunction(value) { return util_1.types.isGeneratorFunction(value); } /** * Check if a value is an async function * @param value The value */ static isAsyncFunction(value) { return util_1.types.isAsyncFunction(value); } /** * Check if a value is a promise * @param value The value */ static isPromise(value) { return util_1.types.isPromise(value); } /** * Check if a value is a proxy * @param value The value */ static isProxy(value) { return util_1.types.isProxy(value); } /** * Check if a value is a regular expression * @param value The value */ static isRegExp(value) { return util_1.types.isRegExp(value); } /** * Check if a value is an iterable * @param value The value */ static isIterable(value) { if (typeof value !== 'object' || value === null) { return false; } return Symbol.iterator in value && typeof value[Symbol.iterator] === 'function'; } /** * Check if a value is an async iterable * @param value The value */ static isAsyncIterable(value) { if (typeof value !== 'object' || value === null) { return false; } return Symbol.asyncIterator in value && typeof (value)[Symbol.asyncIterator] === 'function'; } /** * Check if a value is an array * @param value The value */ static isArray(value) { return Array.isArray(value); } /** * Check if a value is array like * @param value The value */ static isArrayLike(value) { if (typeof value !== 'object' || value === null) { return false; } return Type.hasKeysOf(value, ['length']); } /** * Check if a value is a non-null object * @param value The value */ static isObject(value) { if (typeof value !== 'object' || value === null || Array.isArray(value) || util_1.types.isRegExp(value)) { return false; } return true; } /** * Check if a value is a class * @param value The value */ // tslint:disable-next-line:ban-types static isClass(value) { if (typeof value !== 'object' || value === null || Array.isArray(value) || util_1.types.isRegExp(value)) { return false; } return value.constructor !== Object.prototype.constructor; // TODO: Is this the best way to test is a class? } /** * Check if an object is a primitive * @param value The value */ static isPrimitive(value) { if (value === null) { return false; } if (value === undefined) { return false; } return (value !== Object(value)); } // public static hasKeysOf<T extends object>(value: unknown, expectedKeys: ReadonlyArray<keyof T> | ReadonlyMap<keyof T, typeof value[keyof T]>): value is T { static hasKeysOf(value, expectedKeys) { // public static hasKeysOf<T extends object>(value: unknown, expectedKeys: ReadonlyArray<keyof T> | { readonly [key: keyof T]: string }): value is T { // public static hasKeysOf<T extends object>(value: unknown, expectedKeys: ReadonlyArray<keyof T>): value is T { if (typeof value !== 'object' || value === null) { return false; } const expectedKeyMap = Type.isArray(expectedKeys) ? new Map(expectedKeys.map((expectedKey) => [expectedKey, '*'])) : expectedKeys; // : new Map(Object.entries(expectedKeys)); for (const [expectedKey, expectedKeyType] of expectedKeyMap) { if (!(expectedKey in value)) { return false; } if (expectedKeyType === '*') { continue; } if (typeof value[expectedKey] !== expectedKeyType) { return false; } } return true; } } exports.Type = Type; //# sourceMappingURL=Type.js.map