UNPKG

@nasriya/atomix

Version:

Composable helper functions for building reliable systems

376 lines (375 loc) 14.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const strings_utils_1 = __importDefault(require("./domains/data-types/string/strings-utils")); const objects_utils_1 = __importDefault(require("./domains/data-types/object/objects-utils")); const records_utils_1 = __importDefault(require("./domains/data-types/record/records-utils")); const arrays_utils_1 = __importDefault(require("./domains/data-types/array/arrays-utils")); const numbers_utils_1 = __importDefault(require("./domains/data-types/number/numbers-utils")); const regex_utils_1 = __importDefault(require("./domains/data-types/regex/regex-utils")); class ValueIs { /** * Checks if the provided value is defined, i.e. not null and not undefined. * * @param value - The value to check. * @returns True if the value is defined, otherwise false. * @example * valueIs.defined(null); // ❌ false * valueIs.defined(undefined); // ❌ false * valueIs.defined(0); // ✅ true * @since v1.0.0 */ defined(value) { return value !== undefined && value !== null; } /** * Checks if the provided value is an instance of the specified class. * * @template T - The type of the instance. * @param value - The value to check. * @param constructor - The constructor function to check against. * @returns True if the value is an instance of the constructor, otherwise false. * @example * const date = new Date(); * valueIs.instanceOf(date, Date); // ✅ true * @example * const number = 42; * valueIs.instanceOf(number, Date); // ❌ false * @since v1.0.0 */ instanceOf(value, constructor) { return value instanceof constructor; } /** * Checks if the provided value is an instance of the specified class, or if its * type matches the constructor in a loose sense. * * For example, if the value is a number, it will return true for the Number * constructor, even if the value is not an instance of Number (i.e. it is a * primitive number, not a boxed Number object). * * @template T - The type of the instance. * @param value - The value to check. * @param constructor - The constructor function to check against. * @returns True if the value is an instance of the constructor, or if its type * matches the constructor in a loose sense, otherwise false. * @example * const number = 42; * valueIs.instanceOfLoose(number, Number); // ✅ true * @example * const string = 'hello'; * valueIs.instanceOfLoose(string, String); // ✅ true * @example * const boolean = true; * valueIs.instanceOfLoose(boolean, Boolean); // ✅ true * @example * const date = new Date(); * valueIs.instanceOfLoose(date, Date); // ✅ true * @example * const invalid = null; * valueIs.instanceOfLoose(invalid, Date); // ❌ false * @since v1.0.0 */ instanceOfLoose(value, constructor) { if (typeof value === 'object' && value !== null) { return value instanceof constructor; } // Handle boxed primitives like Number, String, Boolean const type = constructor; switch (type) { case Number: return typeof value === 'number'; case String: return typeof value === 'string'; case Boolean: return typeof value === 'boolean'; default: return false; } } // ========================================== // ============= Numbers Guards ============= // ========================================== /** * Checks if the provided value is a number. * * @param value - The value to check. * @returns True if the value is a number, otherwise false. * @since v1.0.0 */ number = numbers_utils_1.default.guard.isNumber; /** * Checks if the provided value is a negative number. * * @param value - The value to check. * @returns True if the value is a negative number, otherwise false. * @since v1.0.0 */ negativeNumber = numbers_utils_1.default.guard.isNegative.bind(numbers_utils_1.default.guard); /** * Checks if the provided value is a positive number. * * @param value - The value to check. * @returns True if the value is a positive number, otherwise false. * @since v1.0.0 */ positiveNumber = numbers_utils_1.default.guard.isPositive.bind(numbers_utils_1.default.guard); /** * Checks if the provided value is an integer. * * @param value - The value to check. * @returns True if the value is an integer, otherwise false. * @since v1.0.0 */ integer = numbers_utils_1.default.guard.isInteger.bind(numbers_utils_1.default.guard); /** * Checks if the provided value is a floating point number. * * @param value - The value to check. * @returns True if the value is a floating point number, otherwise false. * @since v1.0.0 */ float = numbers_utils_1.default.guard.isFloat.bind(numbers_utils_1.default.guard); /** * Checks if the provided value is a finite number. * * @param value - The value to check. * @returns True if the value is a finite number, otherwise false. * @since v1.0.0 */ finite = numbers_utils_1.default.guard.isFinite.bind(numbers_utils_1.default.guard); /** * Checks if the provided value is `NaN` (Not-a-Number). * * @param value - The value to check. * @returns True if the value is NaN, otherwise false. * @since v1.0.0 */ NaN = numbers_utils_1.default.guard.isNaN.bind(numbers_utils_1.default.guard); // ========================================= // ============= String Guards ============= // ========================================= /** * Checks if the provided value is a string. * * @param value - The value to check. * @returns True if the value is a string, otherwise false. * @since v1.0.0 */ string = strings_utils_1.default.guard.isString; /** * Checks if the provided string value is blank (contains only whitespace). * * @param value - The value to check. * @returns True if the value is a string containing only whitespace, otherwise false. * @since v1.0.0 */ blankString = strings_utils_1.default.guard.isBlank.bind(strings_utils_1.default.guard); /** * Checks if the provided string value is empty. * * @param value - The value to check. * @returns True if the value is an empty string, otherwise false. * @since v1.0.0 */ emptyString = strings_utils_1.default.guard.isEmpty.bind(strings_utils_1.default.guard); /** * Checks if the provided string value is not empty. * * @param value - The value to check. * @returns True if the value is a non-empty string, otherwise false. * @since v1.0.0 */ notEmptyString = strings_utils_1.default.guard.isNotEmpty.bind(strings_utils_1.default.guard); /** * Checks if the provided value is a valid string. * * @param value - The value to check. * @returns True if the value is a valid string, otherwise false. * @since v1.0.0 * @description * A valid string is a string that is not empty and has some content. * The string is trimmed before its length is checked. */ validString = strings_utils_1.default.guard.isValidString.bind(strings_utils_1.default.guard); /** * Checks if the provided value is an alphabetic string. * * @param value - The value to check. * @returns True if the value is a string containing only alphabetic characters, otherwise false. * @since v1.0.0 */ alphaString = strings_utils_1.default.guard.isAlpha; /** * Checks if the provided value is an alphanumeric string. * * @param value - The value to check. * @returns True if the value is a string containing only alphabetic and numeric characters, otherwise false. * @since v1.0.0 */ alphaNumericString = strings_utils_1.default.guard.isAlphaNumeric; /** * Checks if the provided value is a valid UUID string. * * @param value - The value to check. * @param version - The UUID version to check against, defaults to v4. * @returns True if the value is a valid UUID, otherwise false. * @since v1.0.0 * @description * A valid UUID is a string of the following format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx * The format is different for each version. */ uuid = strings_utils_1.default.guard.isUUID; /** * Checks if the given pattern is a glob-like pattern, meaning it contains at least one of the * characters `*` or `?`. * @param pattern The pattern to check. * @returns true if the given pattern is a glob-like pattern, false otherwise. * @since v1.0.0 */ globLike = regex_utils_1.default.guard.isGlobLike; // ========================================== // ============= Objects Guards ============= // ========================================== /** * Checks if the provided value is an object. * * @param value - The value to check. * @returns True if the value is an object, otherwise false. * @since v1.0.0 * @description * An object is a value that is not null and is of type object. * This includes objects created with the Object constructor, objects * created with object literals, and objects created with the new keyword. * * @example * const object = { }; * objectsUtils.guard.isObject(object); // true * @example * const invalid = null; * objectsUtils.guard.isObject(invalid); // false * @example * class MyClass { } * const invalid = new MyClass(); * objectsUtils.guard.isObject(invalid); // false */ object = objects_utils_1.default.guard.isObject; /** * Checks if the provided value is freezable. * * @param value - The value to check. * @returns True if the value is freezable, otherwise false. * @since v1.0.0 * @description * A freezable is a value that is an object or an array. * This means that the value can be frozen using the Object.freeze method. * * @example * const object = { }; * objectsUtils.guard.isFreezable(object); // true * @example * const array = [1, 2, 3]; * objectsUtils.guard.isFreezable(array); // true * @example * const invalid = null; * objectsUtils.guard.isFreezable(invalid); // false * @example * class MyClass { } * const invalid = new MyClass(); * objectsUtils.guard.isFreezable(invalid); // false */ freezable = objects_utils_1.default.guard.isFreezable.bind(objects_utils_1.default.guard); // ========================================== // ============= Records Guards ============= // ========================================== /** * Checks if the provided value is a Record. * * @param value - The value to check. * @returns True if the value is a Record, otherwise false. * @since v1.0.0 * @description * A Record is an object that is not null, is not an array, and contains * at least one key-value pair. * * @example * const record = { foo: 'bar' }; * recordsUtils.guard.isRecord(record); // ✅ true * @example * const invalid = null; * recordsUtils.guard.isRecord(invalid); // ❌ false * @example * const invalid = [1, 2, 3]; * recordsUtils.guard.isRecord(invalid); // ❌ false */ record = records_utils_1.default.guard.isRecord; /** * Checks if the provided value is an empty Record. * * @param value - The value to check. * @returns True if the value is an empty Record, otherwise false. * @since v1.0.0 * @description * An empty Record is a Record that contains no key-value pairs. * * @example * const record = { }; * recordsUtils.guard.isEmpty(record); // ✅ true * @example * const invalid = null; * recordsUtils.guard.isEmpty(invalid); // ❌ false * @example * const invalid = [1, 2, 3]; * recordsUtils.guard.isEmpty(invalid); // ❌ false */ emptyRecord = records_utils_1.default.guard.isEmpty.bind(records_utils_1.default.guard); // ========================================== // ============= Arrays Guards ============== // ========================================== /** * Checks if the provided value is an array. * * @param value - The value to check. * @returns True if the value is an array, otherwise false. * @since v1.0.0 */ array = arrays_utils_1.default.guard.isArray; /** * Checks if the provided value is a non-empty array. * * @param value - The value to check. * @returns True if the value is a non-empty array, otherwise false. * @since v1.0.0 */ notEmptyArray = arrays_utils_1.default.guard.isNotEmpty.bind(arrays_utils_1.default.guard); /** * Checks if the provided value is an array of the given type. * * @param itemGuard - A function that takes an item and returns true if the * item is of the given type, otherwise false. * @returns A function that takes a value and returns true if the value is an * array and every item in the array is of the given type, otherwise false. * @since v1.0.0 */ arrayOf = arrays_utils_1.default.guard.isArrayOf.bind(arrays_utils_1.default.guard); /** * Checks if the provided value is an array of numbers. * * @param value - The value to check. * @returns True if the value is an array of numbers, otherwise false. * @since v1.0.0 */ arrayOfNumbers = arrays_utils_1.default.guard.isArrayOfNumbers.bind(arrays_utils_1.default.guard); /** * Checks if the provided value is an array of strings. * * @param value - The value to check. * @returns True if the value is an array of strings, otherwise false. * @since v1.0.0 */ arrayOfStrings = arrays_utils_1.default.guard.isArrayOfStrings.bind(arrays_utils_1.default.guard); } const valueIs = new ValueIs; exports.default = valueIs;