UNPKG

@terminus/ngx-tools

Version:

[![CircleCI][circle-badge]][circle-link] [![codecov][codecov-badge]][codecov-project] [![semantic-release][semantic-release-badge]][semantic-release] [![MIT License][license-image]][license-url] <br> [![NPM version][npm-version-image]][npm-url] [![Github

285 lines (263 loc) 7.77 kB
import { isValid } from 'date-fns'; /** * Determine if an item is an Array * * NOTE: This is the fastest performer across all primary browsers. * * @param item - The item to test * @returns The result * * @example * isArray([1, 2]); // Returns: true * isArray<string>(['foo', 'bar']); // Returns: true * isArray('foo'); // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isArray = (item) => !!(item && item.constructor === Array); /** * Determine if an item is null * * @param x - The value to test * @returns The result * * @example * isNull(null) // Returns: true * isNull(1) // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isNull = (x) => x === null; /** * Check that all elements are set * * @param arr - The array * @returns Boolean */ // eslint-disable-next-line @typescript-eslint/no-explicit-any function arrayHasAllElementsSet(arr) { if (!isArray(arr)) { return false; } for (const item of arr) { if (isNull(item) || typeof item === 'undefined') { return false; } } return true; } /** * Determine if the item is an AbstractControl * * @param x - The item to test * @returns The result * * @example * isAbstractControl(new FormControl()) // Returns: true * isAbstractControl('hi') // Returns: false */ const isAbstractControl = // eslint-disable-next-line @typescript-eslint/no-explicit-any (x) => !!x && x.hasOwnProperty('valueChanges'); /** * Determine if an item is a function * * NOTE: While this isn't the fastest performing test in every browser, it is the faster when averaged across the browsers we care about. * * @param x - The item to test * @returns The result * * @example * isFunction(() => {}); // Returns: true * isFunction('foo'); // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isFunction = (x) => !!(x && x.constructor && x.call && x.apply); /** * Determine if all array elements are of a certain type * * @param x - The array to test * @param guard - The function to test for the specific type * @returns The result * * @example * isArrayOfType<number>([1, 5], isNumber) // Returns: true * isArrayOfType<number>([1, 'foo'], isNumber) // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any function isArrayOfType(x, guard) { if (!guard || !isFunction(guard)) { return false; } for (const value of x) { if (!guard(value)) { return false; } } return true; } /** * Determine if a value is a boolean * * @param value - The value to test * @returns The result * * @example * isBoolean(true); // Returns: true * isBoolean('foo'); // Returns: false */ const isBoolean = // eslint-disable-next-line @typescript-eslint/no-explicit-any (value) => value === true || value === false || Object.prototype.toString.call(value) === '[object Boolean]'; /** * Helper function to determine if input is undefined. * * @param input - the input being tested * @returns boolean * * @example * isUndefined(undefined) // Returns: true * isUndefined(null) // Returns: false * isUndefined('foo') // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isUndefined = (input) => input === undefined; /** * Determine if the item has a value * * @param x - The value being tested * @returns The result * * @example * isSet<string>('hi') // Returns: true * isSet<number>(void 0) // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isSet = (x) => !isUndefined(x); /** * Coerce the type to DragEvent * * @param x - The item to test * @returns True if the value is a DragEvent * * @example * isDragEvent(myDragEvent); // Returns: true * isDragEvent(myClickEvent); // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isDragEvent = (x) => !!x && isSet(x.dataTransfer); /** * Coerce the type to HTMLInputElement * * @param x - The item to test * @returns True if the value is a HTMLInputElement * * @example * const myInput = document.querySelector('#myInput'); * const myDiv = document.querySelector('#myDiv'); * * isHTMLInputElement(myInput); // Returns: true * isHTMLInputElement(myDiv); // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isHTMLInputElement = (x) => !!x && isSet(x.files); /** * Determine if an item is an HTTP response * * @param x - The value to check * @returns The result * * @example * isHttpResponse({headers: {...}}) // Returns: true * isHttpResponse<MyResponseType>({foo: 'bar'}) // Returns: false */ const isHttpResponse = (x) => !isNull(x) && !isUndefined(x) && x.hasOwnProperty('headers'); /** * Coerce the type to KeyboardEvent * * @param x - The item to test * @returns True if the value is a KeyboardEvent * * @example * isKeyboardEvent(myKeyboardEvent); // Returns: true * isKeyboardEvent(myClickEvent); // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isKeyboardEvent = (x) => !!x && isSet(x.code); /** * Coerce the type to MouseEvent * * @param x - The item to test * @returns True if the value is a MouseEvent * * @example * isMouseEvent(myMouseEvent); // Returns: true * isMouseEvent(myKeyboardEvent); // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isMouseEvent = (x) => !!x && isSet(x.relatedTarget); /** * Determine if a value is a number * * @param value - The value to check * @returns The result * * @example * isNumber(2) // Returns: true * isNumber('2') // Returns: true * isNumber('a') // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isNumber = (value) => !isNaN(parseFloat(value)) && !isNaN(Number(value)); /** * Determine if an item is an object * * @param x - The item to test * @returns The result * * @example * isObject({}); // Returns: true * isObject('foo'); // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isObject = (x) => Object.prototype.toString.call(x) === '[object Object]'; /** * Determine if a value is a string * * @param x - The value to test * @returns The result * * @example * isString('foo'); // Returns: true * isString({}); // Returns: false */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isString = (x) => !!(typeof x === 'string' || x instanceof String); /** * Determine if an item is a token response * * @param x - The item to check * @returns The result * * @example * isTokenResponse({token: 'any'}) // Returns: true * isTokenResponse<MyResponseType>({foo: 'bar'}) // Returns: false */ const isTokenResponse = (x) => !isNull(x) && !isUndefined(x) && x.hasOwnProperty('token'); /** * Determine if an item is a valid date * * @param value - The item to test * @returns The result * * @example * isValidDate('foo'); // Returns: false * isValidDate('2020-02-07'); // Returns: true */ function isValidDate(value) { const date = (typeof value === 'string') ? new Date(value) : value; return isValid(date); } /** * Generated bundle index. Do not edit. */ export { arrayHasAllElementsSet, isAbstractControl, isArray, isArrayOfType, isBoolean, isDragEvent, isFunction, isHTMLInputElement, isHttpResponse, isKeyboardEvent, isMouseEvent, isNull, isNumber, isObject, isSet, isString, isTokenResponse, isUndefined, isValidDate }; //# sourceMappingURL=terminus-ngx-tools-type-guards.js.map