UNPKG

@carv/is

Version:

Type checking utilities

464 lines (421 loc) 11.3 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function isType(value, type) { return typeof value === type; } /** * 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()`. */ function isEqual(value, other) { return value === other || isNaN(value) && isNaN(other); } /** * Returns `true` if passed `value` is a string. * @param value to check * @alias is.string */ function isString(value) { return isType(value, 'string'); } /** * Returns `true` if passed `value` is a string with a length of zero. * @param value to check * @alias is.emptyString */ function isEmptyString(value) { return value === ''; } /** * Returns `true` if passed `value` is a string with a length greater than zero. * @param value to check * @alias is.nonEmptyString */ function isNonEmptyString(value) { return isString(value) && value.length > 0; } /** * 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 */ function isNumber(value) { return isType(value, 'number') && !isNaN(value); } /** * Returns `true` if passed `value` is finite. * * @param value to check * @alias is.finite */ function isFinite(value) { return Number.isFinite(value); } /** * Returns `true` if passed `value` is infinite (`Infinite` or `+Infinite`). * * @param value to check * @alias is.infinite */ function isInfinite(value) { return value === Infinity || value === -Infinity; } /** * Returns `true` if the `value` passed is an integer, `false` otherwise. * @param value to check * @alias is.integer */ function isInteger(value) { return Number.isInteger(value); } /** * Returns `true` if the `value` passed is a safe integer. * @param value to check * @alias is.safeInteger */ function isSafeInteger(value) { return Number.isSafeInteger(value); } /** * Returns a Boolean `value` that indicates whether a value is the reserved value `NaN` (not a * number). * @param value to check * @alias is.NaN */ function isNaN(value) { return Number.isNaN(value); } /** * Returns `true` if passed `value` is a boolean. * @param value to check * @alias is.boolean */ function isBoolean(value) { return isType(value, 'boolean'); } /** * Returns `true` if passed `value` is a function. * @param value to check * @alias is.function */ function isFunction(value) { return isType(value, 'function'); } /** * Returns `true` if passed `value` is a symbol. * @param value to check * @alias is.symbol */ function isSymbol(value) { return isType(value, 'symbol') || value instanceof Symbol; } /** * Returns `true` if passed `value` is a bigint. * @param value to check * @alias is.bigint */ function isBigInt(value) { return isType(value, 'bigint'); } /** * Returns `true` if passed `value` is a {@link Primitive}. * @param value to check * @alias is.primitve */ function isPrimitive(value) { return isNil(value) || /^[sbn]/.test(typeof value); } /** * Returns `true` if passed `value` is not `undefined`. * @param value to check * @alias is.defined */ function isDefined(value) { return value !== undefined; } /** * Returns `true` if passed `value` is `undefined`. * @param value to check * @alias is.undefined */ function isUndefined(value) { return value === undefined; } /** * Returns `true` if passed `value` is `null`. * @param value to check * @alias is.null */ function isNull(value) { return value === null; } /** * Returns `true` if value is nullish (`null` or `undefined`). * * @param value to check * @alias is.nil */ function isNil(value) { return value == null; } /** * 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 */ function isObject(value) { return !isNil(value) && isType(value, '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 */ function isPlainObject(value) { if (!isObject(value)) { return false; } var prototype = Object.getPrototypeOf(value); return prototype === null || prototype === Object.getPrototypeOf({}); } /** * 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 */ function isArray(value, assertion) { if (!Array.isArray(value)) { return false; } if (!isFunction(assertion)) { return true; } return value.every(assertion); } /** * Returns `true` if `value` is an array with a length of zero. * * @param value to check * @alias is.emptyArray */ function isEmptyArray(value) { return isArray(value) && value.length === 0; } /** * 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 */ function isNonEmptyArray(value, assertion) { return isArray(value, assertion) && value.length > 0; } /** * Returns `true` if `value` is a Date. * * @param value to check * @alias is.date */ function isDate(value) { return value instanceof Date; } /** * Returns `true` if `value` is a valid Date. * * @param value to check * @alias is.validDate */ function isValidDate(value) { // If the date object is invalid it will return 'NaN' on getTime() return isDate(value) && !isNaN(value.getTime()); } /** * Returns `true` if `value` is a RegExp. * * @param value to check * @alias is.regExp */ function isRegExp(value) { return value instanceof 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 */ function isPromise(value) { return isPromiseLike(value) && isFunction(value["catch"]); } /** * Returns `true` for any object with a `.then()` method. * * @typeparam T promise value type * @param value to check * @alias is.promiseLike */ function isPromiseLike(value) { return !isNil(value) && isFunction(value.then); } /** * Returns `true` for a native promise. * * @typeparam T promise value type * @param value to check * @alias is.nativePromise */ function isNativePromise(value) { return value instanceof Promise; } /** * 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 */ function isLike(matcher) { return function (value) { return match(value, matcher); }; } /** * 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 */ function isMatch(value, matcher) { return match(value, matcher); } function match(value, predicate, key, object, matcher) { if (isEqual(value, predicate)) { return true; } if (isFunction(predicate)) { return Boolean(predicate.call(matcher, value, key, object, matcher)); } if (isObject(value) && isObject(predicate)) { return Object.keys(predicate).every(function (key) { return match(value[key], predicate[key], key, value, predicate); }); } return false; } exports.NaN = isNaN; exports.array = isArray; exports.bigint = isBigInt; exports.boolean = isBoolean; exports.date = isDate; exports.defined = isDefined; exports.emptyArray = isEmptyArray; exports.emptyString = isEmptyString; exports.equal = isEqual; exports.finite = isFinite; exports.function = isFunction; exports.infinite = isInfinite; exports.integer = isInteger; exports.isArray = isArray; exports.isBigInt = isBigInt; exports.isBoolean = isBoolean; exports.isDate = isDate; exports.isDefined = isDefined; exports.isEmptyArray = isEmptyArray; exports.isEmptyString = isEmptyString; exports.isEqual = isEqual; exports.isFinite = isFinite; exports.isFunction = isFunction; exports.isInfinite = isInfinite; exports.isInteger = isInteger; exports.isLike = isLike; exports.isMatch = isMatch; exports.isNaN = isNaN; exports.isNativePromise = isNativePromise; exports.isNil = isNil; exports.isNonEmptyArray = isNonEmptyArray; exports.isNonEmptyString = isNonEmptyString; exports.isNull = isNull; exports.isNumber = isNumber; exports.isObject = isObject; exports.isPlainObject = isPlainObject; exports.isPrimitive = isPrimitive; exports.isPromise = isPromise; exports.isPromiseLike = isPromiseLike; exports.isRegExp = isRegExp; exports.isSafeInteger = isSafeInteger; exports.isString = isString; exports.isSymbol = isSymbol; exports.isType = isType; exports.isUndefined = isUndefined; exports.isValidDate = isValidDate; exports.like = isLike; exports.match = isMatch; exports.nativePromise = isNativePromise; exports.nil = isNil; exports.nonEmptyArray = isNonEmptyArray; exports.nonEmptyString = isNonEmptyString; exports.null = isNull; exports.number = isNumber; exports.object = isObject; exports.plainObject = isPlainObject; exports.primitive = isPrimitive; exports.promise = isPromise; exports.promiseLike = isPromiseLike; exports.regExp = isRegExp; exports.safeInteger = isSafeInteger; exports.string = isString; exports.symbol = isSymbol; exports.type = isType; exports.undefined = isUndefined; exports.validDate = isValidDate; //# sourceMappingURL=is.cjs.development.js.map