README

Pro-script Library Documentation
https://pro-script.dev
https://pro-script.gitbook.io/as-is
Please update to 1.6+ version. Major bugs have been fixed, and a validation error feature has been added.
The NEW Enum functionality !!!
This is example of old variant
Enum.init('enum object here'); // where init is mandatoryThis is new functionality
Enum.roles({ // now roles it's a name of stored enum inside of the as/is proxy
admin: 0,
user: 1
});
as.roles('admin'); // -> admin
as.roles('fakeRole'); // -> TypeError: String is not a(an) member of roles enum
Ofc an old Enum checker works too
const enumName = Enum.roles({
admin: 0,
user: 1
});
as.Enum(enumName); // -> Enum { '0': 'admin', '1': 'user', admin: 0, user: 1 }Now you can use enums in types like this
const { IUser } = Type({
IUser: {
name: as.string,
email: as.email,
password: as.string,
avatar: (value)=> {
as.url(value);
as.minStr({arg: value, value: 3});
as.maxStr({arg: value, value: 40});
},
role: as.roles
}
});The Check password added to strings validator.
as.password('Password1!') // -> Password1!'
as.password('Password1') // -> TypeErrorOverview
This library provides a comprehensive framework for type checking, utility functions, and macros for automated testing in JavaScript environments. It offers tools to validate types, manage enumerations, and enhance code quality through structured checks and assertions.
Table of Contents
Installation
npm install @pro-script/as-is @pro-script/as-is-pluginsFor Browsers
Without module:
<script src="https://www.unpkg.com/@pro-script/as-is"></script>
<script src="https://www.unpkg.com/@pro-script/as-is-plugins/numbers"></script>
<script src="https://www.unpkg.com/@pro-script/as-is-plugins/strings"></script>With module:
<script type="module">
import { Checker } from "https://www.unpkg.com/@pro-script/as-is";
import { NumbersValidator } from "https://www.unpkg.com/@pro-script/as-is-plugins/numbers";
import { StringsValidator } from "https://www.unpkg.com/@pro-script/as-is-plugins/strings";
// usage code
</script>Usage
Node.js (ESM)
import { Checker } from '@pro-script/as-is';
import { NumbersValidator } from '@pro-script/as-is-plugins/numbers';
import { StringsValidator } from '@pro-script/as-is-plugins/strings';
const { as, is } = new Checker({ integrate: Object.assign(NumbersValidator, StringsValidator) });Node.js (CommonJS)
const { Checker } = require('@pro-script/as-is');
const { NumbersValidator } = require('@pro-script/as-is-plugins/numbers');
const { StringsValidator } = require('@pro-script/as-is-plugins/strings');
const { as, is } = new Checker({ integrate: Object.assign(NumbersValidator, StringsValidator) });Browser
Without module:
<script>
const { as, is } = new Checker({ integrate: Object.assign(NumbersValidator, StringsValidator) });
</script>With module:
<script type="module">
import { Checker } from "https://www.unpkg.com/@pro-script/as-is";
import { NumbersValidator } from "https://www.unpkg.com/@pro-script/as-is-plugins/numbers";
import { StringsValidator } from "https://www.unpkg.com/@pro-script/as-is-plugins/strings";
const { as, is } = new Checker({ integrate: Object.assign(NumbersValidator, StringsValidator) });
</script>With import map:
<script type="importmap">
{
"imports": {
"@pro-script/as-is": "https://www.unpkg.com/@pro-script/as-is",
"@pro-script/as-is-plugins/numbers": "https://www.unpkg.com/@pro-script/as-is-plugins/numbers",
"@pro-script/as-is-plugins/strings": "https://www.unpkg.com/@pro-script/as-is-plugins/strings",
}
}
</script>
<script type="module">
import { Checker } from '@pro-script/as-is';
import { NumbersValidator } from '@pro-script/as-is-plugins/numbers';
import { StringsValidator } from '@pro-script/as-is-plugins/strings';
const { as, is } = new Checker({ integrate: Object.assign(NumbersValidator, StringsValidator) });
</script>Everything in one code block
const checker = new Checker({
'IF/ELSE/END': true,
strict: true,
Enum: true,
utility: true,
integrate: Object.assign(NumbersValidator, StringsValidator) });
const { multi, Interface, as, is, IF, ELSE, END, optional, get, macro, strict, Enum } = checker;
const { START, STOP, FINISH, METHOD, PROPERTY, IS, CHECK, passed, failed } = new MicroTest({ is, as });in global scope
Object.assign(global, { multi, Interface, as, is, Enum });or
Object.assign(window, { multi, Interface, as, is, Enum });After that you can use an as or is etc in other files.
Summary of Features
Supported Types:
Types list with alias name:
Number | number
String | string
Boolean | boolean
Symbol | symbol
Function | function
BigInt | bigInt | bigint
Array | array
TypedArray | typedArray | Typedarray | typedarray
Buffer | buffer
SharedArrayBuffer | sharedArrayBuffer | SharedarrayBuffer | sharedsrrayBuffer | sharedsrraybuffer
Date | date
Object | date
Class | class
instance
Enum | enum
Set | set
Map | map
Iterator | iterator
Nullish | nullish
WeakSet | weakSet | weeakset
WeakMap | wearMap | weakmap
WeakRef | weakRef | weakref
RegExp | regExp | regexp
Promise | promise
Error | error
RangeError | rangeError
ReferenceError | referenceError
SyntaxError |syntaxError
TypeError | typeError
Any | any
IF/ELSE/END for type checking
IF.number(string_)? (
console.log('IF type checking')
):ELSE.string(string_)? (
console.log('ELSE type checking'),
expect(string_).to.be.eq(string_)
):END;Strict type object:
strict.string`name`
strict.name = 'string'
// or
strict.string`name`.name = 'string'Class checking:
[className]
[classInstance]
is.date(Date);
is.date(new Date);
is.class(Date) or is.class(new Date)Interface Interfaces works only in the set way where IName = { * interface data * }, not like IName({ * interface data * })
const { IUser } = Interface({
IUser: {
name: as.string,
age: as.number,
birthDate: as.date
}
});
as.IUser = { name: 'Jesus', age: 2022, birthDate: 'Sat Apr 7 0:0:0 GMT+0200 (Central European Summer Time)'};
function example(name, age, _ = as.IUser = { name, age }) {
console.log(name, age);
return 'returned string';
}
as.StringNumber(example({ name: 'text', age: 12, pages:['page'] }));Types Types works only in the apply way where TName({ * types data * }), not like interfaces TName = { * types data * }
const { multi, Interface, Type, as, is, IF, ELSE, END, optional, get, macro, strict, Enum } = checker;
const { TUser } = Type({
IUser: {
name: as.string,
birthDate: as.date,
age: (value)=> {
// any code here to check the value
}
}
});
as.TUser({ name: 'Jesus', age: 2022, birthDate: 'Sat Apr 7 0:0:0 GMT+0200 (Central European Summer Time)'});Integration
You can integrate any feature you want.
import { Checker, BaseInterface, MicroTest, Utility } from '@pro-script/as-is';
import axios from "axios";
const integrate = {
up: async function (url) {
const result = await axios.get(url);
if(result.status === 200) return 'Ok';
else throw new TypeError('url is down');
}
};
const { multi, strict, as, is } = new Checker({ integrate });
const isUrl = as;
async function example(arg, arg2, arg3,
type = [as.string(arg), as.number(arg2), as.boolean(arg3)]) {
await isUrl.up('https://google.com');
console.log(type);
};
await example('text', 2, true)
// [ 'text', 2, true ]
await isUrl?.up('https://not-google.com');
// TypeError: url is downInstancing
Minimal set of methods.
const { as, is } = new Checker;All methods without plugins.
const checker = new Checker({ 'IF/ELSE/END': true, strict: true, Enum: true, utility: true });
const { multi, Interface, as, is, IF, ELSE, END, optional, get, macro, strict, Enum } = checker;
const { START, STOP, FINISH, METHOD, PROPERTY, IS, CHECK, passed, failed } = new MicroTest({ is, as });All methods with plugins.
const checker = new Checker({
'IF/ELSE/END': true,
strict: true,
Enum: true,
utility: true,
integrate: Object.assign(NumbersValidator, StringsValidator)
});
const { multi, Interface, as, is, IF, ELSE, END, optional, get, macro, strict, Enum } = checker;
const { START, STOP, FINISH, METHOD, PROPERTY, IS, CHECK, passed, failed } = new MicroTest({ is, as });Type Checks
String
is.string(value) -> true | false
as.string(value) -> value | TypeError: [get.type(value)] is not a(an) stringDescription:
Checks if the provided argument is a string.
is.string(arg):
Returns
trueifargis a string.Returns
falseotherwise.
as.string(arg):
Returns
argif it is a string.Throws
TypeErrorif
arg is not a string.
Example:
is.string('hello'); // Returns true
is.string(123); // Returns false
as.string('hello'); // Returns 'hello'
as.string(123); // Throws TypeError: Number is not a(an) stringNumber
is.number(value) -> true | false
as.number(value) -> value | TypeError: [get.type(value)] is not is not a(an) numberDescription:
Checks if the provided argument is a number.
is.number(arg):
Returns
trueifargis a number.Returns
falseotherwise.
as.number(arg):
Returns
argif it is a number.Throws
TypeErrorifargis not a number.
Example:
is.number(123); // Returns true
is.number('123'); // Returns false
as.number(123); // Returns 123
as.number('123'); // Throws TypeError: Number is not a(an) numberBoolean
is.boolean(value) -> true | false
as.boolean(value) -> value | TypeError: [get.type(value)] is not a(an) booleanDescription:
Checks if the provided argument is a boolean.
is.boolean(arg):
Returns
trueifargis a boolean.Returns
falseotherwise.
as.boolean(arg):
Returns
argif it is a boolean.Throws
TypeErrorifargis not a boolean.
Example:
is.boolean(true); // Returns true
is.boolean(1); // Returns false
as.boolean(true); // Returns true
as.boolean(1); // Throws TypeError: Number is not a(an) booleanSymbol
is.symbol(value) -> true | false
as.symbol(value) -> value | TypeError: [get.type(value)] is not a(an) symbolDescription:
Checks if the provided argument is a symbol.
is.symbol(arg):
Returns
trueifargis a symbol.Returns
falseotherwise.
as.symbol(arg):
Returns
argif it is a symbol.Throws
TypeErrorifargis not a symbol.
Example:
const sym = Symbol('test');
is.symbol(sym); // Returns true
is.symbol('symbol'); // Returns false
as.symbol(sym); // Returns sym
as.symbol('symbol'); // Throws TypeError: String is not a(an) symbolFunction
is.function(value) -> true | false
as.function(value) -> value | TypeError: [get.type(value)] is not a(an) functionDescription:
Checks if the provided argument is a function.
is.function(arg):
Returns
trueifargis a function.Returns
falseotherwise.
as.function(arg):
Returns
argif it is a function.Throws
TypeErrorifargis not a function.
Example:
const func = () => {};
is.function(func); // Returns true
is.function(123); // Returns false
as.function(func); // Returns func
as.function(123); // Throws TypeError: Number is not a(an) functionBigInt
is.bigInt(value) -> true | false
as.bigInt(value) -> value | TypeError: [get.type(value)] is not a(an) bigIntDescription:
Checks if the provided argument is a BigInt.
is.bigInt(arg):
Returns
trueifargis a BigInt.Returns
falseotherwise.
as.bigInt(arg):
Returns
argif it is a BigInt.Throws
TypeErrorifargis not a BigInt.
Example:
const bigIntExample = BigInt('1234567890123456789012345678901234567890');
is.bigInt(bigIntExample); // Returns true
is.bigInt(1234567890); // Returns false
as.bigInt(bigIntExample); // Returns bigIntExample
as.bigInt(1234567890); // Throws TypeError: Number is not a(an) bigIntArray
is.array(value) -> true | false
as.array(value) -> value | TypeError: [get.type(value)] is not a(an) bigIntDescription:
Checks if the provided argument is an array.
is.array(arg):
Returns
trueifargis an array.Returns
falseotherwise.
as.array(arg):
Returns
argif it is an array.Throws
TypeErrorifargis not an array.
Example:
const arrayExample = [];
is.array(arrayExample); // Returns true
is.array('not an array'); // Returns false
as.array(arrayExample); // Returns arrayExample
as.array('not an array'); // Throws TypeError: String is not a(an) arrayDate
is.date(value) -> true | false
as.date(value) -> value | TypeError: [get.type(value)] is not a(an) dateDescription:
Checks if the provided argument is a date.
is.date(arg):
Returns
trueifargis a date.Returns
falseotherwise.
as.date(arg):
Returns
argif it is a date.Throws
TypeErrorifargis not a date.
Example:
const dateExample = new Date();
is.date(dateExample); // Returns true
is.date('not a date'); // Returns false
as.date(dateExample); // Returns dateExample
as.date('not a date'); // Throws TypeError: String is not a(an) dateObject
is.object(value) -> true | false
as.object(value) -> value | TypeError: [get.type(value)] is not a(an) objectDescription:
Checks if the provided argument is an object.
is.object(arg):
Returns
trueifargis an object.Returns
falseotherwise.
as.object(arg):
Returns
argif it is an object.Throws
TypeErrorifargis not an object.
Example:
const objectExample = {};
is.object(objectExample); // Returns true
is.object('not an object'); // Returns false
as.object(objectExample); // Returns objectExample
as.object('not an object'); // Throws TypeError: String is not a(an) objectSet
is.set(value) -> true | false
as.set(value) -> value | TypeError: [get.type(value)] is not a(an) setDescription:
Checks if the provided argument is a set.
is.set(arg):
Returns
trueifargis a set.Returns
falseotherwise.
as.set(arg):
Returns
argif it is a set.Throws
TypeErrorifargis not a set.
Example:
const setExample = new Set();
is.set(setExample); // Returns true
is.set('not a set'); // Returns false
as.set(setExample); // Returns setExample
as.set('not a set'); // Throws TypeError: String is not a(an) setMap
is.map(value) -> true | false
as.map(value) -> value | TypeError: [get.type(value)] is not a(an) mapDescription:
Checks if the provided argument is a map.
is.map(arg):
Returns
trueifargis a map.Returns
falseotherwise.
as.map(arg):
Returns
argif it is a map.Throws
TypeErrorifargis not a map.
Example:
const mapExample = new Map();
is.map(mapExample); // Returns true
is.map('not a map'); // Returns false
as.map(mapExample); // Returns mapExample
as.map('not a map'); // Throws TypeError: String is not a(an) mapWeakSet
is.weakSet(value) -> true | false
as.weakSet(value) -> value | TypeError: [get.type(value)] is not a(an) weakSetDescription:
Checks if the provided argument is a WeakSet.
is.weakSet(arg):
Returns
trueifargis a WeakSet.Returns
falseotherwise.
as.weakSet(arg):
Returns
argif it is a WeakSet.Throws
TypeErrorifargis not a WeakSet.
Example:
const weakSetExample = new WeakSet();
is.weakSet(weakSet
Example); // Returns true
is.weakSet('not a weakSet'); // Returns false
as.weakSet(weakSetExample); // Returns weakSetExample
as.weakSet('not a weakSet'); // Throws TypeError: String is not a(an) weakSetWeakMap
is.weakMap(value) -> true | false
as.weakMap(value) -> value | TypeError: [get.type(value)] is not a(an) weakMapDescription:
Checks if the provided argument is a WeakMap.
is.weakMap(arg):
Returns
trueifargis a WeakMap.Returns
falseotherwise.
as.weakMap(arg):
Returns
argif it is a WeakMap.Throws
TypeErrorifargis not a WeakMap.
Example:
const weakMapExample = new WeakMap();
is.weakMap(weakMapExample); // Returns true
is.weakMap('not a weakMap'); // Returns false
as.weakMap(weakMapExample); // Returns weakMapExample
as.weakMap('not a weakMap'); // Throws TypeError: String is not a(an) weakMapRegExp
is.regExp(value) -> true | false
as.regExp(value) -> value | TypeError: [get.type(value)] is not a(an) regExpDescription:
Checks if the provided argument is a regular expression.
is.regExp(arg):
Returns
trueifargis a regular expression.Returns
falseotherwise.
as.regExp(arg):
Returns
argif it is a regular expression.Throws
TypeErrorifargis not a regular expression.
Example:
const regExpExample = /./g;
is.regExp(regExpExample); // Returns true
is.regExp('not a regExp'); // Returns false
as.regExp(regExpExample); // Returns regExpExample
as.regExp('not a regExp'); // Throws TypeError: String is not a(an) regExpPromise
is.promise(value) -> true | false
as.promise(value) -> value | TypeError: [get.type(value)] is not a(an) promiseDescription:
Checks if the provided argument is a Promise.
is.promise(arg):
Returns
trueifargis a Promise.Returns
falseotherwise.
as.promise(arg):
Returns
argif it is a Promise.Throws
TypeErrorifargis not a Promise.
Example:
const promiseExample = new Promise((resolve) => resolve());
is.promise(promiseExample); // Returns true
is.promise('not a promise'); // Returns false
as.promise(promiseExample); // Returns promiseExample
as.promise('not a promise'); // Throws TypeError: String is not a(an) promiseGenerator
is.generator(value) -> true | false
as.generator(value) -> value | TypeError: [get.type(value)] is not a(an) generatorDescription:
Checks if the provided argument is a Generator.
is.generator(arg):
Returns
trueifargis a Generator.Returns
falseotherwise.
as.generator(arg):
Returns
argif it is a Generator.Throws
TypeErrorifargis not a Generator.
Example:
function* generatorExample() {
yield 1;
yield 2;
}
is.generator(generatorExample()); // Returns true
is.generator('not a generator'); // Returns false
as.generator(generatorExample()); // Returns generatorExample
as.generator('not a generator'); // Throws TypeError: String is not a(an) generatorHere is the documentation for the additional type checks:
TypedArray
is.typedArray(value) -> true | false
as.typedArray(value) -> value | TypeError: TypedArray is not a value that passed validationDescription:
Checks if the provided argument is a TypedArray.
is.typedArray(arg):
Returns
trueifargis a TypedArray.Returns
falseotherwise.
as.typedArray(arg):
Returns
argif it is a TypedArray.Throws
TypeErrorifargis not a TypedArray.
Example:
is.typedArray(new Int8Array()); // Returns true
is.typedArray([]); // Returns false
as.typedArray(new Int8Array()); // Returns Int8Array
as.typedArray([]); // Throws TypeError: Array is not a(an) typedArrayBuffer
is.buffer(value) -> true | false
as.buffer(value) -> value | TypeError: [get.type(value)] is not a(an) bufferDescription:
Checks if the provided argument is a Buffer.
is.buffer(arg):
Returns
trueifargis a Buffer.Returns
falseotherwise.
as.buffer(arg):
Returns
argif it is a Buffer.Throws
TypeErrorifargis not a Buffer.
Example:
is.buffer(Buffer.from('hello')); // Returns true
is.buffer('hello'); // Returns false
as.buffer(Buffer.from('hello')); // Returns Buffer
as.buffer('hello'); // Throws TypeError: String is not a(an) bufferSharedArrayBuffer
is.sharedArrayBuffer(value) -> true | false
as.sharedArrayBuffer(value) -> value | TypeError: [get.type(value)] is not a(an) sharedArrayBufferDescription:
Checks if the provided argument is a SharedArrayBuffer.
is.sharedArrayBuffer(arg):
Returns
trueifargis a SharedArrayBuffer.Returns
falseotherwise.
as.sharedArrayBuffer(arg):
Returns
argif it is a SharedArrayBuffer.Throws
TypeErrorifargis not a SharedArrayBuffer.
Example:
is.sharedArrayBuffer(new SharedArrayBuffer()); // Returns true
is.sharedArrayBuffer([]); // Returns false
as.sharedArrayBuffer(new SharedArrayBuffer()); // Returns SharedArrayBuffer
as.sharedArrayBuffer([]); // Throws TypeError: Array is not a(an) sharedArrayBufferDate
is.date(value) -> true | false
as.date(value) -> value | TypeError: [get.type(value)] is not a(an) dateDescription:
Checks if the provided argument is a Date object.
is.date(arg):
Returns
trueifargis a Date object.Returns
falseotherwise.
as.date(arg):
Returns
argif it is a Date object.Throws
TypeErrorifargis not a Date object.
Example:
is.date(new Date()); // Returns true
is.date('2021-01-01'); // Returns false
as.date(new Date()); // Returns Date
as.date('2021-01-01'); // Throws TypeError: String is not a(an) dateObject
is.object(value) -> true | false
as.object(value) -> value | TypeError: [get.type(value)] is not a(an) objectDescription:
Checks if the provided argument is an object.
is.object(arg):
Returns
trueifargis an object.Returns
falseotherwise.
as.object(arg):
Returns
argif it is an object.Throws
TypeErrorifargis not an object.
Example:
is.object({}); // Returns true
is.object('hello'); // Returns false
as.object({}); // Returns {}
as.object('hello'); // Throws TypeError: String is not a(an) objectClass
is.class(value) -> true | false
as.class(value) -> value | TypeError: [get.type(value)] is not a(an) classDescription:
Checks if the provided argument is a class.
is.class(arg):
Returns
trueifargis a class.Returns
falseotherwise.
as.class(arg):
Returns
argif it is a class.Throws
TypeErrorifargis not a class.
Example:
class MyClass {}
is.class(MyClass); // Returns true
is.class(() => {}); // Returns false
as.class(MyClass); // Returns MyClass
as.class(() => {}); // Throws TypeError: Function is not a(an) classInstance
is.instance(value, constructor) -> true | false
as.instance(value, constructor) -> value | TypeError: [get.type(value)] is not a(an) instanceDescription:
Checks if the provided argument is an instance of the specified class.
is.instance(arg, constructor):
Returns
trueifargis an instance ofconstructor.Returns
falseotherwise.
as.instance(arg, constructor):
Returns
argif it is an instance ofconstructor.Throws
TypeErrorifargis not an instance ofconstructor.
Example:
class MyClass {}
const instance = new MyClass();
is.instance(instance, MyClass); // Returns true
is.instance({}, MyClass); // Returns false
as.instance(instance, MyClass); // Returns instance
as.instance({}, MyClass); // Throws TypeError: Object is not a(an) instanceIterator
is.iterator(value) -> true | false
as.iterator(value) -> value | TypeError: [get.type(value)] is not a(an) iteratorDescription:
Checks if the provided argument is an iterator.
is.iterator(arg):
Returns
trueifargis an iterator.Returns
falseotherwise.
as.iterator(arg):
Returns
argif it is an iterator.Throws
TypeErrorifargis not an iterator.
Example:
const iterator = [][Symbol.iterator]();
is.iterator(iterator); // Returns true
is.iterator([]); // Returns false
as.iterator(iterator); // Returns iterator
as.iterator([]); // Throws TypeError: Array is not a(an) iteratorNullish
is.nullish(value) -> true | false
as.nullish(value) -> value | TypeError: [get.type(value)] is not a(an) nullishDescription:
Checks if the provided argument is null or undefined.
is.nullish(arg):
Returns
trueifargis null or undefined.Returns
falseotherwise.
as.nullish(arg):
Returns
argif it is null or undefined.Throws
TypeErrorifargis not null or undefined.
Example:
is.nullish(null); // Returns true
is.nullish(undefined); // Returns true
is.nullish('hello'); // Returns false
as.nullish(null); // Returns null
as.nullish(undefined); // Returns undefined
as.nullish('hello'); // Throws TypeError: String is not a(an) nullishError
is.error(value) -> true | false
as.error(value) -> value | TypeError: [get.type(value)] is not a(an) errorDescription:
Checks if the provided argument is an Error.
is.error(arg):
Returns
trueifargis an Error.Returns
falseotherwise.
as.error(arg):
Returns
argif it is an Error.Throws
TypeErrorifargis not an Error.
Example:
is.error(new Error()); // Returns true
is.error('error'); // Returns false
as.error(new Error()); // Returns Error
as.error('error'); // Throws TypeError: String is not a(an) errorRangeError
is.rangeError(value) -> true | false
as.rangeError(value) -> value | TypeError: [get.type(value)] is not a(an) rangeErrorDescription:
Checks if the provided argument is a RangeError.
is.rangeError(arg):
Returns
trueifargis a RangeError.Returns
falseotherwise.
as.rangeError(arg):
Returns
argif it is a RangeError.Throws
TypeErrorifargis not a RangeError.
Example:
is.rangeError(new RangeError()); // Returns true
is.rangeError('error'); // Returns false
as.rangeError(new RangeError()); // Returns RangeError
as.rangeError('error'); // Throws TypeError: String is not a(an) rangeErrorReferenceError
is.referenceError(value) -> true | false
as.referenceError(value) -> value | TypeError: [get.type(value)] is not a(an) referenceErrorDescription:
Checks if the provided argument is a ReferenceError.
is.referenceError(arg):
Returns
trueifargis a ReferenceError.Returns
falseotherwise.
as.referenceError(arg):
Returns
argif it is a ReferenceError.Throws
TypeErrorifargis not a ReferenceError.
Example:
is.referenceError(new ReferenceError()); // Returns true
is.referenceError('error'); // Returns false
as.referenceError(new ReferenceError()); // Returns ReferenceError
as.referenceError('error'); // Throws TypeError: String is not a(an) referenceErrorSyntaxError
is.syntaxError(value) -> true | false
as.syntaxError(value) -> value | TypeError: [get.type(value)] is not a(an) syntaxErrorDescription:
Checks if the provided argument is a SyntaxError.
is.syntaxError(arg):
Returns
trueifargis a SyntaxError.Returns
falseotherwise.
as.syntaxError(arg):
Returns
argif it is a SyntaxError.Throws
TypeErrorifargis not a SyntaxError.
Example:
is.syntaxError(new SyntaxError()); // Returns true
is.syntaxError('error'); // Returns false
as.syntaxError(new SyntaxError()); // Returns SyntaxError
as.syntaxError('error'); // Throws TypeError: String is not a(an) syntaxErrorTypeError
is.typeError(value) -> true | false
as.typeError(value) -> value | TypeError: [get.type(value)] is not a(an) typeErrorDescription:
Checks if the provided argument is a TypeError.
is.typeError(arg):
Returns
trueifargis a TypeError.Returns
falseotherwise.
as.typeError(arg):
Returns
argif it is a TypeError.Throws
TypeErrorifargis not a TypeError.
Example:
is.typeError(new TypeError()); // Returns true
is.typeError('error'); // Returns false
as.typeError(new TypeError()); // Returns TypeError
as.typeError('error'); // Throws TypeError: String is not a(an) typeErrorAny
is.any(value) -> true
as.any(value) -> valueDescription:
Checks if the provided argument is any value.
is.any(arg):
Returns
trueifargis any value.Returns
falseotherwise.
as.any(arg):
Returns
argif it is any value.Throws
TypeErrorifargis not any value.
Example:
is.any('hello'); // Returns true
is.any(123); // Returns true
as.any('hello'); // Returns 'hello'
as.any(123); // Returns 123Enum type
Enum type Basic
Enum['name of stored enum']('enum object here')Enum type Basic usage
Use increment
Enum.color({
RED: 0,
GREEN: Enum.inc,
BLUE: Enum.inc,
});
// Enum {
// '0': 'RED',
// '1': 'GREEN',
// '2': 'BLUE',
// RED: 0,
// GREEN: 1,
// BLUE: 2
// }
as.color('RED');// -> 'RED'
as.color('RED2');// -> TypeError String is not a member of color enumUse decrement
Enum.house({
ROOF: 2,
FLOOR: Enum.dec,
BASEMENT: Enum.dec,
});
// Enum {
// '0': 'BASEMENT',
// '1': 'FLOOR',
// '2': 'ROOF',
// ROOF: 2,
// FLOOR: 1,
// BASEMENT: 0
// }Use both
Enum.colorAndHouse({
RED: 0,
GREEN: Enum.inc,
BLUE: Enum.inc,
ROOF: 6,
FLOOR: Enum.dec,
BASEMENT: Enum.dec,
});
// Enum {
// '0': 'RED',
// '1': 'GREEN',
// '2': 'BLUE',
// '4': 'BASEMENT',
// '5': 'FLOOR',
// '6': 'ROOF',
// RED: 0,
// GREEN: 1,
// BLUE: 2,
// ROOF: 6,
// FLOOR: 5,
// BASEMENT: 4
// }Use with step
Enum.color({
[Enum.step]: 10, // ['Enum.step'] the same but with a quotes
RED: Enum.inc,
GREEN: Enum.inc,
BLUE: Enum.inc,
});
// Enum {
// '10': 'RED',
// '20': 'GREEN',
// '30': 'BLUE',
// RED: 10,
// GREEN: 20,
// BLUE: 30
// }
const enumExample = Enum.house({
[Enum.step]: 10,
ROOF: Enum.dec,
FLOOR: 30,
BASEMENT: Enum.dec,
});
// Enum {
// '10': 'ROOF',
// '20': 'BASEMENT',
// '30': 'FLOOR',
// ROOF: 10,
// FLOOR: 30,
// BASEMENT: 20
// }Check the Enum type like this
as.Enum(enumExample) && as.enum(enumExample);Build-in validators
This list of validators works with a minimum set of methods.
Empty
is.empty(value) -> true | false
as.empty(value) -> value | TypeError: Value is not emptyDescription:
Checks if the provided argument is empty.
is.empty(arg):
Returns
trueifargis empty.Returns
falseotherwise.
as.empty(arg):
Returns
argif it is empty.Throws
TypeErrorifargis not empty.
Example:
const emptyArray = [];
is.empty(emptyArray); // Returns true
is.empty([1, 2, 3]); // Returns false
as.empty(emptyArray); // Returns emptyArray
as.empty([1, 2, 3]); // Throws TypeError: Value is not emptyNotEmpty
is.notEmpty(value) -> true | false
as.notEmpty(value) -> value | TypeError: Value is emptyDescription:
Checks if the provided argument is not empty.
is.notEmpty(arg):
Returns
trueifargis not empty.Returns
falseotherwise.
as.notEmpty(arg):
Returns
argif it is not empty.Throws
TypeErrorifargis empty.
Example:
const notEmptyArray = [1, 2, 3];
is.notEmpty(notEmptyArray); // Returns true
is.notEmpty([]); // Returns false
as.notEmpty(notEmptyArray); // Returns notEmptyArray
as.notEmpty([]); // Throws TypeError: Value is emptyJSON
is.json(value) -> true | false
as.json(value) -> value | TypeError: Value is not JSONDescription:
Checks if the provided argument is a valid JSON string.
is.json(arg):
Returns
trueifargis a valid JSON string.Returns
falseotherwise.
**
as.json(arg):**
Returns
argif it is a valid JSON string.Throws
TypeErrorifargis not a valid JSON string.
Example:
const jsonExample = JSON.stringify({ test: 'test' });
is.json(jsonExample); // Returns true
is.json('not json'); // Returns false
as.json(jsonExample); // Returns jsonExample
as.json('not json'); // Throws TypeError: Value is not JSONJSON5
is.json5(value) -> true | false
as.json5(value) -> value | TypeError: Value is not JSON5Description:
Checks if the provided argument is a valid JSON5 string.
is.json5(arg):
Returns
trueifargis a valid JSON5 string.Returns
falseotherwise.
as.json5(arg):
Returns
argif it is a valid JSON5 string.Throws
TypeErrorifargis not a valid JSON5 string.
Example:
const json5Example = '{property: "value"}';
is.json5(json5Example); // Returns true
is.json5('not json5'); // Returns false
as.json5(json5Example); // Returns json5Example
as.json5('not json5'); // Throws TypeError: Value is not JSON5Null
is.null(value) -> true | false
as.null(value) -> value | TypeError: Value is not nullDescription:
Checks if the provided argument is null.
is.null(arg):
Returns
trueifargis null.Returns
falseotherwise.
as.null(arg):
Returns
argif it is null.Throws
TypeErrorifargis not null.
Example:
const nullExample = null;
is.null(nullExample); // Returns true
is.null('not null'); // Returns false
as.null(nullExample); // Returns nullExample
as.null('not null'); // Throws TypeError: Value is not nullNumber Validators
Sure, I'll restructure the documentation as requested. Here is the first part of the updated documentation:
Zero
is.zero(value) -> true | false
as.zero(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is exactly zero.
is.zero(arg):
Returns
trueifargis0.Returns
falseotherwise.
as.zero(arg):
Returns
argif it is0.Throws
TypeErrorifargis not0.
Example:
is.zero(0); // Returns true
is.zero(5); // Returns false
as.zero(0); // Returns 0
as.zero(5); // Throws TypeError: Number is not a value that passed validationEven
is.even(value) -> true | false
as.even(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is an even number.
is.even(arg):
Returns
trueifargis even.Returns
falseotherwise.
as.even(arg):
Returns
argif it is even.Throws
TypeErrorifargis not even.
Example:
is.even(2); // Returns true
is.even(3); // Returns false
as.even(2); // Returns 2
as.even(3); // Throws TypeError: Number is not a value that passed validationOdd
is.odd(value) -> true | false
as.odd(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is an odd number.
is.odd(arg):
Returns
trueifargis odd.Returns
falseotherwise.
as.odd(arg):
Returns
argif it is odd.Throws
TypeErrorifargis not odd.
Example:
is.odd(1); // Returns true
is.odd(4); // Returns false
as.odd(1); // Returns 1
as.odd(4); // Throws TypeError: Number is not a value that passed validationPositive
is.positive(value) -> true | false
as.positive(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a positive number.
is.positive(arg):
Returns
trueifargis positive.Returns
falseotherwise.
as.positive(arg):
Returns
argif it is positive.Throws
TypeErrorifargis not positive.
Example:
is.positive(1.1); // Returns true
is.positive(-1.1); // Returns false
as.positive(1.1); // Returns 1.1
as.positive(-1.1); // Throws TypeError: Number is not a value that passed validationNegative
is.negative(value) -> true | false
as.negative(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a negative number.
is.negative(arg):
Returns
trueifargis negative.Returns
falseotherwise.
as.negative(arg):
Returns
argif it is negative.Throws
TypeErrorifargis not negative.
Example:
is.negative(-1.1); // Returns true
is.negative(1.1); // Returns false
as.negative(-1.1); // Returns -1.1
as.negative(1.1); // Throws TypeError: Number is not a value that passed validationPositive Integer
is.positiveInteger(value) -> true | false
as.positiveInteger(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a positive integer.
is.positiveInteger(arg):
Returns
trueifargis a positive integer.Returns
falseotherwise.
as.positiveInteger(arg):
Returns
argif it is a positive integer.Throws
TypeErrorifargis not a positive integer.
Example:
is.positiveInteger(1); // Returns true
is.positiveInteger(-1); // Returns false
as.positiveInteger(1); // Returns 1
as.positiveInteger(-1); // Throws TypeError: Number is not a value that passed validationNegative Integer
is.negativeInteger(value) -> true | false
as.negativeInteger(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a negative integer.
is.negativeInteger(arg):
Returns
trueifargis a negative integer.Returns
falseotherwise.
as.negativeInteger(arg):
Returns
argif it is a negative integer.Throws
TypeErrorifargis not a negative integer.
Example:
is.negativeInteger(-1); // Returns true
is.negativeInteger(1); // Returns false
as.negativeInteger(-1); // Returns -1
as.negativeInteger(1); // Throws TypeError: Number is not a value that passed validationFinite
is.isFinite(value) -> true | false
as.isFinite(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a finite number.
is.isFinite(arg):
Returns
trueifargis finite.Returns
falseotherwise.
as.isFinite(arg):
Returns
argif it is finite.Throws
TypeErrorifargis not finite.
Example:
is.isFinite(0); // Returns true
is.isFinite(Infinity); // Returns false
as.isFinite(0); // Returns 0
as.isFinite(Infinity); // Throws TypeError: Number is not a value that passed validationNaN
is.NaN(value) -> true | false
as.NaN(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is NaN (Not-a-Number).
is.NaN(arg):
Returns
trueifargis NaN.Returns
falseotherwise.
as.NaN(arg):
Returns
argif it is NaN.Throws
TypeErrorifargis not NaN.
Example:
is.NaN(NaN); // Returns true
is.NaN(0); // Returns false
as.NaN(NaN); // Returns NaN
as.NaN(0); // Throws TypeError: Number is not a value that passed validationBetween
is.between({ arg, min, max }) -> true | false
as.between({ arg, min, max }) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is between min and max values.
is.between({ arg, min, max }):
Returns
trueifargis betweenminandmax.Returns
falseotherwise.
as.between({ arg, min, max }):
Returns
argif it is betweenminandmax.Throws
TypeErrorifargis not betweenminandmax.
Example:
is.between({ arg: 5, min: 0, max: 10 }); // Returns true
is.between({ arg: 15, min: 0, max: 10 }); // Returns false
as.between({ arg: 5, min: 0, max: 10 }); // Returns 5
as.between({ arg: 15, min: 0, max: 10 }); // Throws TypeError: Number is not a value that passed validationGreater
is.greater({ arg, value }) -> true | false
as.greater({ arg, value }) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is greater than the specified value.
is.greater({ arg, value }):
Returns
trueifargis greater thanvalue.Returns `
false` otherwise.
as.greater({ arg, value }):
Returns
argif it is greater thanvalue.Throws
TypeErrorifargis not greater thanvalue.
Example:
is.greater({ arg: 15, value: 5 }); // Returns true
is.greater({ arg: 5, value: 15 }); // Returns false
as.greater({ arg: 15, value: 5 }); // Returns 15
as.greater({ arg: 5, value: 15 }); // Throws TypeError: Number is not a value that passed validationLess
is.less({ arg, value }) -> true | false
as.less({ arg, value }) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is less than the specified value.
is.less({ arg, value }):
Returns
trueifargis less thanvalue.Returns
falseotherwise.
as.less({ arg, value }):
Returns
argif it is less thanvalue.Throws
TypeErrorifargis not less thanvalue.
Example:
is.less({ arg: 5, value: 15 }); // Returns true
is.less({ arg: 15, value: 5 }); // Returns false
as.less({ arg: 5, value: 15 }); // Returns 5
as.less({ arg: 15, value: 5 }); // Throws TypeError: Number is not a value that passed validationEqual or Greater
is.equalGreater({ arg, value }) -> true | false
as.equalGreater({ arg, value }) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is equal to or greater than the specified value.
is.equalGreater({ arg, value }):
Returns
trueifargis equal to or greater thanvalue.Returns
falseotherwise.
as.equalGreater({ arg, value }):
Returns
argif it is equal to or greater thanvalue.Throws
TypeErrorifargis not equal to or greater thanvalue.
Example:
is.equalGreater({ arg: 5, value: 5 }); // Returns true
is.equalGreater({ arg: 4, value: 5 }); // Returns false
as.equalGreater({ arg: 5, value: 5 }); // Returns 5
as.equalGreater({ arg: 4, value: 5 }); // Throws TypeError: Number is not a value that passed validationEqual or Less
is.equalLess({ arg, value }) -> true | false
as.equalLess({ arg, value }) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is equal to or less than the specified value.
is.equalLess({ arg, value }):
Returns
trueifargis equal to or less thanvalue.Returns
falseotherwise.
as.equalLess({ arg, value }):
Returns
argif it is equal to or less thanvalue.Throws
TypeErrorifargis not equal to or less thanvalue.
Example:
is.equalLess({ arg: 5, value: 5 }); // Returns true
is.equalLess({ arg: 6, value: 5 }); // Returns false
as.equalLess({ arg: 5, value: 5 }); // Returns 5
as.equalLess({ arg: 6, value: 5 }); // Throws TypeError: Number is not a value that passed validationMax
is.max({ arg, value }) -> true | false
as.max({ arg, value }) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is equal to the specified maximum value.
is.max({ arg, value }):
Returns
trueifargis equal to the maximumvalue.Returns
falseotherwise.
as.max({ arg, value }):
Returns
argif it is equal to the maximumvalue.Throws
TypeErrorifargis not equal to the maximumvalue.
Example:
is.max({ arg: 5, value: 5 }); // Returns true
is.max({ arg: 4, value: 5 }); // Returns false
as.max({ arg: 5, value: 5 }); // Returns 5
as.max({ arg: 4, value: 5 }); // Throws TypeError: Number is not a value that passed validationMin
is.min({ arg, value }) -> true | false
as.min({ arg, value }) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is equal to the specified minimum value.
is.min({ arg, value }):
Returns
trueifargis equal to the minimumvalue.Returns
falseotherwise.
as.min({ arg, value }):
Returns
argif it is equal to the minimumvalue.Throws
TypeErrorifargis not equal to the minimumvalue.
Example:
is.min({ arg: 5, value: 5 }); // Returns true
is.min({ arg: 6, value: 5 }); // Returns false
as.min({ arg: 5, value: 5 }); // Returns 5
as.min({ arg: 6, value: 5 }); // Throws TypeError: Number is not a value that passed validationMultiple
is.multiple({ arg, value }) -> true | false
as.multiple({ arg, value }) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a multiple of the specified value.
is.multiple({ arg, value }):
Returns
trueifargis a multiple ofvalue.Returns
falseotherwise.
as.multiple({ arg, value }):
Returns
argif it is a multiple ofvalue.Throws
TypeErrorifargis not a multiple ofvalue.
Example:
is.multiple({ arg: 15, value: 5 }); // Returns true
is.multiple({ arg: 14, value: 5 }); // Returns false
as.multiple({ arg: 15, value: 5 }); // Returns 15
as.multiple({ arg: 14, value: 5 }); // Throws TypeError: Number is not a value that passed validationPort
is.port(value) -> true | false
as.port(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a valid port number (between 0 and 65535).
is.port(arg):
Returns
trueifargis a valid port number.Returns
falseotherwise.
as.port(arg):
Returns
argif it is a valid port number.Throws
TypeErrorifargis not a valid port number.
Example:
is.port(80); // Returns true
is.port(70000); // Returns false
as.port(80); // Returns 80
as.port(70000); // Throws TypeError: Number is not a value that passed validationSafe Integer
is.safe(value) -> true | false
as.safe(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a safe integer (within the range of Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER).
is.safe(arg):
Returns
trueifargis a safe integer.Returns
falseotherwise.
as.safe(arg):
Returns
argif it is a safe integer.Throws
TypeErrorifargis not a safe integer.
Example:
is.safe(Number.MAX_SAFE_INTEGER); // Returns true
is.safe(Number.MAX_SAFE_INTEGER + 1); // Returns false
as.safe(Number.MAX_SAFE_INTEGER); // Returns Number.MAX_SAFE_INTEGER
as.safe(Number.MAX_SAFE_INTEGER + 1); // Throws TypeError: Number is not a value that passed validationPrecision
is.precision({ arg, value }) -> true | false
as.precision({ arg, value }) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument has the specified precision value.
is.precision({ arg, value }):
Returns `true
ifarghas the specified precisionvalue`.
Returns
falseotherwise.as.precision({ arg, value }):
Returns
argif it has the specified precisionvalue.Throws
TypeErrorifargdoes not have the specified precisionvalue.
Example:
is.precision({ arg: 5.22, value: 2 }); // Returns true
is.precision({ arg: 5.2, value: 2 }); // Returns false
as.precision({ arg: 5.22, value: 2 }); // Returns 5.22
as.precision({ arg: 5.2, value: 2 }); // Throws TypeError: Number is not a value that passed validationDigits
is.digits({ arg, value }) -> true | false
as.digits({ arg, value }) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument has the specified number of digits value.
is.digits({ arg, value }):
Returns
trueifarghas the specified number of digitsvalue.Returns
falseotherwise.
as.digits({ arg, value }):
Returns
argif it has the specified number of digitsvalue.Throws
TypeErrorifargdoes not have the specified number of digitsvalue.
Example:
is.digits({ arg: 12345, value: 5 }); // Returns true
is.digits({ arg: 1234, value: 5 }); // Returns false
as.digits({ arg: 12345, value: 5 }); // Returns 12345
as.digits({ arg: 1234, value: 5 }); // Throws TypeError: Number is not a value that passed validationISBN-10
is.ISBN10(value) -> true | false
as.ISBN10(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a valid ISBN-10 number.
is.ISBN10(arg):
Returns
trueifargis a valid ISBN-10 number.Returns
falseotherwise.
as.ISBN10(arg):
Returns
argif it is a valid ISBN-10 number.Throws
TypeErrorifargis not a valid ISBN-10 number.
Example:
is.ISBN10(1234567890); // Returns true
is.ISBN10(123456789); // Returns false
as.ISBN10(1234567890); // Returns 1234567890
as.ISBN10(123456789); // Throws TypeError: Number is not a value that passed validationISBN-13
is.ISBN13(value) -> true | false
as.ISBN13(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a valid ISBN-13 number.
is.ISBN13(arg):
Returns
trueifargis a valid ISBN-13 number.Returns
falseotherwise.
as.ISBN13(arg):
Returns
argif it is a valid ISBN-13 number.Throws
TypeErrorifargis not a valid ISBN-13 number.
Example:
is.ISBN13(1234567890123); // Returns true
is.ISBN13(123456789012); // Returns false
as.ISBN13(1234567890123); // Returns 1234567890123
as.ISBN13(123456789012); // Throws TypeError: Number is not a value that passed validationEAN
is.EAN(value) -> true | false
as.EAN(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a valid EAN (European Article Number).
is.EAN(arg):
Returns
trueifargis a valid EAN.Returns
falseotherwise.
as.EAN(arg):
Returns
argif it is a valid EAN.Throws
TypeErrorifargis not a valid EAN.
Example:
is.EAN(1234567890123); // Returns true
is.EAN(123456789012); // Returns false
as.EAN(1234567890123); // Returns 1234567890123
as.EAN(123456789012); // Throws TypeError: Number is not a value that passed validationSSN
is.SSN(value) -> true | false
as.SSN(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a valid SSN (Social Security Number).
is.SSN(arg):
Returns
trueifargis a valid SSN.Returns
falseotherwise.
as.SSN(arg):
Returns
argif it is a valid SSN.Throws
TypeErrorifargis not a valid SSN.
Example:
is.SSN(123456789); // Returns true
is.SSN(12345678); // Returns false
as.SSN(123456789); // Returns 123456789
as.SSN(12345678); // Throws TypeError: Number is not a value that passed validationVIN
is.VIN(value) -> true | false
as.VIN(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a valid VIN (Vehicle Identification Number).
is.VIN(arg):
Returns
trueifargis a valid VIN.Returns
falseotherwise.
as.VIN(arg):
Returns
argif it is a valid VIN.Throws
TypeErrorifargis not a valid VIN.
Example:
is.VIN(12345678901234567); // Returns true
is.VIN(1234567890123456); // Returns false
as.VIN(12345678901234567); // Returns 12345678901234567
as.VIN(1234567890123456); // Throws TypeError: Number is not a value that passed validationINN-10
is.INN10(value) -> true | false
as.INN10(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a valid INN (Individual Taxpayer Number) with 10 digits.
is.INN10(arg):
Returns
trueifargis a valid INN with 10 digits.Returns
falseotherwise.
as.INN10(arg):
Returns
argif it is a valid INN with 10 digits.Throws
TypeErrorifargis not a valid INN with 10 digits.
Example:
is.INN10(1234567890); // Returns true
is.INN10(123456789); // Returns false
as.INN10(1234567890); // Returns 1234567890
as.INN10(123456789); // Throws TypeError: Number is not a value that passed validationINN-12
is.INN12(value) -> true | false
as.INN12(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a valid INN (Individual Taxpayer Number) with 12 digits.
is.INN12(arg):
Returns
trueifargis a valid INN with 12 digits.Returns
falseotherwise.
as.INN12(arg):
Returns
argif it is a valid INN with 12 digits.Throws
TypeErrorifargis not a valid INN with 12 digits.
Example:
is.INN12(123456789012); // Returns true
is.INN12(12345678901); // Returns false
as.INN12(123456789012); // Returns 123456789012
as.INN12(12345678901); // Throws TypeError: Number is not a value that passed validationGLN
is.GLN(value) -> true | false
as.GLN(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a valid GLN (Global Location Number).
is.GLN(arg):
Returns
trueifargis
a valid GLN.
Returns
falseotherwise.as.GLN(arg):
Returns
argif it is a valid GLN.Throws
TypeErrorifargis not a valid GLN.
Example:
is.GLN(1234567890123); // Returns true
is.GLN(123456789012); // Returns false
as.GLN(1234567890123); // Returns 1234567890123
as.GLN(123456789012); // Throws TypeError: Number is not a value that passed validationIMEI
is.IMEI(value) -> true | false
as.IMEI(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a valid IMEI (International Mobile Equipment Identity).
is.IMEI(arg):
Returns
trueifargis a valid IMEI.Returns
falseotherwise.
as.IMEI(arg):
Returns
argif it is a valid IMEI.Throws
TypeErrorifargis not a valid IMEI.
Example:
is.IMEI(123456789012345); // Returns true
is.IMEI(12345678901234); // Returns false
as.IMEI(123456789012345); // Returns 123456789012345
as.IMEI(12345678901234); // Throws TypeError: Number is not a value that passed validationNPI
is.NPI(value) -> true | false
as.NPI(value) -> value | TypeError: Number is not a value that passed validationDescription:
Checks if the provided argument is a valid NPI (National Provider Identifier).
is.NPI(arg):
Returns
trueifargis a valid NPI.Returns
falseotherwise.
as.NPI(arg):
Returns
argif it is a valid NPI.Throws
TypeErrorifargis not a valid NPI.
Example:
is.NPI(1234567890); // Returns true
is.NPI(123456789); // Returns false
as.NPI(1234567890); // Returns 1234567890
as.NPI(123456789); // Throws TypeError: Number is not a value that passed validationString Validators
Here is the restructured documentation for the string validation methods:
Alphabetic
is.alphabetic(value) -> true | false
as.alphabetic(value) -> value | TypeError: String is not alphabeticDescription:
Checks if the provided argument is an alphabetic string.
is.alphabetic(arg):
Returns
trueifargcontains only alphabetic characters.Returns
falseotherwise.
as.alphabetic(arg):
Returns
argif it contains only alphabetic characters.Throws
TypeErrorifargdoes not contain only alphabetic characters.
Example:
is.alphabetic('hello'); // Returns true
is.alphabetic('hello123'); // Returns false
as.alphabetic('hello'); // Returns 'hello'
as.alphabetic('hello123'); // Throws TypeError: String is not alphabeticDigit
is.digit(value) -> true | false
as.digit(value) -> value | TypeError: String is not digitDescription:
Checks if the provided argument is an digit string.
is.digit(arg):
Returns
trueifargcontains only digit characters.Returns
falseotherwise.
as.digit(arg):
Returns
argif it contains only digit characters.Throws
TypeErrorifargdoes not contain only digit characters.
Example:
is.digit('123'); // Returns true
is.digit('hello123'); // Returns false
as.digit('123'); // Returns '123'
as.digit('hello123'); // Throws TypeError: String is not digitLowercase
is.lowercase(value) -> true | false
as.lowercase(value) -> value | TypeError: String is not lowercaseDescription:
Checks if the provided argument is a lowercase string.
is.lowercase(arg):
Returns
trueifargis a lowercase string.Returns
falseotherwise.
as.lowercase(arg):
Returns
argif it is a lowercase string.Throws
TypeErrorifargis not a lowercase string.
Example:
is.lowercase('hello'); // Returns true
is.lowercase('Hello'); // Returns false
as.lowercase('hello'); // Returns 'hello'
as.lowercase('Hello'); // Throws TypeError: String is not lowercaseUppercase
is.uppercase(value) -> true | false
as.uppercase(value) -> value | TypeError: String is not uppercaseDescription:
Checks if the provided argument is an uppercase string.
is.uppercase(arg):
Returns
trueifargis an uppercase string.Returns
falseotherwise.
as.uppercase(arg):
Returns
argif it is an uppercase string.Throws
TypeErrorifargis not an uppercase string.
Example:
is.uppercase('HELLO'); // Returns true
is.uppercase('Hello'); // Returns false
as.uppercase('HELLO'); // Returns 'HELLO'
as.uppercase('Hello'); // Throws TypeError: String is not uppercaseCamelCase
is.camelCase(value) -> true | false
as.camelCase(value) -> value | TypeError: String is not camelCaseDescription:
Checks if the provided argument is a camelCase string.
is.camelCase(arg):
Returns
trueifargis a camelCase string.Returns
falseotherwise.
as.camelCase(arg):
Returns
argif it is a camelCase string.Throws
TypeErrorifargis not a camelCase string.
Example:
is.camelCase('camelCase'); // Returns true
is.camelCase('CamelCase'); // Returns false
as.camelCase('camelCase'); // Returns 'camelCase'
as.camelCase('CamelCase'); // Throws TypeError: String is not camelCasesnakeCase
is.snakeCase(value) -> true | false
as.snakeCase(value) -> value | TypeError: String is not snakeCaseDescription:
Checks if the provided argument is a snakeCase string.
is.snakeCase(arg):
Returns
trueifargis a snakeCase string.Returns
falseotherwise.
as.snakeCase(arg):
Returns
argif it is a snakeCase string.Throws
TypeErrorifargis not a snakeCase string.
Example:
is.snakeCase('snake_Case'); // Returns true
is.snakeCase('SnakeCase'); // Returns false
as.snakeCase('snake_Case'); // Returns 'snakeCase'
as.snakeCase('SnakeCase'); // Throws TypeError: String is not snakeCaseKebab-Case
is.kebabCase(value) -> true | false
as.kebabCase(value) -> value | TypeError: String is not kebab-caseDescription:
Checks if the provided argument is a kebab-case string.
is.kebabCase(arg):
Returns
trueifargis a kebab-case string.Returns
falseotherwise.
as.kebabCase(arg):
Returns
argif it is a kebab-case string.Throws
TypeErrorifargis not a kebab-case string.
Example:
is.kebabCase('kebab-case'); // Returns true
is.kebabCase('KebabCase'); // Returns false
as.kebabCase('kebab-case'); // Returns 'kebab-case'
as.kebabCase('KebabCase'); // Throws TypeError: String is not kebab-caseTrain-Case
is.trainCase(value) -> true | false
as.trainCase(value) -> value | TypeError: String is not train-caseDescription:
Checks if the provided argument is a train-Case string.
is.trainCase(arg):
Returns
trueifargis a train-Case string.Returns
falseotherwise.
as.trainCase(arg):
Returns
argif it is a train-Case string.Throws
TypeErrorifargis not a train-Case string.
Example:
is.trainCase('Train-Case'); // Returns true
is.trainCase('train-case'); // Returns false
as.trainCase('Train-Case'); // Returns 'Train-Case'
as.trainCase('train-case'); // Throws TypeError: String is not train-casePath
is.path(value) -> true | false
as.path(value) -> value | TypeError: String is not a valid pathDescription:
Checks if the provided argument is a valid path.
is.path(arg):
Returns
trueifargis a valid path.Returns
falseotherwise.
as.path(arg):
Returns
argif it is a valid path.Throws
TypeErrorifargis not a valid path.
Example:
is.path('/user/home'); // Returns true
is.path('invalid//path'); // Returns false
as.path('/user/home'); // Returns '/user/home'
as.path('invalid//path'); // Throws TypeError: String is not a valid pathUUID
is.uuid(value) -> true | false
as.uuid(value) -> value | TypeError: String is not a valid UUIDDescription:
Checks if the provided argument is a valid UUID.
is.uuid(arg):
Returns
trueifargis a valid UUID.Returns
falseotherwise.
as.uuid(arg):
Returns
argif it is a valid UUID.Throws
TypeErrorifargis not a valid UUID.
Example:
is.uuid('123e4567-e89b-12d3-a456-426614174000'); // Returns true
is.uuid('invalid-uuid'); // Returns false
as.uuid('123e4567-e89b-12d3-a456-426614174000'); // Returns '123e4567-e89b-12d3-a456-426614174000'
as.uuid('invalid-uuid'); // Throws TypeError: String is not a valid UUIDHTTP URL
is.http(value) -> true | false
as.http(value) -> value | TypeError: String is not a valid HTTP URLDescription:
Checks if the provided argument is a valid HTTP URL.
is.http(arg):
Returns
trueifargis a valid HTTP URL.Returns
falseotherwise.
as.http(arg):
Returns
argif it is a valid HTTP URL.Throws
TypeErrorifargis not a valid HTTP URL.
Example:
is.http('http://example.com'); // Returns true
is.http('https://example.com'); // Returns false
as.http('http://example.com'); // Returns 'http://example.com'
as.http('https://example.com'); // Throws TypeError: String is not a valid HTTP URLHTTPS URL
is.https(value) -> true | false
as.https(value) -> value | TypeError: String is not a valid HTTPS URLDescription:
Checks if the provided argument is a valid HTTPS URL.
is.https(arg):
Returns
trueifargis a valid HTTPS URL.Returns
falseotherwise
.
as.https(arg):
Returns
argif it is a valid HTTPS URL.Throws
TypeErrorifargis not a valid HTTPS URL.
Example:
is.https('https://example.com'); // Returns true
is.https('http://example.com'); // Returns false
as.https('https://example.com'); // Returns 'https://example.com'
as.https('http://example.com'); // Throws TypeError: String is not a valid HTTPS URLURL
is.url(value) -> true | false
as.url(value) -> value | TypeError: String is not a valid URLDescription:
Checks if the provided argument is a valid URL.
is.url(arg):
Returns
trueifargis a valid URL.Returns
falseotherwise.
as.url(arg):
Returns
argif it is a valid URL.Throws
TypeErrorifargis not a valid URL.
Example:
is.url('https://example.com'); // Returns true
is.url('invalid-url'); // Returns false
as.url('https://example.com'); // Returns 'https://example.com'
as.url('invalid-url'); // Throws TypeError: String is not a valid URLEmail
is.email(value) -> true | false
as.email(value) -> value | TypeError: String is not a valid email addressDescription:
Checks if the provided argument is a valid email address.
is.email(arg):
Returns
trueifargis a valid email address.Returns
falseotherwise.
as.email(arg):
Returns
argif it is a valid email address.Throws
TypeErrorifargis not a valid email address.
Example:
is.email('user@example.com'); // Returns true
is.email('user@invalid'); // Returns false
as.email('user@example.com'); // Returns 'user@example.com'
as.email('user@invalid'); // Throws TypeError: String is not a valid email addressIPv4
is.ipv4(value) -> true | false
as.ipv4(value) -> value | TypeError: String is not a valid IPv4 addressDescription:
Checks if the provided argument is a valid IPv4 address.
is.ipv4(arg):
Returns
trueifargis a valid IPv4 address.Returns
falseotherwise.
as.ipv4(arg):
Returns
argif it is a valid IPv4 address.Throws
TypeErrorifargis not a valid IPv4 address.
Example:
is.ipv4('192.168.0.1'); // Returns true
is.ipv4('999.999.999.999'); // Returns false
as.ipv4('192.168.0.1'); // Returns '192.168.0.1'
as.ipv4('999.999.999.999'); // Throws TypeError: String is not a valid IPv4 addressIPv6
is.ipv6(value) -> true | false
as.ipv6(value) -> value | TypeError: String is not a valid IPv6 addressDescription:
Checks if the provided argument is a valid IPv6 address.
is.ipv6(arg):
Returns
trueifargis a valid IPv6 address.Returns
falseotherwise.
as.ipv6(arg):
Returns
argif it is a valid IPv6 address.Throws
TypeErrorifargis not a valid IPv6 address.
Example:
is.ipv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // Returns true
is.ipv6('invalid-ipv6'); // Returns false
as.ipv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // Returns '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
as.ipv6('invalid-ipv6'); // Throws TypeError: String is not a valid IPv6 addressIP
is.ip(value) -> true | false
as.ip(value) -> value | TypeError: String is not a valid IP addressDescription:
Checks if the provided argument is a valid IP address (either IPv4 or IPv6).
is.ip(arg):
Returns
trueifargis a valid IP address.Returns
falseotherwise.
as.ip(arg):
Returns
argif it is a valid IP address.Throws
TypeErrorifargis not a valid IP address.
Example:
is.ip('192.168.0.1'); // Returns true
is.ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // Returns true
is.ip('invalid-ip'); // Returns false
as.ip('192.168.0.1'); // Returns '192.168.0.1'
as.ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // Returns '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
as.ip('invalid-ip'); // Throws TypeError: String is not a valid IP addressFile Extension
is.fileExtension(value) -> true | false
as.fileExtension(value) -> value | TypeError: String is not a valid file extensionDescription:
Checks if the provided argument is a valid file extension.
is.fileExtension(arg):
Returns
trueifargis a valid file extension.Returns
falseotherwise.
as.fileExtension(arg):
Returns
argif it is a valid file extension.Throws
TypeErrorifargis not a valid file extension.
Example:
is.fileExtension('.txt'); // Returns true
is.fileExtension('txt'); // Returns false
as.fileExtension('.txt'); // Returns '.txt'
as.fileExtension('txt'); // Throws TypeError: String is not a valid file extensionHex Color
is.hexColor(value) -> true | false
as.hexColor(value) -> value | TypeError: String is not a valid hex colorDescription:
Checks if the provided argument is a valid hex color code.
is.hexColor(arg):
Returns
trueifargis a valid hex color code.Returns
falseotherwise.
as.hexColor(arg):
Returns
argif it is a valid hex color code.Throws
TypeErrorifargis not a valid hex color code.
Example:
is.hexColor('#ff00ff'); // Returns true
is.hexColor('ff00ff'); // Returns false
as.hexColor('#ff00ff'); // Returns '#ff00ff'
as.hexColor('ff00ff'); // Throws TypeError: String is not a valid hex colorBase64
is.base64(value) -> true | false
as.base64(value) -> value | TypeError: String is not a valid base64 stringDescription:
Checks if the provided argument is a valid base64 encoded string.
is.base64(arg):
Returns
trueifargis a valid base64 encoded string.Returns
falseotherwise.
as.base64(arg):
Returns
argif it is a valid base64 encoded string.Throws
TypeErrorifargis not a valid base64 encoded string.
Example:
is.base64('dGhpcyBpcyBhIHRlc3Q='); // Returns true
is.base64('invalid-base64'); // Returns false
as.base64('dGhpcyBpcyBhIHRlc3Q='); // Returns 'dGhpcyBpcyBhIHRlc3Q='
as.base64('invalid-base64'); // Throws TypeError: String is not a valid base64 stringData URL
is.dataURL(value) -> true | false
as.dataURL(value) -> value | TypeError: String is not a valid data URLDescription:
Checks if the provided argument is a valid data URL.
is.dataURL(arg):
Returns
trueifargis a valid data URL.Returns
falseotherwise.
as.dataURL(arg):
Returns
argif it is a valid data
URL.
Throws
TypeErrorifargis not a valid data URL.
Example:
is.dataURL('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA'); // Returns true
is.dataURL('invalid-data-url'); // Returns false
as.dataURL('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA'); // Returns 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA'
as.dataURL('invalid-data-url'); // Throws TypeError: String is not a valid data URLCredit Card
is.creditCard(value) -> true | false
as.creditCard(value) -> value | TypeError: String is not a valid credit card numberDescription:
Checks if the provided argument is a valid credit card number.
is.creditCard(arg):
Returns
trueifargis a valid credit card number.Returns
falseotherwise.
as.creditCard(arg):
Returns
argif it is a valid credit card number.Throws
TypeErrorifargis not a valid credit card number.
Example:
is.creditCard('4111111111111111'); // Returns true
is.creditCard('1234567890123456'); // Returns false
as.creditCard('4111111111111111'); // Returns '4111111111111111'
as.creditCard('1234567890123456'); // Throws TypeError: String is not a valid credit card numberMasterCard
is.masterCard(value) -> true | false
as.masterCard(value) -> value | TypeError: String is not a valid MasterCard numberDescription:
Checks if the provided argument is a valid MasterCard number.
is.masterCard(arg):
Returns
trueifargis a valid MasterCard number.Returns
falseotherwise.
as.masterCard(arg):
Returns
argif it is a valid MasterCard number.Throws
TypeErrorifargis not a valid MasterCard number.
Example:
is.masterCard('5555555555554444'); // Returns true
is.masterCard('1234567890123456'); // Returns false
as.masterCard('5555555555554444'); // Returns '5555555555554444'
as.masterCard('1234567890123456'); // Throws TypeError: String is not a valid MasterCard numberVisa
is.visa(value) -> true | false
as.visa(value) -> value | TypeError: String is not a valid Visa numberDescription:
Checks if the provided argument is a valid Visa number.
is.visa(arg):
Returns
trueifargis a valid Visa number.Returns
falseotherwise.
as.visa(arg):
Returns
argif it is a valid Visa number.Throws
TypeErrorifargis not a valid Visa number.
Example:
is.visa('4111111111111111'); // Returns true
is.visa('1234567890123456'); // Returns false
as.visa('4111111111111111'); // Returns '4111111111111111'
as.visa('1234567890123456'); // Throws TypeError: String is not a valid Visa numberAmerican Express
is.americanExpress(value) -> true | false
as.americanExpress(value) -> value | TypeError: String is not a valid American Express numberDescription:
Checks if the provided argument is a valid American Express number.
is.americanExpress(arg):
Returns
trueifargis a valid American Express number.Returns
falseotherwise.
as.americanExpress(arg):
Returns
argif it is a valid American Express number.Throws
TypeErrorifargis not a valid American Express number.
Example:
is.americanExpress('378282246310005'); // Returns true
is.americanExpress('123456789012345'); // Returns false
as.americanExpress('378282246310005'); // Returns '378282246310005'
as.americanExpress('123456789012345'); // Throws TypeError: String is not a valid American Express numberDiners Club
is.dinersClub(value) -> true | false
as.dinersClub(value) -> value | TypeError: String is not a valid Diners Club numberDescription:
Checks if the provided argument is a valid Diners Club number.
is.dinersClub(arg):
Returns
trueifargis a valid Diners Club number.Returns
falseotherwise.
as.dinersClub(arg):
Returns
argif it is a valid Diners Club number.Throws
TypeErrorifargis not a valid Diners Club number.
Example:
is.dinersClub('30569309025904'); // Returns true
is.dinersClub('12345678901234'); // Returns false
as.dinersClub('30569309025904'); // Returns '30569309025904'
as.dinersClub('12345678901234'); // Throws TypeError: String is not a valid Diners Club numberDomain
is.domain(value) -> true | false
as.domain(value) -> value | TypeError: String is not a valid domainDescription:
Checks if the provided argument is a valid domain.
is.domain(arg):
Returns
trueifargis a valid domain.Returns
falseotherwise.
as.domain(arg):
Returns
argif it is a valid domain.Throws
TypeErrorifargis not a valid domain.
Example:
is.domain('example.com'); // Returns true
is.domain('invalid_domain'); // Returns false
as.domain('example.com'); // Returns 'example.com'
as.domain('invalid_domain'); // Throws TypeError: String is not a valid domainGUID
is.guid(value) -> true | false
as.guid(value) -> value | TypeError: String is not a valid GUIDDescription:
Checks if the provided argument is a valid GUID.
is.guid(arg):
Returns
trueifargis a valid GUID.Returns
falseotherwise.
as.guid(arg):
Returns
argif it is a valid GUID.Throws
TypeErrorifargis not a valid GUID.
Example:
is.guid('123e4567-e89b-12d3-a456-426614174000'); // Returns true
is.guid('invalid_guid'); // Returns false
as.guid('123e4567-e89b-12d3-a456-426614174000'); // Returns '123e4567-e89b-12d3-a456-426614174000'
as.guid('invalid_guid'); // Throws TypeError: String is not a valid GUIDHostname
is.hostname(value) -> true | false
as.hostname(value) -> value | TypeError: String is not a valid hostnameDescription:
Checks if the provided argument is a valid hostname.
is.hostname(arg):
Returns
trueifargis a valid hostname.Returns
falseotherwise.
as.hostname(arg):
Returns
argif it is a valid hostname.Throws
TypeErrorifargis not a valid hostname.
Example:
is.hostname('example.com'); // Returns true
is.hostname('invalid_hostname'); // Returns false
as.hostname('example.com'); // Returns 'example.com'
as.hostname('invalid_hostname'); // Throws TypeError: String is not a valid hostnameISO Date
is.isoDate(value) -> true | false
as.isoDate(value) -> value | TypeError: String is not a valid ISO dateDescription:
Checks if the provided argument is a valid ISO date.
is.isoDate(arg):
Returns
trueifargis a valid ISO date.Returns
falseotherwise.
as.isoDate(arg):
Returns
argif it is a valid ISO date.Throws
TypeErrorifargis not a valid ISO date.
Example:
is.isoDate('2021-12-03T10:15:30Z'); // Returns true
is.isoDate('invalid_date'); // Returns false
as.isoDate('2021-12-03T10:15:30Z'); // Returns '2021-12-03T10:15:30Z'
as.isoDate('invalid_date'); // Throws TypeError: String is not a valid ISO date
ISO Duration
is.isoDuration(value) -> true | false
as.isoDuration(value) -> value | TypeError: String is not a valid ISO durationDescription:
Checks if the provided argument is a valid ISO duration.
is.isoDuration(arg):
Returns
trueifargis a valid ISO duration.Returns
falseotherwise.
as.isoDuration(arg):
Returns
argif it is a valid ISO duration.Throws
TypeErrorifargis not a valid ISO duration.
Example:
is.isoDuration('P3Y6M4DT12H30M5S'); // Returns true
is.isoDuration('invalid_duration'); // Returns false
as.isoDuration('P3Y6M4DT12H30M5S'); // Returns 'P3Y6M4DT12H30M5S'
as.isoDuration('invalid_duration'); // Throws TypeError: String is not a valid ISO durationJWT
is.jwt(value) -> true | false
as.jwt(value) -> value | TypeError: String is not a valid JWTDescription:
Checks if the provided argument is a valid JWT.
is.jwt(arg):
Returns
trueifargis a valid JWT.Returns
falseotherwise.
as.jwt(arg):
Returns
argif it is a valid JWT.Throws
TypeErrorifargis not a valid JWT.
Example:
is.jwt('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'); // Returns true
is.jwt('invalid_jwt'); // Returns false
as.jwt('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'); // Returns the JWT
as.jwt('invalid_jwt'); // Throws TypeError: String is not a valid JWTEmoji
is.emoji(value) -> true | false
as.emoji(value) -> value | TypeError: String is not a valid emojiDescription:
Checks if the provided argument is a valid emoji.
is.emoji(arg):
Returns
trueifargis a valid emoji.Returns
falseotherwise.
as.emoji(arg):
Returns
argif it is a valid emoji.Throws
TypeErrorifargis not a valid emoji.
Example:
is.emoji('😊'); // Returns true
is.emoji('not_emoji'); // Returns false
as.emoji('😊'); // Returns '😊'
as.emoji('not_emoji'); // Throws TypeError: String is not a valid emojiNanoid
is.nanoid(value) -> true | false
as.nanoid(value) -> value | TypeError: String is not a valid nanoidDescription:
Checks if the provided argument is a valid nanoid.
is.nanoid(arg):
Returns
trueifargis a valid nanoid.Returns
falseotherwise.
as.nanoid(arg):
Returns
argif it is a valid nanoid.Throws
TypeErrorifargis not a valid nanoid.
Example:
is.nanoid('V1StGXR8_Z5jdHi6B-myT'); // Returns true
is.nanoid('invalid_nanoid'); // Returns false
as.nanoid('V1StGXR8_Z5jdHi6B-myT'); // Returns 'V1StGXR8_Z5jdHi6B-myT'
as.nanoid('invalid_nanoid'); // Throws TypeError: String is not a valid nanoidCUID
is.cuid(value) -> true | false
as.cuid(value) -> value | TypeError: String is not a valid CUIDDescription:
Checks if the provided argument is a valid CUID.
is.cuid(arg):
Returns
trueifargis a valid CUID.Returns
falseotherwise.
as.cuid(arg):
Returns
argif it is a valid CUID.Throws
TypeErrorifargis not a valid CUID.
Example:
is.cuid('cjld2cjxh0000qzrmn831i7rn'); // Returns true
is.cuid('invalid_cuid'); // Returns false
as.cuid('cjld2cjxh0000qzrmn831i7rn'); // Returns 'cjld2cjxh0000qzrmn831i7rn'
as.cuid('invalid_cuid'); // Throws TypeError: String is not a valid CUIDCUID2
is.cuid2(value) -> true | false
as.cuid2(value) -> value | TypeError: String is not a valid CUID2Description:
Checks if the provided argument is a valid CUID2.
is.cuid2(arg):
Returns
trueifargis a valid CUID2.Returns
falseotherwise.
as.cuid2(arg):
Returns
argif it is a valid CUID2.Throws
TypeErrorifargis not a valid CUID2.
Example:
is.cuid2('c9g2h0a8'); // Returns true
is.cuid2('invalid_cuid2'); // Returns false
as.cuid2('c9g2h0a8'); // Returns 'c9g2h0a8'
as.cuid2('invalid_cuid2'); // Throws TypeError: String is not a valid CUID2Excludes
is.excludes({ arg: string, value: string }) -> true | false
as.excludes({ arg: string, value: string }) -> value | TypeError: String includes the excluded valueDescription:
Checks if the provided argument excludes a given substring.
is.excludes({ arg, value }):
Returns
trueifargexcludesvalue.Returns
falseotherwise.
as.excludes({ arg, value }):
Returns
argif it excludesvalue.Throws
TypeErrorifargincludesvalue.
Example:
is.excludes({ arg: 'hello world', value: 'test' }); // Returns true
is.excludes({ arg: 'hello world', value: 'world' }); // Returns false
as.excludes({ arg: 'hello world', value: 'test' }); // Returns 'hello world'
as.excludes({ arg: 'hello world', value: 'world' }); // Throws TypeError: String includes the excluded valueTime (HH:MM:SS)
is.time(value) -> true | false
as.time(value) -> value | TypeError: String is not a valid timeDescription:
Checks if the provided argument is a valid time in the format HH:MM:SS.
is.time(arg):
Returns
trueifargis a valid time.Returns
falseotherwise.
as.time(arg):
Returns
argif it is a valid time.Throws
TypeErrorifargis not a valid time.
Example:
is.time('12:34:56'); // Returns true
is.time('25:00:00'); // Returns false
as.time('12:34:56'); // Returns '12:34:56'
as.time('25:00:00'); // Throws TypeError: String is not a valid timeDateTime (YYYY-MM-DDTHH:MM:SS)
is.datetime(value) -> true | false
as.datetime(value) -> value | TypeError: String is not a valid datetimeDescription:
Checks if the provided argument is a valid datetime in the format YYYY-MM-DDTHH:MM:SS.
is.datetime(arg):
Returns
trueifargis a valid datetime.Returns
falseotherwise.
as.datetime(arg):
Returns `
arg` if it is a valid datetime.
Throws
TypeErrorifargis not a valid datetime.
Example:
is.datetime('2018-01-04T05:52:20'); // Returns true
is.datetime('invalid_datetime'); // Returns false
as.datetime('2018-01-04T05:52:20'); // Returns '2018-01-04T05:52:20'
as.datetime('invalid_datetime'); // Throws TypeError: String is not a valid datetimeDate (YYYY-MM-DD)
is.date(value) -> true | false
as.date(value) -> value | TypeError: String is not a valid dateDescription:
Checks if the provided argument is a valid date in the format YYYY-MM-DD.
is.date(arg):
Returns
trueifargis a valid date.Returns
falseotherwise.
as.date(arg):
Returns
argif it is a valid date.Throws
TypeErrorifargis not a valid date.
Example:
is.date('2021-12-03'); // Returns true
is.date('invalid_date'); // Returns false
as.date('2021-12-03'); // Returns '2021-12-03'
as.date('invalid_date'); // Throws TypeError: String is not a valid dateSHA-256 Hash
is.hash(value) -> true | false
as.hash(value) -> value | TypeError: String is not a valid SHA-256 hashDescription:
Checks if the provided argument is a valid SHA-256 hash.
is.hash(arg):
Returns
trueifargis a valid SHA-256 hash.Returns
falseotherwise.
as.hash(arg):
Returns
argif it is a valid SHA-256 hash.Throws
TypeErrorifargis not a valid SHA-256 hash.
Example:
is.hash('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'); // Returns true
is.hash('invalid_hash'); // Returns false
as.hash('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'); // Returns 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
as.hash('invalid_hash'); // Throws TypeError: String is not a valid SHA-256 hashISO Time with Seconds
is.isoTimeSecond(value) -> true | false
as.isoTimeSecond(value) -> value | TypeError: String is not a valid ISO time with secondsDescription:
Checks if the provided argument is a valid ISO time with seconds.
is.isoTimeSecond(arg):
Returns
trueifargis a valid ISO time with seconds.Returns
falseotherwise.
as.isoTimeSecond(arg):
Returns
argif it is a valid ISO time with seconds.Throws
TypeErrorifargis not a valid ISO time with seconds.
Example:
is.isoTimeSecond('T12:34:56'); // Returns true
is.isoTimeSecond('invalid_time'); // Returns false
as.isoTimeSecond('T12:34:56'); // Returns 'T12:34:56'
as.isoTimeSecond('invalid_time'); // Throws TypeError: String is not a valid ISO time with secondsISO Timestamp
is.isoTimestamp(value) -> true | false
as.isoTimestamp(value) -> value | TypeError: String is not a valid ISO timestampDescription:
Checks if the provided argument is a valid ISO timestamp.
is.isoTimestamp(arg):
Returns
trueifargis a valid ISO timestamp.Returns
falseotherwise.
as.isoTimestamp(arg):
Returns
argif it is a valid ISO timestamp.Throws
TypeErrorifargis not a valid ISO timestamp.
Example:
is.isoTimestamp('2021-12-03T10:15:30Z'); // Returns true
is.isoTimestamp('invalid_timestamp'); // Returns false
as.isoTimestamp('2021-12-03T10:15:30Z'); // Returns '2021-12-03T10:15:30Z'
as.isoTimestamp('invalid_timestamp'); // Throws TypeError: String is not a valid ISO timestampISO Week
is.isoWeek(value) -> true | false
as.isoWeek(value) -> value | TypeError: String is not a valid ISO weekDescription:
Checks if the provided argument is a valid ISO week.
is.isoWeek(arg):
Returns
trueifargis a valid ISO week.Returns
falseotherwise.
as.isoWeek(arg):
Returns
argif it is a valid ISO week.Throws
TypeErrorifargis not a valid ISO week.
Example:
is.isoWeek('2021-W48'); // Returns true
is.isoWeek('invalid_week'); // Returns false
as.isoWeek('2021-W48'); // Returns '2021-W48'
as.isoWeek('invalid_week'); // Throws TypeError: String is not a valid ISO weekMAC Address
is.mac(value) -> true | false
as.mac(value) -> value | TypeError: String is not a valid MAC addressDescription:
Checks if the provided argument is a valid MAC address.
is.mac(arg):
Returns
trueifargis a valid MAC address.Returns
falseotherwise.
as.mac(arg):
Returns
argif it is a valid MAC address.Throws
TypeErrorifargis not a valid MAC address.
Example:
is.mac('00:1B:44:11:3A:B7'); // Returns true
is.mac('invalid_mac'); // Returns false
as.mac('00:1B:44:11:3A:B7'); // Returns '00:1B:44:11:3A:B7'
as.mac('invalid_mac'); // Throws TypeError: String is not a valid MAC addressMAC-48 Address
is.mac48(value) -> true | false
as.mac48(value) -> value | TypeError: String is not a valid MAC-48 addressDescription:
Checks if the provided argument is a valid MAC-48 address.
is.mac48(arg):
Returns
trueifargis a valid MAC-48 address.Returns
falseotherwise.
as.mac48(arg):
Returns
argif it is a valid MAC-48 address.Throws
TypeErrorifargis not a valid MAC-48 address.
Example:
is.mac48('00-1B-44-11-3A-B7'); // Returns true
is.mac48('invalid_mac'); // Returns false
as.mac48('00-1B-44-11-3A-B7'); // Returns '00-1B-44-11-3A-B7'
as.mac48('invalid_mac'); // Throws TypeError: String is not a valid MAC-48 addressMAC-64 Address
is.mac64(value) -> true | false
as.mac64(value) -> value | TypeError: String is not a valid MAC-64 addressDescription:
Checks if the provided argument is a valid MAC-64 address.
is.mac64(arg):
Returns
trueifargis a valid MAC-64 address.Returns
falseotherwise.
as.mac64(arg):
Returns
argif it is a valid MAC-64 address.Throws
TypeErrorifargis not a valid MAC-64 address.
Example:
is.mac64('00-1B-44-11-3A-B7-44-11'); // Returns true
is.mac64('invalid_mac'); // Returns false
as.mac64('00-1B-44-11-3A-B7-44-11'); // Returns '00-1B-44-11-3A-B7-44-11'
as.mac64('invalid_mac'); // Throws TypeError: String is not a valid MAC-64 addressPast Date
is.past(value) -> true | false
as.past(value) -> value | TypeError: Date is not in the pastDescription:
Checks if the provided argument is a date in the past.
is.past(arg):
Returns
trueif `
arg` is a date in the past.
Returns
falseotherwise.as.past(arg):
Returns
argif it is a date in the past.Throws
TypeErrorifargis not a date in the past.
Example:
is.past('2020-12-03T10:15:30Z'); // Returns true
is.past('2099-12-03T10:15:30Z'); // Returns false
as.past('2020-12-03T10:15:30Z'); // Returns '2020-12-03T10:15:30Z'
as.past('2099-12-03T10:15:30Z'); // Throws TypeError: Date is not in the pastFuture Date
is.future(value) -> true | false
as.future(value) -> value | TypeError: Date is not in the futureDescription:
Checks if the provided argument is a date in the future.
is.future(arg):
Returns
trueifargis a date in the future.Returns
falseotherwise.
as.future(arg):
Returns
argif it is a date in the future.Throws
TypeErrorifargis not a date in the future.
Example:
is.future('2099-12-03T10:15:30Z'); // Returns true
is.future('2020-12-03T10:15:30Z'); // Returns false
as.future('2099-12-03T10:15:30Z'); // Returns '2099-12-03T10:15:30Z'
as.future('2020-12-03T10:15:30Z'); // Throws TypeError: Date is not in the futureASCII String
is.ascii(value) -> true | false
as.ascii(value) -> value | TypeError: String is not an ASCII stringDescription:
Checks if the provided argument is an ASCII string.
is.ascii(arg):
Returns
trueifargis an ASCII string.Returns
falseotherwise.
as.ascii(arg):
Returns
argif it is an ASCII string.Throws
TypeErrorifargis not an ASCII string.
Example:
is.ascii('hello'); // Returns true
is.ascii('こんにちは'); // Returns false
as.ascii('hello'); // Returns 'hello'
as.ascii('こんにちは'); // Throws TypeError: String is not an ASCII stringBase32
is.base32(value) -> true | false
as.base32(value) -> value | TypeError: String is not a valid Base32 stringDescription:
Checks if the provided argument is a valid Base32 encoded string.
is.base32(arg):
Returns
trueifargis a valid Base32 encoded string.Returns
falseotherwise.
as.base32(arg):
Returns
argif it is a valid Base32 encoded string.Throws
TypeErrorifargis not a valid Base32 encoded string.
Example:
is.base32('MZXW6YTBOI======'); // Returns true
is.base32('invalid_base32'); // Returns false
as.base32('MZXW6YTBOI======'); // Returns 'MZXW6YTBOI======'
as.base32('invalid_base32'); // Throws TypeError: String is not a valid Base32 stringBase58
is.base58(value) -> true | false
as.base58(value) -> value | TypeError: String is not a valid Base58 stringDescription:
Checks if the provided argument is a valid Base58 encoded string.
is.base58(arg):
Returns
trueifargis a valid Base58 encoded string.Returns
falseotherwise.
as.base58(arg):
Returns
argif it is a valid Base58 encoded string.Throws
TypeErrorifargis not a valid Base58 encoded string.
Example:
is.base58('3mJr7AoUXx2Wqd'); // Returns true
is.base58('invalid_base58'); // Returns false
as.base58('3mJr7AoUXx2Wqd'); // Returns '3mJr7AoUXx2Wqd'
as.base58('invalid_base58'); // Throws TypeError: String is not a valid Base58 stringDate Before Specific Date
is.before({ arg: string, value: string }) -> true | false
as.before({ arg: string, value: string }) -> value | TypeError: Date is not before the specific dateDescription:
Checks if the provided argument is a date before a specific date.
is.before({ arg, value }):
Returns
trueifargis beforevalue.Returns
falseotherwise.
as.before({ arg, value }):
Returns
argif it is beforevalue.Throws
TypeErrorifargis not beforevalue.
Example:
is.before({ arg: '2021-12-03T10:15:30Z', value: '2022-01-01T00:00:00Z' }); // Returns true
is.before({ arg: '2023-01-01T00:00:00Z', value: '2022-01-01T00:00:00Z' }); // Returns false
as.before({ arg: '2021-12-03T10:15:30Z', value: '2022-01-01T00:00:00Z' }); // Returns '2021-12-03T10:15:30Z'
as.before({ arg: '2023-01-01T00:00:00Z', value: '2022-01-01T00:00:00Z' }); // Throws TypeError: Date is not before the specific dateDate After Specific Date
is.after({ arg: string, value: string }) -> true | false
as.after({ arg: string, value: string }) -> value | TypeError: Date is not after the specific dateDescription:
Checks if the provided argument is a date after a specific date.
is.after({ arg, value }):
Returns
trueifargis aftervalue.Returns
falseotherwise.
as.after({ arg, value }):
Returns
argif it is aftervalue.Throws
TypeErrorifargis not aftervalue.
Example:
is.after({ arg: '2023-01-01T00:00:00Z', value: '2022-01-01T00:00:00Z' }); // Returns true
is.after({ arg: '2021-12-03T10:15:30Z', value: '2022-01-01T00:00:00Z' }); // Returns false
as.after({ arg: '2023-01-01T00:00:00Z', value: '2022-01-01T00:00:00Z' }); // Returns '2023-01-01T00:00:00Z'
as.after({ arg: '2021-12-03T10:15:30Z', value: '2022-01-01T00:00:00Z' }); // Throws TypeError: Date is not after the specific dateMaximum String Length
is.maxStr({ arg: string, value: number }) -> true | false
as.maxStr({ arg: string, value: number }) -> value | TypeError: String length is not within the limitDescription:
Checks if the provided argument's length is less than or equal to a specific value.
is.maxStr({ arg, value }):
Returns
trueifarglength is less than or equal tovalue.Returns
falseotherwise.
as.maxStr({ arg, value }):
Returns
argif its length is less than or equal tovalue.Throws
TypeErrorifarglength is greater thanvalue.
Example:
is.maxStr({ arg: '1234567890', value: 10 }); // Returns true
is.maxStr({ arg: '12345678901', value: 10 }); // Returns false
as.maxStr({ arg: '1234567890', value: 10 }); // Returns '1234567890'
as.maxStr({ arg: '12345678901', value: 10 }); // Throws TypeError: String length is not within the limitMinimum String Length
is.minStr({ arg: string, value: number }) -> true | false
as.minStr
({ arg: string, value: number }) -> value | TypeError: String length is not sufficientDescription:
Checks if the provided argument's length is greater than or equal to a specific value.
is.minStr({ arg, value }):
Returns
trueifarglength is greater than or equal tovalue.Returns
falseotherwise.
as.minStr({ arg, value }):
Returns
argif its length is greater than or equal tovalue.Throws
TypeErrorifarglength is less thanvalue.
Example:
is.minStr({ arg: '123', value: 3 }); // Returns true
is.minStr({ arg: '12', value: 3 }); // Returns false
as.minStr({ arg: '123', value: 3 }); // Returns '123'
as.minStr({ arg: '12', value: 3 }); // Throws TypeError: String length is not sufficientHas Uppercase
is.hasUppercase(string) -> true | false
as.hasUppercase(string) -> string | TypeError: String must contain at least one uppercase letterDescription:
Checks if the provided string contains at least one uppercase letter (A-Z).
is.hasUppercase({ arg }):
Returns
trueif the string contains uppercase letters.Returns
falseotherwise.
as.hasUppercase({ arg }):
Returns the original string
argif it contains at least one uppercase letter.Throws
TypeError(e.g.,"String must contain at least one uppercase letter") otherwise.
Example:
is.hasUppercase({ arg: 'Hello' }); // Returns true
is.hasUppercase({ arg: 'hello' }); // Returns false
as.hasUppercase({ arg: 'Hello' }); // Returns 'Hello'
as.hasUppercase('Hello'); // Throws TypeError: String must contain at least one uppercase letterHas Lowercase
is.hasLowercase(string) -> true | false
as.hasLowercase(string) -> string | TypeError: String must contain at least one lowercase letterDescription:
Checks if the provided string contains at least one lowercase letter (a-z).
is.hasLowercase({ arg }):
Returns
trueif the string contains lowercase letters.Returns
falseotherwise.
as.hasLowercase({ arg }):
Returns the original string
argif contains at least one lowercase letter.Throws
TypeError(e.g.,"String must contain at least one lowercase letter") otherwise.
Example:
is.hasLowercase('Hello'); // Returns true
is.hasLowercase('HELLO'); // Returns false
as.hasLowercase('Hello'); // Returns 'Hello'
as.hasLowercase('Hello'); // Throws TypeError: String must contain at least one lowercase letterHas Digit
is.hasDigit(string) -> true | false
as.hasDigit(string) -> string | TypeError: String must contain at least one digitDescription:
Checks if the provided string contains at least one digit (0-9).
is.hasDigit({ arg }):
Returns
trueif the string has a digit.Returns
falseotherwise.
as.hasDigit({ arg }):
Returns the original string
argif string contains at least one digit.Throws
TypeError(e.g.,"String must contain at least one digit") otherwise.
Example:
is.hasDigit('Hello123'); // Returns true
is.hasDigit('Hello'); // Returns false
as.hasDigit('Hello123'); // Returns 'Hello123'
as.hasDigit('Hello'); // Throws TypeError: String must contain at least one digitHas Special Character
is.hasSpecialCharacter(string) -> true | false
as.hasSpecialCharacter(string) -> string | TypeError: String must contain at least one special characterDescription:
Checks if the provided string contains at least one special character (example, !@#$%^&*(),.?":{}|<>).
is.hasSpecialCharacter({ arg }):
Returns
truecontains at least one special character.Returns
falseotherwise.
as.hasSpecialCharacter({ arg }):
Returns the original string
arg, if it contains at least one special character.Throws
TypeError(e.g.,"String must contain at least one special character") otherwise.
Example:
is.hasSpecialCharacter('Hello!'); // Returns true
is.hasSpecialCharacter('Hello'); // Returns false
as.hasSpecialCharacter('Hello!'); // Returns 'Hello!'
as.hasSpecialCharacter('Hello'); // Throws TypeError: String must contain at least one special characterPassword
is.password(string) -> true | false
as.password(string) -> string | TypeError: String must meet password requirementsDescription:
Checks if the provided string meets all of the following requirements:
Contains at least one uppercase letter.
Contains at least one lowercase letter.
Contains at least one digit.
Contains at least one special character.
Has a minimum 8 characters.
is.password({ arg }):
Returns
truestring meets all of the following requirementsReturns
falseotherwise.
as.password({ arg }):
Returns
arg, if string meets all of the following requirements.Throws
TypeError(example,"String must meet password requirements") otherwise.
Example:
is.password('Hello1!'); // Returns true
is.password('Hello'); // Returns false
as.password('Hello1!'); // Returns 'Hello1!'
as.password('Hello'); // Throws TypeError: String must meet password requirementsSettings
To customize error messages, you can override Checker.errorMsg. To disable throwing errors, set checker.disabled = true.
Example:
const checker = new Checker();
checker.errorMsg = (params) => `${params[2] || (params[0]?.constructor ? params[0].constructor.name : params[0])} , really? I'm not sure that is a(an) ${params[1]}`;
const { multi, as, is } = checker;
checker.disabled = true;
// Example error message: TypeError: Number, really? I'm not sure that is a(an) stringUtility
A simple method to get the type of argument
get.type('any argument here'); // type of argument
get.type(43); // Number
get.type(Checker); // CheckerAliases
You can check the following types by "is", "as" or javascript, but this looks more readable
is.arguments(1); // true | false
as.arguments(1) // 1 | TypeError
// the same of
is.arrayObject(1); // true
is.generator(function*() { yield 0 }) // true | false
as.generator(function*() { yield 0 }) // function*() { yield 0 } | TypeError
// the same of
is.iterator(function*() { yield 0 }) // true | false
as.iterator(function*() { yield 0 }) // function*() { yield 0 } | TypeError
is.NodeJs() // true | false
as.NodeJs() // process | TypeError
// the same of
process !== undefined // true
is.browser() // true
as.browser() // navigator | TypeError
// the same of
navigator !== undefined // true
is.Chrome() // true
as.Chrome() // navigator | TypeError
// the same of
navigator.userAgent.includes('Chrome') // true
Micro-tests Basic usage
There are only five main methods: METHOD, PROPERTY, IS, passed and failed.
METHOD usage
METHOD strictly checks methods.
import { Checker, BaseInterface, MicroTest, Utility, MicroTest } from '@pro-script/as-is';
const checker = new Checker({ 'IF/ELSE/END': true, strict: true, Enum: true, utility: true});
const { multi, Interface, strict, as, is, IF, ELSE, END, optional, get } = checker;
const { START, STOP, FINISH, METHOD, PROPERTY, IS, passed, failed } = new MicroTest({ is, as });
METHOD.toString(''); // ✗ METHOD.toString failed
const object = {
foo: ()=> {}
};
METHOD.foo(object) // ✓ METHOD.foo passedPROPERTY usage
PROPERTY strictly checks properties.
import { Checker, BaseInterface, MicroTest, Utility, MicroTest } from '@pro-script/as-is';
const checker = new Checker({ 'IF/ELSE/END': true, strict: true, Enum: true, utility: true});
const { multi, Interface, strict, as, is, IF, ELSE, END, optional, get } = checker;
const { START, STOP, FINISH, METHOD, PROPERTY, IS, passed, failed } = new MicroTest({ is, as });
const object = {
foo: ()=> 'Hello world',
property1: 1
};
PROPERTY.foo(object) // ✗ PROPERTY.foo failed
PROPERTY.property1(object) // ✓ PROPERTY.property1 passedIS usage
The IS function is a wrapper of "is" method, but with four additional methods like: true, false, ok, notOk. You can check any type with IS, what can do "is".
import { Checker, BaseInterface, MicroTest, Utility, MicroTest } from '@pro-script/as-is';
const checker = new Checker({ 'IF/ELSE/END': true, strict: true, Enum: true, utility: true});
const { multi, Interface, strict, as, is, IF, ELSE, END, optional, get } = checker;
const { START, STOP, FINISH, METHOD, PROPERTY, IS, passed, failed } = new MicroTest({ is, as });
IS.string(''); // ✓ IS.string passed
IS.objectUndeinedStringNumber(2); // ✓ IS.objectUndeinedStringNumber passedAny testing framework has many methods and you should learn them before testing. Such as ().to.be.equal or assert.isOk() and more and that means a test development takes more time. Using the one percent improvement principle, you can reduce this time to a minimum by using only IS.true or IS.false and IS.ok or IS.notOk. Nothing else is needed for microtesting. When you need to test anything, you will use a different testing framework.
import { Checker, BaseInterface, MicroTest, Utility, MicroTest } from '@pro-script/as-is';
const checker = new Checker({ 'IF/ELSE/END': true, strict: true, Enum: true, utility: true});
const { multi, Interface, strict, as, is, IF, ELSE, END, optional, get } = checker;
const { START, STOP, FINISH, METHOD, PROPERTY, IS, passed, failed } = new MicroTest({ is, as });
const object = {
foo: ()=> 'Hello world',
property1: 1
};
IS.object(object);
IS.string(object.foo());
IS.number(object.property1);
IS.true(object.property1 >=1);
IS.false(object.property1 < 1);
IS.ok(object.foo().includes('world'));
IS.notOk(object.foo().includes('!'));passed and failed usage
This small functionality will be useful when you need to build your own testing scenario, but don't want to use IS for checking.
import { Checker, BaseInterface, MicroTest, Utility, MicroTest } from '@pro-script/as-is';
const checker = new Checker({ 'IF/ELSE/END': true, strict: true, Enum: true, utility: true});
const { multi, Interface, strict, as, is, IF, ELSE, END, optional, get } = checker;
const { START, STOP, FINISH, METHOD, PROPERTY, IS, passed, failed } = new MicroTest({ is, as });
(1 < 2) ? passed.one('Is a won'): failed.two('is a loss');IF/ELSE/END
These commands are an alias for the "is" command and are added to make the code easier to read.
IF/ELSE/END Basic usage
When you need to use a couple of variants of function or method calls, you can do the following
// Case 1
function someFunction(name, age, friends) {
console.log(name, age, friends);
}
// Case 2
function someFunction(name, friends) {
console.log(name, friends);
}
// Case 3
function someFunction(age, friends) {
console.log(age, friends);
}
// Case 4
function someFunction(friends) {
console.log(friends);
}
function someFunction(name, age, friends,
_= [as.stringNumberArray(name),
as.undefinedNumberArray(age),
as.undefinedArray(friends)
]) {
IF.string(name) && is.number(age) && is.array(friends)? (
as.array(_) && as.notEmpty(_)
):ELSE.string(name) && is.array(age)? (
friends = age,
age = undefined
):ELSE.number(name) && is.array(age)? (
friends = age,
age = name,
name = undefined
):ELSE.array(name)? (
friends = name,
name = undefined
):END;
console.log(`name: ${name}, age: ${age}, friends: ${friends}`);
}
someFunction('Rick', 25, ['Mike', 'Liza']);
// name: Rick, age: 25, friends: Mike,Liza
someFunction('Rick', ['Mike', 'Liza']);
// name: Rick, age: undefined, friends: Mike,Liza
someFunction(25, ['Mike', 'Liza']);
// name: undefined, age: 25, friends: Mike,Liza
someFunction(['Mike', 'Liza']);
// name: undefined, age: undefined, friends: Mike,Liza
Advanced techniques
Checking one repeated type
In object, array, set and map. All types ending in 's' will be checked.
is.strings(exampleObject) && as.strings(exampleObject);
is.Numbers(exampleArray) && as.Numbers(exampleArray);
is.Errors(exampleSet) && as.Errors(exampleSet);
is.BigInts(exampleMap) && as.BigInts(exampleMap); Strict typing
Basics
new Strict(type['js type here']`variable name`);// <-- meta programing magic
// after that
type['js type here']`variable name`;const strict = new Strict(type.string`name`);
strict.name = 'Stephen Hawking';Basic usage
import Checker from 'as-is';
const { multi, Strict, type, as, is } = new Checker();
//-- Do it once!
const strict = new Strict(
type.string`name`,
type.number`age`,
type.strings`pages`
);
//--
strict.name = 'Mike';
strict.age = 12;
strict.pages = ['pageOne', 'pageTwo'];
strict.bla = 1; // will not be assigned
console.log(strict.name, strict.age, strict.pages);
// Mike 12 [ 'pageOne', 'pageTwo' ]
strict.name = 2022;
// TypeError: Number is not a(an) stringUse strictObject.values() to get all values.
const values = strict.values();
console.log(values);
// { name: 'Mike', age: 12, pages: [ 'pageOne', 'pageTwo' ] }Once the strict instance has been created, you can do the following:
type.string`example`;
strict.example = '2';Strict has reserved variable names: get, set, values, types, variable, lastType. This means that you can do the following;
const strict = new Strict(type.null`get`);
//or
const strict = new Strict(type.undefined`set`);
//or
const strict = new Strict(type.array`values`);
//or
const strict = new Strict(type.object`variables`);
type.string`example`;
strict.example = '2';
// when you call strict.values()
// you only get { example: '2' }. Any of the reserved variable names will be hidden.Only one strict object in one file is possible, if you want to instantiate any other object, you will get a reference to the first one. This is because Strict must have access to the checker engine.
const strict = new Strict(type.null`set`);
type.string`example`;
const secondStrict = new Strict(type.null`get`);
secondStrict.example = '2';
console.log(strict.values());
// { example: '2' }Any tricks will not help you get the second strict object. Maybe I'll find a solution for this because I think it's a bug, not a feature :)
import Checker from '@pro-script/as-is';
const { multi, Strict, type, as, is } = new Checker();
type.string`example`;
strict.example = 'first';
// even second import
import { default as CEngine } from '@pro-script/as-is';
const { Strict: secondStrict } = new CEngine();
const secondStrict = new SecondStrict(type.null`get`);
secondStrict.string`example2`;
secondStrict.example2 = 'second';
secondStrict.values();
//{ example: 'first', example2: 'second' }Checking multiple types. It looks like generics in typescript, but more simple implementation.
When a variable is part of more than one type, you can also check for that.
Basic
is['couple js type here']('argument here'); // argument | false
as['couple js type here']('argument here'); // argument | TypeError
// as alias syntax
multi`couple js type here`('argument here'); // argument | TypeError
multi(['couple js type here'])('argument here'); // argument | TypeError
Basic usage
as.NumberStringBoolean(2022);
as.Number_String_Boolean_Symbol_Function_BigInt_Array([]);
as['Number|String|Boolean']('text');
as['Number-String-Boolean'](true);
as['Number or String or Boolean'](777);
as['it can be any text with type definition like String or Number'](111);
multi`Number|String|Boolean`(2022);
multi(['Number|String|Boolean'])(true);
const multiType = 'Number|String|Boolean';
as[multiType]({});
// TypeError: Object is not a(an) Number|String|BooleanMacros
To manage syntax and write human-readable code, you might need metaprogramming tools such as macros. When using the architectural pattern of Domain-Driven Design (DDD), you may need to perform a series of validations or type checks for a domain entity. These checks are often required repeatedly, resulting in code that is difficult to read. You could, of course, create your own type or interface, but what do you do when you need to perform some action in addition to type checking and validation that is part of the business logic? In such cases, you can use macros to hide the technical checks under the hood, and the name of the macro will explain what is happening in the domain language.
Basic
[callback1, callback2, ... callbackN].macro is starting array of callbacks. That's it. When [].macro found an object it start his method by name.
Basic usage
callbacks example
[
()=> console.log('callback1'),
()=> console.log('callback2'),
()=> console.log('callbackN')
].macro; // callback1\n callback2\n callbackN\nObject example
[{
first:()=> console.log('first'),
second:()=> console.log('second')
}].macro;
['first', 'second'].macro; // first\n second
['first', 'second'].macro; // first\n second
['first', 'second'].macro; // first\n secondObjects method returns a callback
[{
'domain description':()=> function () { console.log('action') }
}].macro;
const domainAction = ['domain description'].macro;
domainAction.macro; // action
domainAction.macro; // action
domainAction.macro; // actionOnly when the array is contain an object or a callback you can repeat the macro.
const empty1 = [()=> console.log('callback1')].macro;
const empty2 = ['first', 'second'].macro;
const notEmpty = [function (){ return ()=> console.log('This function is returning a callback')}].macro;
empty1.macro; // error
empty2.macro; // error
notEmpty.macro; // This function is returning a callbackWeird syntax
If you need to make something weird or unusual you can try this working syntax.
const macroObject = {
'this.is.a.first.macro': ()=> 'macro value',
'/this.is.a.second macro/': ()=> 2,
'this is a third macro': ()=> console.log('console from third macro'),
0xDad:()=> console.log('any number can be macro')
};
[macroObject].macro;
[
_ => this.is.a.first.macro,
/this.is.a.second macro/,
'this is a third macro',
0xDad
].macro; // console from third macro\n any number can be macro
Possible Errors
If you encounter the following error:
TypeError: Cannot read properties of undefined (reading '#<Object>')Ensure you have a semicolon ; at the end of the previous line.
No Dependencies
This library has no external dependencies.
Last updated