@carv/is
Version:
Type checking utilities
273 lines (272 loc) • 10.1 kB
TypeScript
export { isEqual as equal, isType as type, isString as string, isEmptyString as emptyString, isNonEmptyString as nonEmptyString, isNumber as number, isBoolean as boolean, isFinite as finite, isInfinite as infinite, isInteger as integer, isSafeInteger as safeInteger, isNaN as NaN, isFunction as function, isSymbol as symbol, isBigInt as bigint, isPrimitive as primitive, isUndefined as undefined, isDefined as defined, isNull as null, isNil as nil, isObject as object, isPlainObject as plainObject, isArray as array, isEmptyArray as emptyArray, isNonEmptyArray as nonEmptyArray, isDate as date, isValidDate as validDate, isRegExp as regExp, isPromise as promise, isPromiseLike as promiseLike, isNativePromise as nativePromise, isLike as like, isMatch as match, };
export declare function isType(value: unknown, type: 'undefined' | 'object' | 'boolean' | 'number' | 'bigint' | 'string' | 'symbol' | 'function'): boolean;
/**
* Performs a [SameValueZero](https://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) comparison
* between two values to determine if they are equivalent.
*
* **Note** SameValueZero differs from SameValue only in its treatment of `+0` and `-0`.
* For SameValue comparison use `Object.is()`.
*/
export declare function isEqual(value: unknown, other: unknown): boolean;
/**
* Returns `true` if passed `value` is a string.
* @param value to check
* @alias is.string
*/
export declare function isString(value: unknown): value is string;
/**
* Returns `true` if passed `value` is a string with a length of zero.
* @param value to check
* @alias is.emptyString
*/
export declare function isEmptyString(value: unknown): value is '';
/**
* Returns `true` if passed `value` is a string with a length greater than zero.
* @param value to check
* @alias is.nonEmptyString
*/
export declare function isNonEmptyString(value: unknown): value is string;
/**
* Returns `true` if passed `value` is a number excluding `NaN`.
*
* **Note**: This intentionally deviates from typeof behavior to increase user-friendliness of is type checks.
*
* @param value to check
* @alias is.number
*/
export declare function isNumber(value: unknown): value is number;
/**
* Returns `true` if passed `value` is finite.
*
* @param value to check
* @alias is.finite
*/
export declare function isFinite(value: unknown): value is number;
/**
* Returns `true` if passed `value` is infinite (`Infinite` or `+Infinite`).
*
* @param value to check
* @alias is.infinite
*/
export declare function isInfinite(value: unknown): value is number;
/**
* Returns `true` if the `value` passed is an integer, `false` otherwise.
* @param value to check
* @alias is.integer
*/
export declare function isInteger(value: unknown): value is number;
/**
* Returns `true` if the `value` passed is a safe integer.
* @param value to check
* @alias is.safeInteger
*/
export declare function isSafeInteger(value: unknown): value is number;
/**
* Returns a Boolean `value` that indicates whether a value is the reserved value `NaN` (not a
* number).
* @param value to check
* @alias is.NaN
*/
export declare function isNaN(value: unknown): value is number;
/**
* Returns `true` if passed `value` is a boolean.
* @param value to check
* @alias is.boolean
*/
export declare function isBoolean(value: unknown): value is boolean;
/**
* Returns `true` if passed `value` is a function.
* @param value to check
* @alias is.function
*/
export declare function isFunction(value: unknown): value is Function;
/**
* Returns `true` if passed `value` is a symbol.
* @param value to check
* @alias is.symbol
*/
export declare function isSymbol(value: unknown): value is symbol;
/**
* Returns `true` if passed `value` is a bigint.
* @param value to check
* @alias is.bigint
*/
export declare function isBigInt(value: unknown): value is bigint;
/**
* Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
*/
export declare type Primitive = null | undefined | boolean | number | bigint | string | symbol;
/**
* Returns `true` if passed `value` is a {@link Primitive}.
* @param value to check
* @alias is.primitve
*/
export declare function isPrimitive(value: unknown): value is Primitive;
/**
* Returns `true` if passed `value` is not `undefined`.
* @param value to check
* @alias is.defined
*/
export declare function isDefined<T>(value: T): value is Exclude<T, undefined>;
/**
* Returns `true` if passed `value` is `undefined`.
* @param value to check
* @alias is.undefined
*/
export declare function isUndefined(value: unknown): value is undefined;
/**
* Returns `true` if passed `value` is `null`.
* @param value to check
* @alias is.null
*/
export declare function isNull(value: unknown): value is null;
/**
* Returns `true` if value is nullish (`null` or `undefined`).
*
* @param value to check
* @alias is.nil
*/
export declare function isNil(value: unknown): value is null | undefined;
/**
* Return `true` if `value` is an object-like.
*
* A value is object-like if it's not `null` and has a `typeof` result of `"object"`.
*
* **Note** Keep in mind that functions are objects too.
*
* @param value to check
* @alias is.object
*/
export declare function isObject(value: unknown): value is object;
/**
* Return `true` if `value` is a plain object (an object created by `{}`, the `Object` constructor or one with a `[[Prototype]]` of `null`).
*
* @param value to check
* @alias is.plainObject
*/
export declare function isPlainObject(value: unknown): value is object;
/**
* Returns `true` if `value` is an array and all of its items match the `assertion` (if provided).
*
* ```js
* is.array(value); // Validate `value` is an array.
* is.array(value, is.number); // Validate `value` is an array and all of its items are numbers.
* ```
*
* @typeparam T item type
* @param value to check
* @param assertion to apply to every element
* @alias is.array
*/
export declare function isArray<T = any>(value: unknown, assertion?: (value: unknown) => value is T): value is T[];
/**
* Returns `true` if `value` is an array with a length of zero.
*
* @param value to check
* @alias is.emptyArray
*/
export declare function isEmptyArray(value: unknown): value is never[];
/**
* Returns `true` if `value` is an array with a length greater than zero and all of its items match the `assertion` (if provided).
*
* @typeparam T item type
* @param value to check
* @param assertion to apply to every element
* @alias is.nonEmptyArray
*/
export declare function isNonEmptyArray<T = any>(value: unknown, assertion?: (value: unknown) => value is T): value is T[];
/**
* Returns `true` if `value` is a Date.
*
* @param value to check
* @alias is.date
*/
export declare function isDate(value: unknown): value is Date;
/**
* Returns `true` if `value` is a valid Date.
*
* @param value to check
* @alias is.validDate
*/
export declare function isValidDate(value: unknown): value is Date;
/**
* Returns `true` if `value` is a RegExp.
*
* @param value to check
* @alias is.regExp
*/
export declare function isRegExp(value: unknown): value is RegExp;
/**
* Returns `true` for any object with a `.then()` and `.catch()` method.
*
* Prefer this one over `.nativePromise()` as you usually want to allow userland promise implementations too.
*
* @typeparam T promise value type
* @param value to check
* @alias is.promise
*/
export declare function isPromise<T = any>(value: unknown): value is Promise<T>;
/**
* Returns `true` for any object with a `.then()` method.
*
* @typeparam T promise value type
* @param value to check
* @alias is.promiseLike
*/
export declare function isPromiseLike<T = any>(value: unknown): value is PromiseLike<T>;
/**
* Returns `true` for a native promise.
*
* @typeparam T promise value type
* @param value to check
* @alias is.nativePromise
*/
export declare function isNativePromise<T = any>(value: unknown): value is Promise<T>;
export declare type Predicate = ((this: undefined, value: any, key: undefined, object: any, matcher: undefined) => unknown) | (<T extends Predicates>(this: T, value: any, key: string, object: any, matcher: T) => unknown);
/**
* Defines the predicate properties to be invoked with the corresponding property values of a given object.
*/
export interface Predicates extends Record<string | number | symbol, Matcher> {
}
export declare type Matcher = Predicate | Predicates | unknown;
export declare type MatchPredicate = (value: unknown) => boolean;
/**
* Creates a function that invokes the predicate properties of `matcher` with the corresponding property values of a given object,
* returning `true` if all predicates return truthy, else `false`.
*
* ```js
* [
* { 'a': 2, 'b': 1 },
* { 'a': 1, 'b': 2 }
* ].filter(is.like({ 'b': (n) => n > 1 }))
* // => [{ 'a': 1, 'b': 2 }]
* ```
*
* **Note**: The created function is equivalent to {@link isMatch} with `source` partially applied.
*
* @param matcher The object of property predicates to conform to.
* @alias is.like
*/
export declare function isLike(matcher: Matcher): MatchPredicate;
/**
* Checks if `value` conforms to `matcher` by invoking the predicate properties
* of `matcher` with the corresponding property values of `value`.
*
* ```js
* var object = { 'a': 1, 'b': 2 };
*
* isMatch(object, { 'b': (n) => n > 1 });
* // => true
*
* isMatch(object, { 'b': (n) => n > 2 });
* // => false
* ```
*
* **Note**: This method is equivalent to {@link isLike} when `matcher` is partially applied.
*
* @param value to inspect.
* @param matcher to conform to.
* @returns Returns `true` if `value` matches, else `false`.
* @alias is.match
*/
export declare function isMatch(value: unknown, matcher: Matcher): boolean;