nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.
122 lines (121 loc) • 3.8 kB
JavaScript
/**
* * 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`.
*/
export 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`.
*/
export 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`.
*/
export 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`.
*/
export 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`.
*/
export 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`.
*/
export 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`.
*/
export 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`.
*/
export 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`.
*/
export 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`.
*/
export 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`.
*/
export 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`.
*/
export 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`.
*/
export 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`.
*/
export function isTruthy(value) {
return Boolean(value);
}