- Spec files
npm-lodash
Describes: pkg:npm/lodash@4.5.x
- Description
- Comprehensive JavaScript utility library with 300+ methods for arrays, objects, strings, functions, and more.
- Author
- tessl
- Last updated
type-checking.md docs/
1# Type Checking23Comprehensive type checking utilities for determining data types and validating values. These methods provide reliable type detection and comparison operations for JavaScript values.45## Capabilities67### Basic Type Checking89#### Primitive Types10Check for JavaScript primitive types.1112```javascript { .api }13/**14* Checks if value is classified as a boolean primitive or object15* @param value - The value to check16* @returns Returns true if value is a boolean, else false17*/18function isBoolean(value);1920/**21* Checks if value is classified as a Number primitive or object22* @param value - The value to check23* @returns Returns true if value is a number, else false24*/25function isNumber(value);2627/**28* Checks if value is classified as a String primitive or object29* @param value - The value to check30* @returns Returns true if value is a string, else false31*/32function isString(value);3334/**35* Checks if value is classified as a Symbol primitive or object36* @param value - The value to check37* @returns Returns true if value is a symbol, else false38*/39function isSymbol(value);40```4142#### Null & Undefined43Check for null, undefined, and nil values.4445```javascript { .api }46/**47* Checks if value is null48* @param value - The value to check49* @returns Returns true if value is null, else false50*/51function isNull(value);5253/**54* Checks if value is undefined55* @param value - The value to check56* @returns Returns true if value is undefined, else false57*/58function isUndefined(value);5960/**61* Checks if value is null or undefined62* @param value - The value to check63* @returns Returns true if value is nullish, else false64*/65function isNil(value);66```6768### Object Type Checking6970#### Object Detection71Check for various object types.7273```javascript { .api }74/**75* Checks if value is the language type of Object76* @param value - The value to check77* @returns Returns true if value is an object, else false78*/79function isObject(value);8081/**82* Checks if value is object-like (i.e. not null and typeof result of "object")83* @param value - The value to check84* @returns Returns true if value is object-like, else false85*/86function isObjectLike(value);8788/**89* Checks if value is a plain object, that is, an object created by the Object constructor or one with a [[Prototype]] of null90* @param value - The value to check91* @returns Returns true if value is a plain object, else false92*/93function isPlainObject(value);94```9596#### Array & Array-like97Check for arrays and array-like objects.9899```javascript { .api }100/**101* Checks if value is classified as an Array object102* @param value - The value to check103* @returns Returns true if value is an array, else false104*/105function isArray(value);106107/**108* Checks if value is array-like109* @param value - The value to check110* @returns Returns true if value is array-like, else false111*/112function isArrayLike(value);113114/**115* Checks if value is array-like and not a function or constructor116* @param value - The value to check117* @returns Returns true if value is an array-like object, else false118*/119function isArrayLikeObject(value);120```121122#### Function Detection123Check for various function types.124125```javascript { .api }126/**127* Checks if value is classified as a Function object128* @param value - The value to check129* @returns Returns true if value is a function, else false130*/131function isFunction(value);132133/**134* Checks if value is a pristine native function135* @param value - The value to check136* @returns Returns true if value is a native function, else false137*/138function isNative(value);139```140141### Collection Type Checking142143#### Built-in Collections144Check for built-in collection types.145146```javascript { .api }147/**148* Checks if value is classified as a Map object149* @param value - The value to check150* @returns Returns true if value is a map, else false151*/152function isMap(value);153154/**155* Checks if value is classified as a Set object156* @param value - The value to check157* @returns Returns true if value is a set, else false158*/159function isSet(value);160161/**162* Checks if value is classified as a WeakMap object163* @param value - The value to check164* @returns Returns true if value is a weak map, else false165*/166function isWeakMap(value);167168/**169* Checks if value is classified as a WeakSet object170* @param value - The value to check171* @returns Returns true if value is a weak set, else false172*/173function isWeakSet(value);174```175176#### Typed Arrays177Check for typed array types.178179```javascript { .api }180/**181* Checks if value is classified as a typed array182* @param value - The value to check183* @returns Returns true if value is a typed array, else false184*/185function isTypedArray(value);186187/**188* Checks if value is classified as an ArrayBuffer object189* @param value - The value to check190* @returns Returns true if value is an array buffer, else false191*/192function isArrayBuffer(value);193```194195### Special Object Types196197#### Date & RegExp198Check for date and regular expression objects.199200```javascript { .api }201/**202* Checks if value is classified as a Date object203* @param value - The value to check204* @returns Returns true if value is a date object, else false205*/206function isDate(value);207208/**209* Checks if value is classified as a RegExp object210* @param value - The value to check211* @returns Returns true if value is a regexp, else false212*/213function isRegExp(value);214```215216#### Error Objects217Check for error objects.218219```javascript { .api }220/**221* Checks if value is an Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, or URIError object222* @param value - The value to check223* @returns Returns true if value is an error object, else false224*/225function isError(value);226```227228#### DOM Elements229Check for DOM elements.230231```javascript { .api }232/**233* Checks if value is likely a DOM element234* @param value - The value to check235* @returns Returns true if value is a DOM element, else false236*/237function isElement(value);238```239240#### Arguments Object241Check for arguments objects.242243```javascript { .api }244/**245* Checks if value is likely an arguments object246* @param value - The value to check247* @returns Returns true if value is an arguments object, else false248*/249function isArguments(value);250```251252#### Buffer253Check for buffer objects.254255```javascript { .api }256/**257* Checks if value is a buffer258* @param value - The value to check259* @returns Returns true if value is a buffer, else false260*/261function isBuffer(value);262```263264### Number Type Checking265266#### Number Validation267Check for various number conditions.268269```javascript { .api }270/**271* Checks if value is a finite primitive number272* @param value - The value to check273* @returns Returns true if value is a finite number, else false274*/275function isFinite(value);276277/**278* Checks if value is an integer279* @param value - The value to check280* @returns Returns true if value is an integer, else false281*/282function isInteger(value);283284/**285* Checks if value is a safe integer286* @param value - The value to check287* @returns Returns true if value is a safe integer, else false288*/289function isSafeInteger(value);290291/**292* Checks if value is NaN293* @param value - The value to check294* @returns Returns true if value is NaN, else false295*/296function isNaN(value);297```298299#### Length Validation300Check for valid length values.301302```javascript { .api }303/**304* Checks if value is a valid array-like length305* @param value - The value to check306* @returns Returns true if value is a valid length, else false307*/308function isLength(value);309```310311### Value Testing312313#### Emptiness314Check if values are empty.315316```javascript { .api }317/**318* Checks if value is an empty object, collection, map, or set319* @param value - The value to check320* @returns Returns true if value is empty, else false321*/322function isEmpty(value);323```324325### Value Comparison326327#### Equality328Perform equality comparisons.329330```javascript { .api }331/**332* Performs a SameValueZero comparison between two values to determine if they are equivalent333* @param value - The value to compare334* @param other - The other value to compare335* @returns Returns true if the values are equivalent, else false336*/337function eq(value, other);338339/**340* Performs a deep comparison between two values to determine if they are equivalent341* @param value - The value to compare342* @param other - The other value to compare343* @returns Returns true if the values are equivalent, else false344*/345function isEqual(value, other);346347/**348* Like isEqual except that it accepts customizer which is invoked to compare values349* @param value - The value to compare350* @param other - The other value to compare351* @param customizer - The function to customize comparisons352* @returns Returns true if the values are equivalent, else false353*/354function isEqualWith(value, other, customizer);355```356357#### Matching358Check if objects match patterns.359360```javascript { .api }361/**362* Performs a partial deep comparison between object and source to determine if object contains equivalent property values363* @param object - The object to inspect364* @param source - The object of property values to match365* @returns Returns true if object is a match, else false366*/367function isMatch(object, source);368369/**370* Like isMatch except that it accepts customizer which is invoked to compare values371* @param object - The object to inspect372* @param source - The object of property values to match373* @param customizer - The function to customize comparisons374* @returns Returns true if object is a match, else false375*/376function isMatchWith(object, source, customizer);377```378379#### Conformance380Check if objects conform to schemas.381382```javascript { .api }383/**384* Checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object385* @param object - The object to inspect386* @param source - The object of property predicates to conform to387* @returns Returns true if object conforms, else false388*/389function conformsTo(object, source);390```391392#### Relational Comparisons393Perform greater than and less than comparisons.394395```javascript { .api }396/**397* Checks if value is greater than other398* @param value - The value to compare399* @param other - The other value to compare400* @returns Returns true if value is greater than other, else false401*/402function gt(value, other);403404/**405* Checks if value is greater than or equal to other406* @param value - The value to compare407* @param other - The other value to compare408* @returns Returns true if value is greater than or equal to other, else false409*/410function gte(value, other);411412/**413* Checks if value is less than other414* @param value - The value to compare415* @param other - The other value to compare416* @returns Returns true if value is less than other, else false417*/418function lt(value, other);419420/**421* Checks if value is less than or equal to other422* @param value - The value to compare423* @param other - The other value to compare424* @returns Returns true if value is less than or equal to other, else false425*/426function lte(value, other);427```428429### Type Conversion430431#### Safe Conversion432Convert values to specific types safely.433434```javascript { .api }435/**436* Converts value to an array437* @param value - The value to convert438* @returns Returns the converted array439*/440function toArray(value);441442/**443* Converts value to a finite number444* @param value - The value to convert445* @returns Returns the converted number446*/447function toFinite(value);448449/**450* Converts value to an integer451* @param value - The value to convert452* @returns Returns the converted integer453*/454function toInteger(value);455456/**457* Converts value to an integer suitable for use as the length of an array-like object458* @param value - The value to convert459* @returns Returns the converted integer460*/461function toLength(value);462463/**464* Converts value to a number465* @param value - The value to convert466* @returns Returns the converted number467*/468function toNumber(value);469470/**471* Converts value to a plain object flattening inherited enumerable string keyed properties of value to own properties of the plain object472* @param value - The value to convert473* @returns Returns the converted plain object474*/475function toPlainObject(value);476477/**478* Converts value to a safe integer479* @param value - The value to convert480* @returns Returns the converted integer481*/482function toSafeInteger(value);483484/**485* Converts value to a string486* @param value - The value to convert487* @returns Returns the converted string488*/489function toString(value);490```491492### Object Cloning493494#### Deep & Shallow Cloning495Create copies of values.496497```javascript { .api }498/**499* Creates a shallow clone of value500* @param value - The value to clone501* @returns Returns the cloned value502*/503function clone(value);504505/**506* Creates a deep clone of value507* @param value - The value to clone508* @returns Returns the cloned value509*/510function cloneDeep(value);511512/**513* Like clone except that it accepts customizer which is invoked to produce the cloned value514* @param value - The value to clone515* @param customizer - The function to customize cloning516* @returns Returns the cloned value517*/518function cloneWith(value, customizer);519520/**521* Like cloneDeep except that it accepts customizer which is invoked to produce the cloned value522* @param value - The value to clone523* @param customizer - The function to customize cloning524* @returns Returns the cloned value525*/526function cloneDeepWith(value, customizer);527```528529### Array Casting530531#### Array Conversion532Convert values to arrays.533534```javascript { .api }535/**536* Casts value as an array if it's not one537* @param value - The value to inspect538* @returns Returns the cast array539*/540function castArray(value);541```542543## Usage Examples544545```javascript546import {547isString, isNumber, isArray, isObject, isFunction,548isEmpty, isEqual, isMatch, gt, clone, cloneDeep,549isNil, isFinite, isInteger, toNumber, toString550} from "lodash";551552// Basic type checking553console.log(isString('hello')); // true554console.log(isNumber(42)); // true555console.log(isArray([1, 2, 3])); // true556console.log(isObject({})); // true557console.log(isFunction(() => {})); // true558559// Null/undefined checking560console.log(isNil(null)); // true561console.log(isNil(undefined)); // true562console.log(isNil(0)); // false563564// Number validation565console.log(isFinite(42)); // true566console.log(isFinite(Infinity)); // false567console.log(isInteger(42.0)); // true568console.log(isInteger(42.5)); // false569570// Emptiness checking571console.log(isEmpty([])); // true572console.log(isEmpty({})); // true573console.log(isEmpty('')); // true574console.log(isEmpty([1])); // false575576// Deep equality577const obj1 = { a: 1, b: { c: 2 } };578const obj2 = { a: 1, b: { c: 2 } };579const obj3 = { a: 1, b: { c: 3 } };580581console.log(obj1 === obj2); // false (reference equality)582console.log(isEqual(obj1, obj2)); // true (deep equality)583console.log(isEqual(obj1, obj3)); // false584585// Pattern matching586const user = { name: 'John', age: 30, active: true };587console.log(isMatch(user, { name: 'John' })); // true588console.log(isMatch(user, { name: 'John', age: 30 })); // true589console.log(isMatch(user, { name: 'Jane' })); // false590591// Relational comparisons592console.log(gt(3, 1)); // true593console.log(gt(1, 3)); // false594595// Type-safe validation function596function validateUser(user) {597const errors = [];598599if (!isObject(user)) {600errors.push('User must be an object');601return errors;602}603604if (!isString(user.name) || isEmpty(user.name)) {605errors.push('Name is required and must be a string');606}607608if (!isNumber(user.age) || !isInteger(user.age) || user.age < 0) {609errors.push('Age must be a positive integer');610}611612if (!isNil(user.email) && !isString(user.email)) {613errors.push('Email must be a string if provided');614}615616return errors;617}618619// Usage620console.log(validateUser({ name: 'John', age: 30 })); // []621console.log(validateUser({ name: '', age: 'thirty' })); // ['Name is required...', 'Age must be...']622623// Safe type conversion624const values = ['42', '3.14', 'hello', null, undefined];625const numbers = values.map(v => {626const num = toNumber(v);627return isFinite(num) ? num : 0;628});629console.log(numbers); // [42, 3.14, 0, 0, 0]630631// Deep cloning for safe mutations632const original = {633name: 'John',634hobbies: ['reading', 'coding'],635profile: { age: 30 }636};637638const copy = cloneDeep(original);639copy.hobbies.push('gaming');640copy.profile.age = 31;641642console.log(original.hobbies); // ['reading', 'coding'] (unchanged)643console.log(copy.hobbies); // ['reading', 'coding', 'gaming']644645// Custom type guards646const isNonEmptyString = (value) => isString(value) && !isEmpty(value);647const isPositiveNumber = (value) => isNumber(value) && isFinite(value) && value > 0;648const isValidEmail = (value) => isString(value) && /\S+@\S+\.\S+/.test(value);649650// API response validation651function validateApiResponse(response) {652if (!isObject(response)) return false;653if (!isArray(response.data)) return false;654if (!isNumber(response.status)) return false;655656return response.data.every(item =>657isObject(item) &&658isNonEmptyString(item.id) &&659isNonEmptyString(item.name)660);661}662663// Runtime type checking utility664function createTypedFunction(paramTypes, returnType, fn) {665return function(...args) {666// Check parameter types667paramTypes.forEach((type, index) => {668if (!type(args[index])) {669throw new TypeError(`Parameter ${index} failed type check`);670}671});672673const result = fn(...args);674675// Check return type676if (!returnType(result)) {677throw new TypeError('Return value failed type check');678}679680return result;681};682}683684// Usage685const safeAdd = createTypedFunction(686[isNumber, isNumber], // parameters must be numbers687isNumber, // return value must be number688(a, b) => a + b689);690691console.log(safeAdd(5, 3)); // 8692// safeAdd('5', 3); // TypeError: Parameter 0 failed type check693```