nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.
169 lines (168 loc) • 5.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isMethodDescriptor = void 0;
exports.isArray = isArray;
exports.isValidArray = isValidArray;
exports.isObject = isObject;
exports.isNotEmptyObject = isNotEmptyObject;
exports.isObjectWithKeys = isObjectWithKeys;
exports.isEmptyObject = isEmptyObject;
exports.isFunction = isFunction;
exports.isDate = isDate;
exports.isArrayOfType = isArrayOfType;
exports.isPromise = isPromise;
exports.isSet = isSet;
exports.isMap = isMap;
exports.isRegExp = isRegExp;
exports.isError = isError;
exports.isJSON = isJSON;
exports.isReturningPromise = isReturningPromise;
const primitives_1 = require("./primitives");
/**
* * Type guard to check if a value is an array.
* @param value - The value to check.
* @returns `true` if the value is an array, otherwise `false`.
*/
function isArray(value) {
return Array.isArray(value);
}
/**
* * Type guard to check if a value is an array with length.
* @param value - The value to check.
* @returns `true` if the value is an array with length, otherwise `false`.
*/
function isValidArray(value) {
return Array.isArray(value) && value?.length > 0;
}
/**
* * Type guard to check if a value is an object (excluding null).
* @param value - The value to check.
* @returns `true` if the value is an object, otherwise `false`.
*/
function isObject(value) {
return value !== null && typeof value === 'object' && !isArray(value);
}
/**
* * Type guard to check if a value is an object (excluding null) and has keys in it.
* @param value - The value to check.
* @returns `true` if the value is an object with valid keys, otherwise `false`.
*/
function isNotEmptyObject(value) {
return isObject(value) && Object.keys(value)?.length > 0;
}
/**
* * Type guard to check if a value is an object with specific keys.
* @param value - The value to check.
* @param keys - The set of keys the object should contain.
* @returns `true` if the value is an object with the specified keys, otherwise `false`.
*/
function isObjectWithKeys(value, keys) {
return isObject(value) && keys?.every((key) => key in value);
}
/**
* * Type guard to check if a value is an empty object.
* @param value - The value to check.
* @returns `true` if the value is an empty object, otherwise `false`.
*/
function isEmptyObject(value) {
return isObject(value) && Object.keys(value)?.length === 0;
}
/**
* * Type guard to check if a value is a function.
* @param value - The value to check.
* @returns `true` if the value is a function, otherwise `false`.
*/
function isFunction(value) {
return typeof value === 'function';
}
/**
* * Determines whether the provided property descriptor represents a method.
*
* @param descriptor - The property descriptor to check.
* @returns `true` if the descriptor is defined and its value is a function; otherwise, `false`.
*/
const isMethodDescriptor = (descriptor) => {
return !!descriptor && typeof descriptor?.value === 'function';
};
exports.isMethodDescriptor = isMethodDescriptor;
/**
* * Type guard to check if a value is a Date object.
* @param value - The value to check.
* @returns `true` if the value is a Date object, otherwise `false`.
*/
function isDate(value) {
return value instanceof Date;
}
/**
* * Type guard to check if a value is an array of a specific type.
* @param value - The value to check.
* @param typeCheck - The type guard function to check each item of the array.
* @returns `true` if the value is an array of the specified type, otherwise `false`.
*/
function isArrayOfType(value, typeCheck) {
return isArray(value) && value?.every(typeCheck);
}
/**
* * Type guard to check if a value is a Promise.
* @param value - The value to check.
* @returns `true` if the value is a Promise, otherwise `false`.
*/
function isPromise(value) {
return isObject(value) && isFunction(value.then);
}
/**
* * Type guard to check if a value is a Set.
* @param value - The value to check.
* @returns `true` if the value is a Set, otherwise `false`.
*/
function isSet(value) {
return value instanceof Set;
}
/**
* * Type guard to check if a value is a Map.
* @param value - The value to check.
* @returns `true` if the value is a Map, otherwise `false`.
*/
function isMap(value) {
return value instanceof Map;
}
/**
* * Type guard to check if a value is a RegExp.
* @param value - The value to check.
* @returns `true` if the value is a RegExp, otherwise `false`.
*/
function isRegExp(value) {
return value instanceof RegExp;
}
/**
* * Type guard to check if a value is an Error object.
* @param value - The value to check.
* @returns `true` if the value is an Error object, otherwise `false`.
*/
function isError(value) {
return value instanceof Error;
}
/**
* * Type guard to check if a string is valid JSON.
* @param value - The value to check.
* @returns `true` if the value is valid JSON, otherwise `false`.
*/
function isJSON(value) {
if (!(0, primitives_1.isString)(value))
return false;
try {
JSON.parse(value);
return true;
}
catch {
return false;
}
}
/**
* * Type guard to check if a function returns a Promise.
* @param fn - The function to check.
* @returns `true` if the function returns a Promise, otherwise `false`.
*/
function isReturningPromise(fn) {
return isFunction(fn) && fn.constructor?.name === 'AsyncFunction';
}