UNPKG

ts-prime

Version:

A utility library for JavaScript and Typescript.

183 lines (182 loc) 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Checks if `data` is `Number` * @param data - Anything * @example * const item = { data: 1 } as { data: number } | string * if (P.isString(item)) { * console.log("My name is", item) * } * @category Guard */ // @ts-ignore function isString(data) { return typeof data === 'string'; } exports.isString = isString; /** * Checks if `data` is `Number` * @param data - Anything * @example * const item = { data: 1 } as { data: number } | number * if (P.isNumber(item)) { * console.log("My phone number is", item) * } * @category Guard */ // @ts-ignore function isNumber(data) { return typeof data === 'number' && !isNaN(data); } exports.isNumber = isNumber; /** * Checks if `data` is defined * @param data - Anything * @example * const item = { data: 1 } as { data: number } | undefined * if (P.isDefined(item)) { * console.log(item.data) * } * @category Guard */ function isDefined(data) { return typeof data !== 'undefined' && data !== null; } exports.isDefined = isDefined; /** * Checks if `data` is boolean * @param data - Anything * @example * const item = false as { data: number } | undefined | boolean * if (P.isBoolean(item)) { * console.log(item.data) * } * @category Guard */ // @ts-ignore function isBoolean(data) { return typeof data === 'boolean'; } exports.isBoolean = isBoolean; /** * Checks if `data` is `Promise`. * @param data - Anything * @example * const item = { data: 1 } as { data: number } | Promise<string[]> * if (P.isPromise(item)) { * const result = await item * console.log(Promise resolved,result.map((q)=> q.match(/.../))) * } * // Item is not promise * @category Guard */ // @ts-ignore function isPromise(data) { return data instanceof Promise; } exports.isPromise = isPromise; /** * Checks if `data` is `array`. * @param data - Anything * @example * const item = { data: 1 } as { data: number } | string[] * if (P.isArray(item)) { * console.log(item.map((q)=> q.match(/.../))) * } * * const items = [{ data: 1 },[],"1",4,P.clamp].filter(P.isArray) //=> ["1"] * @category Guard */ function isArray(data // @ts-ignore ) { return Array.isArray(data); } exports.isArray = isArray; /** * Checks if `data` is `object`. * @warning This function does not treat `Array` as `object` * @param data - Anything * @example * const item = { data: 1 } as { data: number } | string[] * if (P.isObject(item)) { * console.log(item.data) * } * * const items = [{ data: 1 },[],1,4,P.clamp].filter(P.isObject) //=> [{ data: 1 }] * @category Guard */ function isObject(data // @ts-ignore ) { return !!data && !Array.isArray(data) && typeof data === 'object'; } exports.isObject = isObject; /** * Checks if `data` is `function` * @param data - Anything * @example * const item = P.clamp as unknown * if (P.isNil(item)) { * // Item is definitely function * console.log('Nice function', item.name) * } * * const items = [1,2,3,4,P.clamp].filter(P.isFunction) //=> [P.clamp] * @category Guard */ // @ts-ignore function isFunction(data) { return typeof data === 'function'; } exports.isFunction = isFunction; /** * Checks if `data` is `null` or `undefined`. Executes basic `data == null` evaluation * @param data - Anything * @example * const item = undefined as unknown * if (P.isNil(item)) { * // Item is definitely null | undefined * console.log('Do something') * } * * const items = [1,2,3,4,undefined].filter(P.isNil) //=> [undefined] * @category Guard */ function isNil(data) { return data == null; } exports.isNil = isNil; /** * Checks if `data` is instance of Error class * @param data - Anything * @example * const item = new Error('Error') as unknown * if (P.isError(item)) { * // This is definitely an error and Typescript resolves it * console.log(item.message) * } * * const items = [1,2,3,4,new Error('Error')].filter(P.isError) //=> [new Error('Error')] * @category Guard */ // @ts-ignore function isError(data) { return data instanceof Error; } exports.isError = isError; function isNot(predicate) { return function (data) { return !predicate(data); }; } exports.isNot = isNot; function haveKeys(keys) { return function (data) { return keys.every(function (q) { return isDefined(data[q]); }); }; } exports.haveKeys = haveKeys;