ts-predicate
Version:
TypeScript predicates and assertions library
261 lines (260 loc) • 7.32 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypeGuard = void 0;
/**
* TypeGuard
*
* @class
*/
class TypeGuard {
/**
* IsPrimitive
*
* @description Predicate that check if a value is primitive. JS primitive values are boolean | number | string | null | undefined.
* @public
* @static
* @param {unknown} value the value
* @return {boolean} a boolean
*/
static IsPrimitive(value) {
if (value === null) {
return true;
}
if (typeof value === "object" || typeof value === "function" || typeof value === "symbol") {
return false;
}
return true;
}
/**
* IsDefined
*
* @description Predicate that check if a value is defined. A value defined is not undefined | null | NaN.
* @public
* @static
* @param {unknown} value the value
* @return {boolean} a boolean
*/
static IsDefined(value) {
return value !== undefined && value !== null && !Number.isNaN(value);
}
/**
* IsBoolean
*
* @description Predicate that check if a value is a boolean.
* @public
* @static
* @param {unknown} value the value
* @return {boolean} a boolean
*/
static IsBoolean(value) {
return typeof value === "boolean";
}
/**
* IsNumber
*
* @description Predicate that check if a value is a number.
* @public
* @static
* @param {unknown} value the value
* @return {boolean} a boolean
*/
static IsNumber(value) {
return typeof value === "number" && !Number.isNaN(value);
}
/**
* IsInteger
*
* @description Predicate that check if a value is an integer.
* @public
* @static
* @param {unknown} value the value
* @return {boolean} a boolean
*/
static IsInteger(value) {
return Number.isSafeInteger(value);
}
/**
* IsFiniteNumber
*
* @description Predicate that check if a value is a finite number.
* @public
* @static
* @param {unknown} value the value
* @return {boolean} a boolean
*/
static IsFiniteNumber(value) {
return Number.isFinite(value);
}
/**
* IsString
*
* @description Predicate that check if a value is a string.
* @public
* @static
* @param {unknown} value the value
* @param {object} [constraints] some additional check
* @param {number} [constraints.minLength] minimal length
* @param {number} [constraints.maxLength] maximum length
* @return {boolean} a boolean
*/
static IsString(value, constraints) {
if (typeof value !== "string") {
return false;
}
if (constraints === undefined) {
return true;
}
if (constraints.minLength !== undefined && value.length < constraints.minLength) {
return false;
}
if (constraints.maxLength !== undefined && value.length > constraints.maxLength) {
return false;
}
return true;
}
/**
* IsFilledString
*
* @description Predicate that check if a value is a non empty string.
* @public
* @static
* @param {unknown} value the value
* @return {boolean} a boolean
*/
static IsFilledString(value) {
return TypeGuard.IsString(value, {
/**
*
*/
minLength: 1
});
}
/**
* IsString
*
* @description Predicate that check if a value is a string.
* @public
* @static
* @param {unknown} value the value
* @param {object} [constraints] some additional check
* @param {number} [constraints.minLength] minimal length
* @param {void} [constraints.itemGuard] another predicate
* @return {boolean} a boolean
*/
static IsArray(value, constraints) {
if (!Array.isArray(value)) {
return false;
}
if (constraints === undefined) {
return true;
}
if (constraints.minLength !== undefined && value.length < constraints.minLength) {
return false;
}
// eslint-disable-next-line unicorn/no-array-callback-reference -- Intended purpose
if (constraints.itemGuard !== undefined && !value.every(constraints.itemGuard)) {
return false;
}
return true;
}
/**
* IsPopulatedArray
*
* @description Predicate that check if a value is a non empty array.
* @public
* @static
* @param {unknown} value the value
* @param {object} [constraints] some additional check
* @param {number} [constraints.minLength] minimal length
* @param {void} [constraints.itemGuard] another predicate
* @return {boolean} a boolean
*/
static IsPopulatedArray(value, constraints) {
return TypeGuard.IsArray(value, Object.assign({
/**
*
*/
minLength: 1 }, constraints));
}
/**
* IsFunction
*
* @description Predicate that check if a value is a function.
* @public
* @static
* @param {unknown} value the value
* @return {boolean} a boolean
*/
// eslint-disable-next-line @typescript-eslint/ban-types -- Allow proper function inference
static IsFunction(value) {
return typeof value === "function";
}
/**
* IsFunction
*
* @description Predicate that check if a value is callable.
* @public
* @static
* @param {unknown} value the value
* @return {boolean} a boolean
*/
// eslint-disable-next-line @typescript-eslint/ban-types -- Allow proper function inference
static IsCallable(value) {
return typeof value === "function" && value.prototype === undefined;
}
/**
* IsRecord
*
* @description Predicate that check if a value is a record.
* @public
* @static
* @param {unknown} value the value
* @return {boolean} a boolean
*/
static IsRecord(value) {
if (!TypeGuard.IsObject(value)) {
return false;
}
const PROTO = Object.getPrototypeOf(value);
return PROTO === null || PROTO === Object.prototype;
}
/**
* IsObject
*
* @description Predicate that check if a value is an object.
* @public
* @static
* @param {unknown} value the value
* @return {boolean} a boolean
*/
static IsObject(value) {
return typeof value === "object" && value !== null;
}
/**
* HasNullableProperty
*
* @description Predicate that check if an object has a nullable property.
* @public
* @static
* @param {object} value the object
* @param {string} property the property
* @return {boolean} a boolean
*/
static HasNullableProperty(value, property) {
return property in value;
}
/**
* HasProperty
*
* @description Predicate that check if an object has a defined property.
* @public
* @static
* @param {object} value the object
* @param {string} property the property
* @return {boolean} a boolean
*/
static HasProperty(value, property) {
return TypeGuard.HasNullableProperty(value, property) && TypeGuard.IsDefined(value[property]);
}
}
exports.TypeGuard = TypeGuard;