UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.

138 lines (137 loc) 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isNumber = isNumber; exports.isString = isString; exports.isInteger = isInteger; exports.isPositiveInteger = isPositiveInteger; exports.isBoolean = isBoolean; exports.isNull = isNull; exports.isUndefined = isUndefined; exports.isSymbol = isSymbol; exports.isBigInt = isBigInt; exports.isPrimitive = isPrimitive; exports.isNormalPrimitive = isNormalPrimitive; exports.isNonEmptyString = isNonEmptyString; exports.isFalsy = isFalsy; exports.isTruthy = isTruthy; /** * * Type guard to check whether a value is a finite number (excluding `NaN` and `Infinity`). * @param value - The value to test. * @returns `true` if the value is a finite number; otherwise `false`. */ function isNumber(value) { return typeof value === 'number' && Number.isFinite(value); } /** * * Type guard to check if a value is a string. * @param value - The value to check. * @returns `true` if the value is a string, otherwise `false`. */ function isString(value) { return typeof value === 'string'; } /** * * Type guard to check if a value is an integer. * @param value - The value to check. * @returns `true` if the value is an integer, otherwise `false`. */ function isInteger(value) { return isNumber(value) && Number.isInteger(value); } /** * * Type guard to check if a value is a positive integer. * @param value - The value to check. * @returns `true` if the value is a positive integer, otherwise `false`. */ function isPositiveInteger(value) { return isInteger(value) && value > 0; } /** * * Type guard to check if a value is a boolean. * @param value - The value to check. * @returns `true` if the value is a boolean, otherwise `false`. */ function isBoolean(value) { return typeof value === 'boolean'; } /** * * Type guard to check if a value is null. * @param value - The value to check. * @returns `true` if the value is null, otherwise `false`. */ function isNull(value) { return value === null; } /** * * Type guard to check if a value is undefined. * @param value - The value to check. * @returns `true` if the value is undefined, otherwise `false`. */ function isUndefined(value) { return value === undefined; } /** * * Type guard to check if a value is a symbol. * @param value - The value to check. * @returns `true` if the value is a symbol, otherwise `false`. */ function isSymbol(value) { return typeof value === 'symbol'; } /** * * Type guard to check if a value is a BigInt. * @param value - The value to check. * @returns `true` if the value is a BigInt, otherwise `false`. */ function isBigInt(value) { return typeof value === 'bigint'; } /** * * Type guard to check if a value is a primitive (i.e. `string | number | boolean | symbol | bigint | null | undefined`). * @param value - The value to check. * @returns `true` if the value is a primitive, otherwise `false`. */ function isPrimitive(value) { return (value === null || [ 'string', 'number', 'boolean', 'symbol', 'bigint', 'undefined', ].includes(typeof value)); } /** * * Type guard to check if a value is a normal primitive (i.e. `string | number | boolean | null | undefined`). * @param value - The value to check. * @returns `true` if the value is a primitive, otherwise `false`. */ function isNormalPrimitive(value) { return (value == null || ['string', 'number', 'boolean', 'undefined'].includes(typeof value)); } /** * * Type guard to check if a value is a non-empty string. * @param value - The value to check. * @returns `true` if the value is a non-empty string, otherwise `false`. */ function isNonEmptyString(value) { return isString(value) && value?.length > 0; } /** * * Type guard to check if a value is falsy. * @param value - The value to check. * @returns `true` if the value is falsy, otherwise `false`. */ function isFalsy(value) { return !value; } /** * * Type guard to check if a value is truthy. * @param value - The value to check. * @returns `true` if the value is truthy (not null or undefined), otherwise `false`. */ function isTruthy(value) { return Boolean(value); }