Package Exports
- @megaorm/test
- @megaorm/test/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@megaorm/test) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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([])); // false
isMap(value: any): booleanChecks if a value is an instance ofMap.console.log(isMap(new Map())); // true console.log(isMap({})); // false
isWeakSet(value: any): booleanChecks if a value is an instance ofWeakSet.console.log(isWeakSet(new WeakSet())); // true console.log(isWeakSet(new Set())); // false
isWeakMap(value: any): booleanChecks if a value is an instance ofWeakMap.console.log(isWeakMap(new WeakMap())); // true console.log(isWeakMap(new Map())); // false
isArr(value: any): booleanChecks if a value is an array.console.log(isArr([])); // true console.log(isArr({})); // false
isObj(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)); // false
isNum(value: any): booleanChecks if a value is a number.console.log(isNum(42)); // true console.log(isNum('42')); // false
isTxt(value: any): booleanChecks if a value is a string.console.log(isTxt('hello')); // true console.log(isTxt(42)); // false
isStr(value: any): booleanAlias forisTxt. Checks if a value is a string.console.log(isStr('hello')); // true console.log(isStr(42)); // false
isBool(value: any): booleanChecks if a value is a boolean.console.log(isBool(true)); // true console.log(isBool('true')); // false
isFunc(value: any): booleanChecks if a value is a function.console.log(isFunc(() => {})); // true console.log(isFunc({})); // false
isSymbol(value: any): booleanChecks if a value is a symbol.console.log(isSymbol(Symbol())); // true console.log(isSymbol('symbol')); // false
isInt(value: any): booleanChecks if a value is an integer.console.log(isInt(42)); // true console.log(isInt(42.42)); // false
isFloat(value: any): booleanChecks if a value is a floating-point number.console.log(isFloat(42.42)); // true console.log(isFloat(42)); // false
isNan(value: any): booleanChecks if a value is NaN (Not a Number).console.log(isNan(NaN)); // true console.log(isNan(42)); // false
isNull(value: any): booleanChecks if a value is null.console.log(isNull(null)); // true console.log(isNull(undefined)); // false
isUndefined(value: any): booleanChecks if a value is undefined.console.log(isUndefined(undefined)); // true console.log(isUndefined(null)); // false
isDefined(value: any): booleanChecks if a value is defined (not undefined).console.log(isDefined(42)); // true console.log(isDefined(undefined)); // false
isDefinedStrict(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)); // false
isInf(value: any): booleanChecks if a value is Infinity.console.log(isInf(Infinity)); // true console.log(isInf(42)); // false
isFinite(value: any): booleanChecks if a value is a finite number.console.log(isFinite(42)); // true console.log(isFinite(Infinity)); // false
isTruthy(value: any): booleanChecks if a value is truthy (not falsy).console.log(isTruthy(1)); // true console.log(isTruthy(0)); // false
isFalsy(value: any): booleanChecks if a value is falsy (false, 0, "", null, undefined, or NaN).console.log(isFalsy(0)); // true console.log(isFalsy(1)); // false
isFalse(value: any): booleanChecks if a value is strictlyfalse.console.log(isFalse(false)); // true console.log(isFalse(true)); // false
isTrue(value: any): booleanChecks if a value is strictlytrue.console.log(isTrue(true)); // true console.log(isTrue(false)); // false
isIterable(value: any): booleanChecks if a value is iterable (has anSymbol.iteratorproperty).console.log(isIterable([1, 2, 3])); // true console.log(isIterable(42)); // false
isEven(value: any): booleanChecks if a number is even.console.log(isEven(42)); // true console.log(isEven(43)); // false
isOdd(value: any): booleanChecks if a number is odd.console.log(isOdd(43)); // true console.log(isOdd(42)); // false
isError(value: any): booleanChecks if a value is an instance of theErrorobject.console.log(isError(new Error())); // true console.log(isError('Error')); // false
isChildOf(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)); // true
isClass(value: any): booleanChecks if a value is a class (constructor function).console.log(isClass(class MyClass {})); // true console.log(isClass(function myFunc() {})); // false
isSubclass(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)); // true
isDate(value: any): booleanChecks if a value is a validDateobject.console.log(isDate(new Date())); // true console.log(isDate('2021-01-01')); // false
isRegex(value: any): booleanChecks if a value is a validRegExp(Regular Expression).console.log(isRegex(/abc/)); // true console.log(isRegex('abc')); // false
isPromise(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')); // false
isPascalCase(value: any): booleanChecks if a string is in PascalCase format.console.log(isPascalCase('PascalCase')); // true console.log(isPascalCase('pascalCase')); // false
isSnakeCase(value: any): booleanChecks if a string is in snake_case format.console.log(isSnakeCase('snake_case')); // true console.log(isSnakeCase('snakeCase')); // false
iskababCase(value: any): booleanChecks if a string is in kebab-case format.console.log(iskababCase('kebab-case')); // true console.log(iskababCase('kebabCase')); // false
isIso(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')); // false
isDateTime(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')); // false
isDateStr(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')); // false
isTimeStr(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')); // false
isPropExp(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')); // false
isNumExp(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')); // false
isIntExp(value: any): booleanChecks if a string is a valid integer.console.log(isIntExp('123')); // true console.log(isIntExp('123.45')); // false
isFloatExp(value: any): booleanChecks if a string is a valid float (decimal number).console.log(isFloatExp('123.45')); // true console.log(isFloatExp('123')); // false
isStrExp(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')); // false
isDotExp(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')); // false
isBracketExp(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"]')); // false
isAnyExp(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({})); // false
isFullArr(value: any): booleanChecks if an array is non-empty (has at least one element).console.log(isFullArr([1, 2, 3])); // true console.log(isFullArr([])); // false
isFullStr(value: any): booleanChecks if a string is non-empty (contains at least one non-whitespace character).console.log(isFullStr('Hello')); // true console.log(isFullStr(' ')); // false
isEmptyObj(value: any): booleanChecks if an object is empty (has no properties).console.log(isEmptyObj({})); // true console.log(isEmptyObj({ key: 'value' })); // false
isEmptyArr(value: any): booleanChecks if an array is empty (contains no elements).console.log(isEmptyArr([])); // true console.log(isEmptyArr([1])); // false
isEmptyStr(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')); // false
isArrOfNum(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])); // false
isArrOfInt(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])); // false
isArrOfFloat(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'])); // false
isArrOfStr(value: any): booleanChecks if an array is non-empty and contains only strings.console.log(isArrOfStr(['hello', 'world'])); // true console.log(isArrOfStr(['hello', 123])); // false
isArrOfArr(value: any): booleanChecks if an array is non-empty and contains only arrays.console.log(isArrOfArr([[], []])); // true console.log(isArrOfArr([[], 'two'])); // false
isArrOfObj(value: any): booleanChecks if an array is non-empty and contains only objects.console.log(isArrOfObj([{}, {}])); // true console.log(isArrOfObj([{}, 1])); // false
isArrOfFunc(value: any): booleanChecks if an array is non-empty and contains only functions.console.log(isArrOfFunc([() => {}, () => {}])); // true console.log(isArrOfFunc([() => {}, 'string'])); // false
isArrOfBool(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'])); // false
isArrOfAny(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([])); // false
isObjOfNum(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' })); // false
isObjOfInt(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 })); // false
isObjOfFloat(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' })); // false
isObjOfStr(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 })); // false
isObjOfArr(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' })); // false
isObjOfObj(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' })); // false
isObjOfFunc(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' })); // false
isObjOfBool(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' })); // false
isObjOfAny(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({})); // false
hasProp(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')); // false
hasProps(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')); // false
hasIndex(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)); // false
hasLength(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)); // false
hasFlag(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')); // false
hasKey(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')); // false
hasKeys(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')); // true
hasValue(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)); // false
hasValues(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