@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
308 lines (286 loc) • 8.8 kB
JavaScript
import { __values } from 'tslib';
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
var isArray = function (item) { return !!(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
var isNull = function (x) { return 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) {
var e_1, _a;
if (!isArray(arr)) {
return false;
}
try {
for (var arr_1 = __values(arr), arr_1_1 = arr_1.next(); !arr_1_1.done; arr_1_1 = arr_1.next()) {
var item = arr_1_1.value;
if (isNull(item) || typeof item === 'undefined') {
return false;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (arr_1_1 && !arr_1_1.done && (_a = arr_1.return)) _a.call(arr_1);
}
finally { if (e_1) throw e_1.error; }
}
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
*/
var isAbstractControl =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function (x) { return !!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
var isFunction = function (x) { return !!(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) {
var e_1, _a;
if (!guard || !isFunction(guard)) {
return false;
}
try {
for (var x_1 = __values(x), x_1_1 = x_1.next(); !x_1_1.done; x_1_1 = x_1.next()) {
var value = x_1_1.value;
if (!guard(value)) {
return false;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (x_1_1 && !x_1_1.done && (_a = x_1.return)) _a.call(x_1);
}
finally { if (e_1) throw e_1.error; }
}
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
*/
var isBoolean =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function (value) { return 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
var isUndefined = function (input) { return 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
var isSet = function (x) { return !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
var isDragEvent = function (x) { return !!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
var isHTMLInputElement = function (x) { return !!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
*/
var isHttpResponse = function (x) { return !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
var isKeyboardEvent = function (x) { return !!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
var isMouseEvent = function (x) { return !!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
var isNumber = function (value) { return !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
var isObject = function (x) { return 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
var isString = function (x) { return !!(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
*/
var isTokenResponse = function (x) { return !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) {
var 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