@moyal/js-type
Version:
Minimal, robust type detection and value classification utility for JavaScript.
253 lines (252 loc) • 10.1 kB
TypeScript
/**
* Returns the semantic version of this library.
* @returns {string} - The semantic version of this library.
*/
export function version(): string;
/**
* Returns the type name of a given value.
* - For primitives: returns `"string"`, `"number"`, etc.
* - For objects: returns class name or `"object"` if plain.
* - For built-in types like Date, RegExp, Map: returns the constructor name.
* - For plain objects: returns "object". For arrays: "array".
* - For classes: returns `"class"` for user defined classes.
* - For functions: returns `"function"`
*
* @param {*} value - The value whose type name to determine.
* @returns {string} The detected type name.
*/
export function getTypeName(value: any): string;
/**
* Checks if the specified value matches the given type name.
* @param {*} value - The value to test.
* @param {string} typeName - The expected type name.
* @returns {boolean} `true` if the value’s type matches the given name.
*/
export function isType(value: any, typeName: string): boolean;
/**
* Checks if the specified value is a string (primitive or String object).
* @param {*} value - The value to test.
* @returns {boolean} `true` if the specified value is a string.
*/
export function isString(value: any): boolean;
/**
* Checks if the specified value is a symbol.
* @param {*} value - The value to test.
* @returns {boolean} `true` if the specified value is a symbol.
*/
export function isSymbol(value: any): boolean;
/**
* Checks whether the specified value is a number (primitive or Number object).
* Excludes NaN and Infinity by default unless explicitly allowed.
*
* @param {*} value - The value to test.
* @param {Object} [options]
* @param {boolean} [options.allowNaN=true] - Whether to consider NaN a valid number.
* @param {boolean} [options.allowInfinity=true] - Whether to consider Infinity/-Infinity valid.
* @returns {boolean} - `true` if the specified value is a number.
*/
export function isNumber(value: any, { allowNaN, allowInfinity }?: {
allowNaN?: boolean | undefined;
allowInfinity?: boolean | undefined;
}): boolean;
/**
* Checks if the specified value is a bigint (primitive or BigInt object).
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is big integer.
*/
export function isBigInt(value: any): boolean;
/**
* Checks if the value is a numeric type (either number or bigint).
* @param {*} value
* @returns {boolean} - `true` if the value is a numeric type (either number or bigint).
*/
export function isNumeric(value: any): boolean;
/**
* Checks if the specified value is a boolean (primitive or Boolean object).
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is boolean.
*/
export function isBoolean(value: any): boolean;
/**
* Checks if the specified value is undefined.
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is undefined.
*/
export function isUndefined(value: any): boolean;
/**
* Checks if the specified value is null.
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is null.
*/
export function isNull(value: any): boolean;
/**
* Checks if the specified value is a standard function.
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is Function.
*/
export function isFunction(value: any): boolean;
/**
* Checks if the specified value is a user defined class.
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is user defined class.
*/
export function isUserDefinedClass(value: any): boolean;
/**
* Checks if the specified value is a plain object (i.e., created via `{}` or `new Object()`).
* This works reliably across realms.
*
* @param {*} value - The value to check.
* @returns {boolean} `true` if it's a plain object.
*/
export function isPlainObject(value: any): boolean;
/**
* Checks if the specified value is a non-wrapper object (i.e., not a function, not null, not a boxed primitive).
* Includes objects like arrays, plain objects, Date, RegExp, etc.
*
* @param {*} value - The value to check.
* @returns {boolean} `true` if it's a real object, excluding primitive wrappers and functions.
*/
export function isObject(value: any): boolean;
/**
* Checks if the specified value is instance of Date.
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is instance of Date.
*/
export function isDate(value: any): boolean;
/**
* Checks if the specified value is an array.
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is an array.
*/
export function isArray(value: any): boolean;
/**
* Checks if the specified value is of Error type.
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is of Error type.
*/
export function isError(value: any): boolean;
/**
* Determines whether the specified value is one of the primitive types: string, number, bigint, boolean and symbol (including wrapper type over primitive)
* This function treats primitives any of the following: string, number, boolean, symbol, bigint and their wrapper String, Number, Boolean.
* @param {*} value
* @returns {boolean} - true if the specified value is one of the primitive types: string, number, bigint, boolean and symbol (including wrapper type over primitive)
*/
export function isPrimitive(value: any): boolean;
/**
* Checks if the value is an integer (number or bigint), optionally applying a custom predicate.
* @param {*} value - The value to test.
* @param {function(number):boolean} [additionalPredicate = null] - Additional test to apply to the value.
* @returns {boolean} - `true` if the value is integral number and passed the test against the additional predicate (if it was specified) .
*/
export function isIntegral(value: any, additionalPredicate?: (arg0: number) => boolean): boolean;
/**
* Checks if the specified value is iterable (has a `[Symbol.iterator]` method).
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is iterable (has a `[Symbol.iterator]` method).
*/
export function isIterable(value: any): boolean;
/**
* Checks if the specified value is a function or a generator function.
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is a function or a generator function.
*/
export function isFunctionOrGeneratorFunction(value: any): boolean;
/**
* Checks if the specified value is a generator function.
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is a generator function.
*/
export function isGeneratorFunction(value: any): boolean;
/**
* Checks if the specified value is an asynchronous function.
* @param {*} value - The value to test.
* @returns {boolean} - `true` if the specified value is an asynchronous function.
*/
export function isAsyncFunction(value: any): boolean;
/**
* Parses common values like string "true" → boolean true, "42.0" → number, etc.
* @param {*} value
* @returns {InferDataTypeResult}
*/
export function inferDataType(value: any): InferDataTypeResult;
/**
* Parses a string to boolean. If the input is already boolean, returns it.
* If it's "true" or "false" (case insensitive), converts accordingly.
* @param {*} value - The value to parse.
* @returns {boolean|undefined} - 'true' if the specified value is a string trimmed to literal 'true' (case insensitive) or `false` if it is trimmed to literal 'false' (case insensitive).
*/
export function parseBool(value: any): boolean | undefined;
/**
* Checks if the specified value is a RegExp.
* @param {*} value - The value to test.
* @returns {boolean} `true` if the value is a RegExp.
*/
export function isRegExp(value: any): boolean;
/**
* Checks if the specified value is a Map.
* @param {*} value - The value to test.
* @returns {boolean} `true` if the value is a Map.
*/
export function isMap(value: any): boolean;
/**
* Checks if the specified value is a Set.
* @param {*} value - The value to test.
* @returns {boolean} `true` if the value is a Set.
*/
export function isSet(value: any): boolean;
/**
* Checks if the specified value is a WeakMap.
* @param {*} value - The value to test.
* @returns {boolean} `true` if the value is a WeakMap.
*/
export function isWeakMap(value: any): boolean;
/**
* Checks if the specified value is a WeakSet.
* @param {*} value - The value to test.
* @returns {boolean} `true` if the value is a WeakSet.
*/
export function isWeakSet(value: any): boolean;
/**
* Checks if the specified value is a Promise.
* @param {*} value - The value to test.
* @returns {boolean} `true` if the value is a Promise.
*/
export function isPromise(value: any): boolean;
/**
* Checks if the specified value is "empty".
* The following are considered empty:
* - null, undefined
* - empty string: ""
* - empty array: []
* - empty object: {}
* - empty Map or Set
* - Note: The implementation avoids treating numbers or booleans as "empty". Only typical "container-like" values and nullish types are evaluated.
*
* @param {*} value - The value to test.
* @returns {boolean} `true` if the value is considered empty.
*/
export function isEmpty(value: any): boolean;
/**
* Checks if the specified value is not empty.
* See `isEmpty` for full empty definition.
* @param {*} value - The value to test.
* @returns {boolean} `true` if the value is not considered empty.
*/
export function isNotEmpty(value: any): boolean;
/**
* Represents the result of inferred data type conversion.
*/
export class InferDataTypeResult {
/**
* @param {*} originalValue
* @param {*} parsedValue
* @param {string|null} type
*/
constructor(originalValue: any, parsedValue: any, type: string | null);
/** @type {*} */
originalValue: any;
/** @type {*} */
parsedValue: any;
/** @type {string|null} */
type: string | null;
}