whether-is
Version:
A comprehensive, extensible, and type-friendly type and value checking utility for JavaScript/TypeScript.
362 lines (359 loc) • 11.9 kB
TypeScript
declare class UntypedWhether extends Function {
constructor();
/**
* Same as `Object.is`
*/
is(a: any, b: any): boolean;
/**
* Deepl equal
* @param a
* @param b
*/
equal(a: any, b: any): boolean;
/**
* Check if the target is **not** one of `undefined`, `null`, `+0`, `-0`, `NaN`, `0`, or `''`
* @param o target
*/
isTruthy(o: any): boolean;
/**
* Check if the target is one of `undefined`, `null`, `+0`, `-0`, `NaN`, `0`, or `''`
* @param o target
*/
isFalsy(o: any): boolean;
/**
* Returns `true` if the target is falsy or `{}`, `[]`
* @param o target
*/
isEmpty(o: any): boolean;
/**
* Check if the target has no keys
* - return `null` if the target is not an object
*/
isEmptyObject(o: any): boolean | null;
/**
* Check if the target is an empty array
* - return `null` if the target is not an object
*/
isEmptyArray(o: any): boolean | null;
/**
* Tell whether the target is negative zero
* @param o target
*/
isNegativeZero(o: any): boolean;
/**
* Tell whether the target is positive zero
* @param o target
*/
isPositiveZero(o: any): boolean;
/**
* Check if the constructor of the target has the similar properties as the given constructor
* - Will check all property keys in the proto chain, and compare their `typeof o[key]`
* @param protoClass
* @param o
* @returns
*/
likeInstanceOf<T extends Class>(protoClass: T, o: any): o is InstanceType<T>;
/**
* Tell whether the target is like an `Error` instance
* - If the target is an instanceof `Error`, returns true immediately.
* - Otherwise, checks if all property keys in the prototype chain have the same `typeof`.
* @param o target
*/
likeError(o: any): o is Error;
/**
* Tell whether the target is like a `Date` instance
* - If the target is an instanceof `Date`, returns true immediately.
* - Otherwise, checks if all property keys in the prototype chain have the same `typeof`.
* @param o target
*/
likeDate(o: any): o is Date;
/**
* Tell whether the target is like a `Promise`
* - If the target is an instanceof `Promise`, returns true immediately.
* - Otherwise, checks if all property keys in the prototype chain have the same `typeof`.
* @param o target
*/
likePromise<T = any>(o: any): o is Promise<T>;
/**
* Tell whether the target is like a `Set`
* - If the target is an instanceof `Set`, returns true immediately.
* - Otherwise, checks if all property keys in the prototype chain have the same `typeof`.
* @param o target
*/
likeSet<T = any>(o: any): o is Set<T>;
/**
* Tell whether the target is like a `Map`
* - If the target is an instanceof `Map`, returns true immediately.
* - Otherwise, checks if all property keys in the prototype chain have the same `typeof`.
* @param o target
*/
likeMap<K = any, V = any>(o: any): o is Map<K, V>;
/**
* Tell whether the target is like a `WeakSet`
* - If the target is an instanceof `WeakSet`, returns true immediately.
* - Otherwise, checks if all property keys in the prototype chain have the same `typeof`.
* - `likeWeakSet(Set)` is **true !**
* @param o target
*/
likeWeakSet<T extends WeakKey>(o: any): o is WeakSet<T>;
/**
* Tell whether the target is like a `WeakMap`
* - If the target is an instanceof `WeakMap`, returns true immediately.
* - Otherwise, checks if all property keys in the prototype chain have the same `typeof`.
* - `likeWeakMap(Map)` is **true !**
* @param o target
*/
likeWeakMap<K extends WeakKey, V = any>(o: any): o is WeakMap<K, V>;
/**
* Tell whether the target is iterable
* @param o target
*/
isIterable(o: any): o is Iterable<any>;
/**
* Tell whether the target is a plain object (created by {} or new Object)
* @param o target
*/
isPlainObject(o: any): o is object;
/**
* Tell whether the target is like a RegExp instance
* - If the target is an instanceof RegExp, returns true immediately.
* - Otherwise, checks if all property keys in the prototype chain have the same typeof.
* @param o target
*/
likeRegExp(o: any): o is RegExp;
/**
* Tell whether the target is Promise-like
*
* Checks if the target is an object or function and has a 'then' method which is a function.
* @param o target
*/
isPromiseLike<T = any>(o: any): o is PromiseLike<T>;
/**
* Tell whether the target is a non-null object
* @param o target
*/
isObject<T extends object>(o: any): o is T;
/**
* Tell whether the target is a non-null object or a function
* @param o target
*/
likeObject<T extends object | Func>(o: unknown): o is T;
/**
* Tell whether the target is a function
* @param o target
*/
isFunction(o: any): o is Func;
/**
* Tell whether the target is a string
* @param o target
*/
isString(o: any): o is string;
/**
* Tell whether the target is a number
* @param o target
*/
isNumber(o: any): o is number;
/**
* Tell whether the target is a boolean
* @param o target
*/
isBoolean(o: any): o is boolean;
/**
* Tell whether the target is undefined
* @param o target
*/
isUndefined(o: any): o is undefined;
/**
* Tell whether the target is null
* @param o target
*/
isNull(o: any): o is null;
/**
* Tell whether the target is a symbol
* @param o target
*/
isSymbol(o: any): o is symbol;
/**
* Tell whether the target is a bigint
* @param o target
*/
isBigInt(o: any): o is bigint;
/**
* Tell whether the target is null or undefined
* @param o target
*/
isNullish(o: any): o is null | undefined;
/**
* Tell whether the target is a primitive value
* @param o target
*/
isPrimitive(o: any): o is string | number | boolean | null | undefined | symbol | bigint;
/**
* Tell whether the target is a field key (string or symbol)
* @param o target
*/
isField(o: unknown): o is string | symbol;
/**
* Tell whether the target is a property key (string, symbol or number)
* @param o target
*/
isPropertyKey(o: unknown): o is string | symbol | number;
/**
* Tell whether the target is NaN
* - **if target is not a number, it will return `null`**
* - if target is a number, it will return `true` if it is `NaN`
* @param o target
*/
isNaN(o: any): boolean | null;
/**
* Tell whether the target is an integer
* @param o target
*/
isInteger(o: any): boolean;
/**
* Tell whether the target is a safe integer
* @param o target
*/
isSafeInteger(o: any): boolean;
/**
* Tell whether the target is a safe number
* @param o target
*/
isSafeNumber(o: any): boolean;
isFinite(o: any): boolean;
/**
* Tell whether the target is a class constructor
* - use proxy to check if the target is constructable
* - then see if `target.toString()` starts with `class` or `[class`(sometimes in node)
* - cannot tell after `.bind()` or Proxied the apply behavior of a class
* @see For precise results, see https://github.com/baendlorel/get-function-features
* @param o target
*/
isClass(o: any): o is Class;
/**
* Asserts that `arr` is an array.
* - If `predicate` is provided, it will be called for each element in the array.
* - If it returns a string, it will throw an error with that message.
* - If it returns `null` or `undefined`, the element is considered valid.
* - If it returns `boolean` and value is `true`, the element is considered valid.
* @param o target array
* @param predicate function to validate each element
* @param msg
*/
isArray<T = any>(o: any, predicate?: (value?: T, index?: number, array?: T[]) => string | boolean): o is T[];
/**
* Tell whether the target is like `(...) => any`
* - Might be not so accurate under some extreme circumstances like bound functions or proxied functions, etc.
* - So we just don't allow such cases.
* @see For precise results, see https://github.com/baendlorel/get-function-features
*/
isArrowFunction(o: any): o is Func;
/**
* (Optional) Tell whether the target is negative zero
* @param o target
*/
orNegativeZero(o: any): boolean;
/**
* (Optional) Tell whether the target is positive zero
* @param o target
*/
orPositiveZero(o: any): boolean;
/**
* (Optional) Tell whether the target is a function
* @param o target
*/
orFunction(o: any): o is Func | undefined;
/**
* (Optional) Tell whether the target is a non-null object
* @param o target
*/
orObject<T extends object>(o: any): o is T | undefined;
/**
* (Optional) Tell whether the target is a non-null object or a function
* @param o target
*/
orLikeObject(o: unknown): o is object | Func | undefined;
/**
* (Optional) Tell whether the target is a string
* @param o target
*/
orString(o: any): o is string | undefined;
/**
* (Optional) Tell whether the target is a number
* @param o target
*/
orNumber(o: any): o is number | undefined;
/**
* (Optional) Tell whether the target is a boolean
* @param o target
*/
orBoolean(o: any): o is boolean | undefined;
/**
* (Optional) Tell whether the target is undefined
* @param o target
*/
orUndefined(o: any): o is undefined;
/**
* (Optional) Tell whether the target is null
* @param o target
*/
orNull(o: any): o is null | undefined;
/**
* (Optional) Tell whether the target is a symbol
* @param o target
*/
orSymbol(o: any): o is symbol | undefined;
/**
* (Optional) Tell whether the target is a bigint
* @param o target
*/
orBigInt(o: any): o is bigint | undefined;
/**
* (Optional) Tell whether the target is a field key (string or symbol)
* @param o target
*/
orField(o: unknown): o is string | symbol | undefined;
/**
* (Optional) Tell whether the target is a property key (string, symbol or number)
* @param o target
*/
orPropertyKey(o: unknown): o is string | symbol | number | undefined;
/**
* (Optional) Tell whether the target is NaN
* @param o target
*/
orNaN(o: any): boolean | null;
/**
* (Optional) Tell whether the target is an integer
* @param o target
*/
orInteger(o: any): boolean;
/**
* (Optional) Tell whether the target is a safe integer
* @param o target
*/
orSafeInteger(o: any): boolean;
/**
* (Optional) Tell whether the target is a safe number
* @param o target
*/
orSafeNumber(o: any): boolean;
/**
* (Optional) Tell whether the target is a class constructor
* @param o target
*/
orClass(o: any): o is Class | undefined;
/**
* (Optional) Tell whether the target is an array
* @param o target array
* @param predicate function to validate each element
*/
orArray<T = any>(o: any, predicate?: (value?: T, index?: number, array?: T[]) => string | boolean): o is T[] | undefined;
}
type Whether = (<T = any>(o: unknown) => o is T) & UntypedWhether;
/**
* Expect the target to be truthy.
* @param target
*/
declare const whether: Whether;
export { whether };