@megaorm/test v1.2.0
MegaORM Test
This package is designed to assist developers in type checking and validation for various JavaScript values. It provides a collection of type-checking functions, and utilities to validate your values.
Table of Contents
Type Functions
Type functions allow you to validate the type of a value. They return a boolean indicating whether the value matches the expected type.
isSet(value: any): booleanChecks if a value is an instance ofSet.console.log(isSet(new Set())); // true console.log(isSet([])); // falseisMap(value: any): booleanChecks if a value is an instance ofMap.console.log(isMap(new Map())); // true console.log(isMap({})); // falseisWeakSet(value: any): booleanChecks if a value is an instance ofWeakSet.console.log(isWeakSet(new WeakSet())); // true console.log(isWeakSet(new Set())); // falseisWeakMap(value: any): booleanChecks if a value is an instance ofWeakMap.console.log(isWeakMap(new WeakMap())); // true console.log(isWeakMap(new Map())); // falseisArr(value: any): booleanChecks if a value is an array.console.log(isArr([])); // true console.log(isArr({})); // falseisObj(value: any): booleanChecks if a value is a non-null object (excluding arrays).console.log(isObj({})); // true console.log(isObj([])); // false console.log(isObj(null)); // falseisNum(value: any): booleanChecks if a value is a number.console.log(isNum(42)); // true console.log(isNum('42')); // falseisTxt(value: any): booleanChecks if a value is a string.console.log(isTxt('hello')); // true console.log(isTxt(42)); // falseisStr(value: any): booleanAlias forisTxt. Checks if a value is a string.console.log(isStr('hello')); // true console.log(isStr(42)); // falseisBool(value: any): booleanChecks if a value is a boolean.console.log(isBool(true)); // true console.log(isBool('true')); // falseisFunc(value: any): booleanChecks if a value is a function.console.log(isFunc(() => {})); // true console.log(isFunc({})); // falseisSymbol(value: any): booleanChecks if a value is a symbol.console.log(isSymbol(Symbol())); // true console.log(isSymbol('symbol')); // falseisInt(value: any): booleanChecks if a value is an integer.console.log(isInt(42)); // true console.log(isInt(42.42)); // falseisFloat(value: any): booleanChecks if a value is a floating-point number.console.log(isFloat(42.42)); // true console.log(isFloat(42)); // falseisNan(value: any): booleanChecks if a value is NaN (Not a Number).console.log(isNan(NaN)); // true console.log(isNan(42)); // falseisNull(value: any): booleanChecks if a value is null.console.log(isNull(null)); // true console.log(isNull(undefined)); // falseisUndefined(value: any): booleanChecks if a value is undefined.console.log(isUndefined(undefined)); // true console.log(isUndefined(null)); // falseisDefined(value: any): booleanChecks if a value is defined (not undefined).console.log(isDefined(42)); // true console.log(isDefined(undefined)); // falseisDefinedStrict(value: any): booleanStrictly checks if a value is defined (not null, undefined, or NaN).console.log(isDefinedStrict(42)); // true console.log(isDefinedStrict(null)); // false console.log(isDefinedStrict(NaN)); // false console.log(isDefinedStrict(undefined)); // falseisInf(value: any): booleanChecks if a value is Infinity.console.log(isInf(Infinity)); // true console.log(isInf(42)); // falseisFinite(value: any): booleanChecks if a value is a finite number.console.log(isFinite(42)); // true console.log(isFinite(Infinity)); // falseisTruthy(value: any): booleanChecks if a value is truthy (not falsy).console.log(isTruthy(1)); // true console.log(isTruthy(0)); // falseisFalsy(value: any): booleanChecks if a value is falsy (false, 0, "", null, undefined, or NaN).console.log(isFalsy(0)); // true console.log(isFalsy(1)); // falseisFalse(value: any): booleanChecks if a value is strictlyfalse.console.log(isFalse(false)); // true console.log(isFalse(true)); // falseisTrue(value: any): booleanChecks if a value is strictlytrue.console.log(isTrue(true)); // true console.log(isTrue(false)); // falseisIterable(value: any): booleanChecks if a value is iterable (has anSymbol.iteratorproperty).console.log(isIterable([1, 2, 3])); // true console.log(isIterable(42)); // falseisEven(value: any): booleanChecks if a number is even.console.log(isEven(42)); // true console.log(isEven(43)); // falseisOdd(value: any): booleanChecks if a number is odd.console.log(isOdd(43)); // true console.log(isOdd(42)); // falseisError(value: any): booleanChecks if a value is an instance of theErrorobject.console.log(isError(new Error())); // true console.log(isError('Error')); // falseisChildOf(child: any, parent: any): booleanChecks if an object is a child of the given class (instanceof).class Parent {} const child = new Parent(); console.log(isChildOf(child, Parent)); // trueisClass(value: any): booleanChecks if a value is a class (constructor function).console.log(isClass(class MyClass {})); // true console.log(isClass(function myFunc() {})); // falseisSubclass(child: any, parent: any): booleanChecks if a class is a subclass of another class.class Parent {} class Child extends Parent {} console.log(isSubclass(Child, Parent)); // trueisDate(value: any): booleanChecks if a value is a validDateobject.console.log(isDate(new Date())); // true console.log(isDate('2021-01-01')); // falseisRegex(value: any): booleanChecks if a value is a validRegExp(Regular Expression).console.log(isRegex(/abc/)); // true console.log(isRegex('abc')); // falseisPromise(value: any): booleanChecks if a value is a Promise.console.log(isPromise(Promise.resolve())); // true console.log(isPromise(function () {})); // false
Pattern Functions
Pattern functions allow you to validate whether a value follows a specific pattern.
isCamelCase(value: any): booleanChecks if a string is in camelCase format.console.log(isCamelCase('camelCase')); // true console.log(isCamelCase('snake_case')); // falseisPascalCase(value: any): booleanChecks if a string is in PascalCase format.console.log(isPascalCase('PascalCase')); // true console.log(isPascalCase('pascalCase')); // falseisSnakeCase(value: any): booleanChecks if a string is in snake_case format.console.log(isSnakeCase('snake_case')); // true console.log(isSnakeCase('snakeCase')); // falseiskababCase(value: any): booleanChecks if a string is in kebab-case format.console.log(iskababCase('kebab-case')); // true console.log(iskababCase('kebabCase')); // falseisIso(value: any): booleanChecks if a string is in ISO 8601 format (e.g., "2024-11-28T12:30:00.000Z").console.log(isIso('2024-11-28T12:30:00.000Z')); // true console.log(isIso('2024-11-28')); // falseisDateTime(value: any): booleanChecks if a string is in the datetime format (e.g., "2024-11-28 12:30:00").console.log(isDateTime('2024-11-28 12:30:00')); // true console.log(isDateTime('2024-11-28')); // falseisDateStr(date: any): booleanChecks if a string is in the date format (e.g., "2024-11-28").console.log(isDateStr('2024-11-28')); // true console.log(isDateStr('2024-11-28 12:30:00')); // falseisTimeStr(date: any): booleanChecks if a string is in the time format (e.g., "12:30:00").console.log(isTimeStr('12:30:00')); // true console.log(isTimeStr('12:30')); // falseisPropExp(value: any): booleanChecks if a string is a valid property name (e.g., a valid js variable name).console.log(isPropExp('myVar')); // true console.log(isPropExp('123var')); // falseisNumExp(value: any): booleanChecks if a string is a valid number (including integers and floats).console.log(isNumExp('123')); // true console.log(isNumExp('123.45')); // true console.log(isNumExp('abc')); // falseisIntExp(value: any): booleanChecks if a string is a valid integer.console.log(isIntExp('123')); // true console.log(isIntExp('123.45')); // falseisFloatExp(value: any): booleanChecks if a string is a valid float (decimal number).console.log(isFloatExp('123.45')); // true console.log(isFloatExp('123')); // falseisStrExp(value: any): booleanChecks if a string is a valid string literal (single or double-quoted).console.log(isStrExp("'hello'")); // true console.log(isStrExp('"world"')); // true console.log(isStrExp('hello')); // falseisDotExp(value: any): booleanChecks if a string is a valid dot notation expression for property access.console.log(isDotExp('object.property')); // true console.log(isDotExp('object.123property')); // falseisBracketExp(value: any): booleanChecks if a string is a valid bracket notation expression for property access.console.log(isBracketExp('object["property"]')); // true console.log(isBracketExp('object["123"]')); // falseisAnyExp(value: any): booleanChecks if a string is a valid combination of dot and bracket notations.console.log(isAnyExp('object.property[0]')); // true console.log(isAnyExp('object.property["key"]')); // true console.log(isAnyExp('object.123property[0]')); // false
Content Functions
Content functions help determine if the content of an object, array, or string matches the expected structure or value.
isFullObj(value: any): booleanChecks if an object is non-empty (has at least one property).console.log(isFullObj({ key: 'value' })); // true console.log(isFullObj({})); // falseisFullArr(value: any): booleanChecks if an array is non-empty (has at least one element).console.log(isFullArr([1, 2, 3])); // true console.log(isFullArr([])); // falseisFullStr(value: any): booleanChecks if a string is non-empty (contains at least one non-whitespace character).console.log(isFullStr('Hello')); // true console.log(isFullStr(' ')); // falseisEmptyObj(value: any): booleanChecks if an object is empty (has no properties).console.log(isEmptyObj({})); // true console.log(isEmptyObj({ key: 'value' })); // falseisEmptyArr(value: any): booleanChecks if an array is empty (contains no elements).console.log(isEmptyArr([])); // true console.log(isEmptyArr([1])); // falseisEmptyStr(value: any): booleanChecks if a string is empty (contains non-whitespace characters).console.log(isEmptyStr('')); // true console.log(isEmptyStr(' ')); // true console.log(isEmptyStr('sss')); // true console.log(isEmptyStr('Hello')); // falseisArrOfNum(value: any): booleanChecks if an array is non-empty and contains only numbers.console.log(isArrOfNum([1, 2, 3.3])); // true console.log(isArrOfNum([1, 'two', 3.3])); // falseisArrOfInt(value: any): booleanChecks if an array is non-empty and contains only integers.console.log(isArrOfInt([1, 2, 3])); // true console.log(isArrOfInt([1, 2.5, 3])); // falseisArrOfFloat(value: any): booleanChecks if an array is non-empty and contains only floating-point numbers.console.log(isArrOfFloat([1.5, 2.7, 3.2])); // true console.log(isArrOfFloat([1, 2.5, '3'])); // falseisArrOfStr(value: any): booleanChecks if an array is non-empty and contains only strings.console.log(isArrOfStr(['hello', 'world'])); // true console.log(isArrOfStr(['hello', 123])); // falseisArrOfArr(value: any): booleanChecks if an array is non-empty and contains only arrays.console.log(isArrOfArr([[], []])); // true console.log(isArrOfArr([[], 'two'])); // falseisArrOfObj(value: any): booleanChecks if an array is non-empty and contains only objects.console.log(isArrOfObj([{}, {}])); // true console.log(isArrOfObj([{}, 1])); // falseisArrOfFunc(value: any): booleanChecks if an array is non-empty and contains only functions.console.log(isArrOfFunc([() => {}, () => {}])); // true console.log(isArrOfFunc([() => {}, 'string'])); // falseisArrOfBool(value: any): booleanChecks if an array is non-empty and contains only booleans.console.log(isArrOfBool([true, false, true])); // true console.log(isArrOfBool([true, 'false'])); // falseisArrOfAny(value: any): booleanChecks if an array is non-empty and allows any type of value (no type restriction).console.log(isArrOfAny([1, 'string', true])); // true console.log(isArrOfAny([])); // falseisObjOfNum(value: any): booleanChecks if an object is non-empty and contains only numeric values.console.log(isObjOfNum({ a: 1, b: 2.2 })); // true console.log(isObjOfNum({ a: 1, b: 'two' })); // falseisObjOfInt(value: any): booleanChecks if an object is non-empty and contains only integer values.console.log(isObjOfInt({ a: 1, b: 2 })); // true console.log(isObjOfInt({ a: 1, b: 2.5 })); // falseisObjOfFloat(value: any): booleanChecks if an object is non-empty and contains only floating-point values.console.log(isObjOfFloat({ a: 1.1, b: 2.5 })); // true console.log(isObjOfFloat({ a: 1, b: '2.5' })); // falseisObjOfStr(value: any): booleanChecks if an object is non-empty and contains only string values.console.log(isObjOfStr({ a: 'apple', b: 'banana' })); // true console.log(isObjOfStr({ a: 'apple', b: 123 })); // falseisObjOfArr(value: any): booleanChecks if an object is non-empty and contains only arrays as values.console.log(isObjOfArr({ a: [1, 2], b: [3, 4] })); // true console.log(isObjOfArr({ a: [1, 2], b: 'string' })); // falseisObjOfObj(value: any): booleanChecks if an object is non-empty and contains only objects as values.console.log(isObjOfObj({ a: { x: 1 }, b: { y: 2 } })); // true console.log(isObjOfObj({ a: { x: 1 }, b: 'string' })); // falseisObjOfFunc(value: any): booleanChecks if an object is non-empty and contains only functions as values.console.log(isObjOfFunc({ a: () => {}, b: () => {} })); // true console.log(isObjOfFunc({ a: () => {}, b: 'string' })); // falseisObjOfBool(value: any): booleanChecks if an object is non-empty and contains only boolean values.console.log(isObjOfBool({ a: true, b: false })); // true console.log(isObjOfBool({ a: true, b: 'false' })); // falseisObjOfAny(value: any): booleanChecks if an object is non-empty and allows any type of value (no type restriction).console.log(isObjOfAny({ a: 1, b: 'string' })); // true console.log(isObjOfAny({})); // falsehasProp(object: any, property: any): booleanChecks if an object has a specific property.console.log(hasProp({ a: 1, b: 2 }, 'a')); // true console.log(hasProp({ a: 1, b: 2 }, 'c')); // falsehasProps(object: any, ...properties: Array<any>): booleanChecks if an object has all specified properties.console.log(hasProps({ a: 1, b: 2 }, 'a', 'b')); // true console.log(hasProps({ a: 1, b: 2 }, 'a', 'c')); // falsehasIndex(target: any, index: any): booleanChecks if an index is within the range of a target (array, string, or object).console.log(hasIndex([1, 2, 3], 1)); // true console.log(hasIndex([1, 2, 3], 3)); // false console.log(hasIndex('hello', 1)); // true console.log(hasIndex('hello', 5)); // falsehasLength(target: any, length: any): booleanChecks if the length of a target (array, string, or object) matches a specified length.console.log(hasLength([1, 2, 3], 3)); // true console.log(hasLength([1, 2, 3], 2)); // false console.log(hasLength('hello', 5)); // true console.log(hasLength('hello', 4)); // false console.log(hasLength({ a: 1, b: 2 }, 2)); // true console.log(hasLength({ a: 1, b: 2 }, 1)); // falsehasFlag(regex: any, flag: any): booleanChecks if a regex has a specific flag.console.log(hasFlag(/abc/i, 'i')); // true console.log(hasFlag(/abc/i, 'g')); // falsehasKey(object: any, key: any): booleanChecks if an object has a specific key (property name).console.log(hasKey({ a: 1, b: 2 }, 'a')); // true console.log(hasKey({ a: 1, b: 2 }, 'c')); // falsehasKeys(object: any, ...keys: Array<any>): booleanChecks if an object contains all of the specified keys (property names).console.log(hasKeys({ a: 1, b: 2 }, 'a', 'b')); // true console.log(hasKeys({ a: 1, b: 2 }, 'a', 'c')); // false console.log(hasKeys({ a: 1, b: 2, c: 3 }, 'a', 'b')); // truehasValue(target: any, value: any): booleanChecks if an (object|array|string) has a specific value.console.log(hasValue({ a: 1, b: 2 }, 2)); // true console.log(hasValue([1, 2, 3], 2)); // true console.log(hasValue('hello world', 'world')); // true console.log(hasValue({ a: 1, b: 2 }, 3)); // falsehasValues(target: any, ...values: Array<any>): booleanChecks if an (object|array|string) contains all of the specified values.console.log(hasValues({ a: 1, b: 2 }, 1, 2)); // true console.log(hasValues([1, 2, 3], 1, 3)); // true console.log(hasValues('hello world', 'hello', 'world')); // true console.log(hasValues(['hello', 'foo'], 'hello', 'bar')); // false