README

Pro-script Library Documentation

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 mandatory

This 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

The Check password added to strings validator.

Overview

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

For Browsers

Without module:

With module:

Usage

Node.js (ESM)

Node.js (CommonJS)

Browser

Without module:

With module:

With import map:

in global scope

or

in global scope

After that you can use an as or is etc in other files without an import.

Everything in one code block

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

Strict type object:

Class checking:

  • [className]

  • [classInstance]

Interface Interfaces works only in the set way where IName = { * interface data * }, not like IName({ * interface data * })

Types Types works only in the apply way where TName({ * types data * }), not like interfaces TName = { * types data * }

Integration

You can integrate any feature you want.

Instancing

Minimal set of methods.

All methods without plugins.

All methods with plugins.

Type Checks

String

Description:

Checks if the provided argument is a string.

  • is.string(arg):

    • Returns true if arg is a string.

    • Returns false otherwise.

  • as.string(arg):

    • Returns arg if it is a string.

    • Throws TypeError if

arg is not a string.

Example:

Number

Description:

Checks if the provided argument is a number.

  • is.number(arg):

    • Returns true if arg is a number.

    • Returns false otherwise.

  • as.number(arg):

    • Returns arg if it is a number.

    • Throws TypeError if arg is not a number.

Example:

Boolean

Description:

Checks if the provided argument is a boolean.

  • is.boolean(arg):

    • Returns true if arg is a boolean.

    • Returns false otherwise.

  • as.boolean(arg):

    • Returns arg if it is a boolean.

    • Throws TypeError if arg is not a boolean.

Example:

Symbol

Description:

Checks if the provided argument is a symbol.

  • is.symbol(arg):

    • Returns true if arg is a symbol.

    • Returns false otherwise.

  • as.symbol(arg):

    • Returns arg if it is a symbol.

    • Throws TypeError if arg is not a symbol.

Example:

Function

Description:

Checks if the provided argument is a function.

  • is.function(arg):

    • Returns true if arg is a function.

    • Returns false otherwise.

  • as.function(arg):

    • Returns arg if it is a function.

    • Throws TypeError if arg is not a function.

Example:

BigInt

Description:

Checks if the provided argument is a BigInt.

  • is.bigInt(arg):

    • Returns true if arg is a BigInt.

    • Returns false otherwise.

  • as.bigInt(arg):

    • Returns arg if it is a BigInt.

    • Throws TypeError if arg is not a BigInt.

Example:

Array

Description:

Checks if the provided argument is an array.

  • is.array(arg):

    • Returns true if arg is an array.

    • Returns false otherwise.

  • as.array(arg):

    • Returns arg if it is an array.

    • Throws TypeError if arg is not an array.

Example:

Date

Description:

Checks if the provided argument is a date.

  • is.date(arg):

    • Returns true if arg is a date.

    • Returns false otherwise.

  • as.date(arg):

    • Returns arg if it is a date.

    • Throws TypeError if arg is not a date.

Example:

Object

Description:

Checks if the provided argument is an object.

  • is.object(arg):

    • Returns true if arg is an object.

    • Returns false otherwise.

  • as.object(arg):

    • Returns arg if it is an object.

    • Throws TypeError if arg is not an object.

Example:

Set

Description:

Checks if the provided argument is a set.

  • is.set(arg):

    • Returns true if arg is a set.

    • Returns false otherwise.

  • as.set(arg):

    • Returns arg if it is a set.

    • Throws TypeError if arg is not a set.

Example:

Map

Description:

Checks if the provided argument is a map.

  • is.map(arg):

    • Returns true if arg is a map.

    • Returns false otherwise.

  • as.map(arg):

    • Returns arg if it is a map.

    • Throws TypeError if arg is not a map.

Example:

WeakSet

Description:

Checks if the provided argument is a WeakSet.

  • is.weakSet(arg):

    • Returns true if arg is a WeakSet.

    • Returns false otherwise.

  • as.weakSet(arg):

    • Returns arg if it is a WeakSet.

    • Throws TypeError if arg is not a WeakSet.

Example:

WeakMap

Description:

Checks if the provided argument is a WeakMap.

  • is.weakMap(arg):

    • Returns true if arg is a WeakMap.

    • Returns false otherwise.

  • as.weakMap(arg):

    • Returns arg if it is a WeakMap.

    • Throws TypeError if arg is not a WeakMap.

Example:

RegExp

Description:

Checks if the provided argument is a regular expression.

  • is.regExp(arg):

    • Returns true if arg is a regular expression.

    • Returns false otherwise.

  • as.regExp(arg):

    • Returns arg if it is a regular expression.

    • Throws TypeError if arg is not a regular expression.

Example:

Promise

Description:

Checks if the provided argument is a Promise.

  • is.promise(arg):

    • Returns true if arg is a Promise.

    • Returns false otherwise.

  • as.promise(arg):

    • Returns arg if it is a Promise.

    • Throws TypeError if arg is not a Promise.

Example:

Generator

Description:

Checks if the provided argument is a Generator.

  • is.generator(arg):

    • Returns true if arg is a Generator.

    • Returns false otherwise.

  • as.generator(arg):

    • Returns arg if it is a Generator.

    • Throws TypeError if arg is not a Generator.

Example:

Here is the documentation for the additional type checks:

TypedArray

Description:

Checks if the provided argument is a TypedArray.

  • is.typedArray(arg):

    • Returns true if arg is a TypedArray.

    • Returns false otherwise.

  • as.typedArray(arg):

    • Returns arg if it is a TypedArray.

    • Throws TypeError if arg is not a TypedArray.

Example:

Buffer

Description:

Checks if the provided argument is a Buffer.

  • is.buffer(arg):

    • Returns true if arg is a Buffer.

    • Returns false otherwise.

  • as.buffer(arg):

    • Returns arg if it is a Buffer.

    • Throws TypeError if arg is not a Buffer.

Example:

SharedArrayBuffer

Description:

Checks if the provided argument is a SharedArrayBuffer.

  • is.sharedArrayBuffer(arg):

    • Returns true if arg is a SharedArrayBuffer.

    • Returns false otherwise.

  • as.sharedArrayBuffer(arg):

    • Returns arg if it is a SharedArrayBuffer.

    • Throws TypeError if arg is not a SharedArrayBuffer.

Example:

Date

Description:

Checks if the provided argument is a Date object.

  • is.date(arg):

    • Returns true if arg is a Date object.

    • Returns false otherwise.

  • as.date(arg):

    • Returns arg if it is a Date object.

    • Throws TypeError if arg is not a Date object.

Example:

Object

Description:

Checks if the provided argument is an object.

  • is.object(arg):

    • Returns true if arg is an object.

    • Returns false otherwise.

  • as.object(arg):

    • Returns arg if it is an object.

    • Throws TypeError if arg is not an object.

Example:

Class

Description:

Checks if the provided argument is a class.

  • is.class(arg):

    • Returns true if arg is a class.

    • Returns false otherwise.

  • as.class(arg):

    • Returns arg if it is a class.

    • Throws TypeError if arg is not a class.

Example:

Instance

Description:

Checks if the provided argument is an instance of the specified class.

  • is.instance(arg, constructor):

    • Returns true if arg is an instance of constructor.

    • Returns false otherwise.

  • as.instance(arg, constructor):

    • Returns arg if it is an instance of constructor.

    • Throws TypeError if arg is not an instance of constructor.

Example:

Iterator

Description:

Checks if the provided argument is an iterator.

  • is.iterator(arg):

    • Returns true if arg is an iterator.

    • Returns false otherwise.

  • as.iterator(arg):

    • Returns arg if it is an iterator.

    • Throws TypeError if arg is not an iterator.

Example:

Undefined

It's a very specific type checking. To check if a value is exactly undefined.

Description:

Checks if the provided argument is undefined.

  • is.undefined(arg):

    • Returns true if arg is undefined.

    • Returns false otherwise.

  • as.undefined(arg):

    • Returns arg if it is undefined.

    • Throws TypeError if arg is not Example:

Nullish

Description:

Checks if the provided argument is null or undefined.

  • is.nullish(arg):

    • Returns true if arg is null or undefined.

    • Returns false otherwise.

  • as.nullish(arg):

    • Returns arg if it is null or undefined.

    • Throws TypeError if arg is not null or undefined.

Example:

Error

Description:

Checks if the provided argument is an Error.

  • is.error(arg):

    • Returns true if arg is an Error.

    • Returns false otherwise.

  • as.error(arg):

    • Returns arg if it is an Error.

    • Throws TypeError if arg is not an Error.

Example:

RangeError

Description:

Checks if the provided argument is a RangeError.

  • is.rangeError(arg):

    • Returns true if arg is a RangeError.

    • Returns false otherwise.

  • as.rangeError(arg):

    • Returns arg if it is a RangeError.

    • Throws TypeError if arg is not a RangeError.

Example:

ReferenceError

Description:

Checks if the provided argument is a ReferenceError.

  • is.referenceError(arg):

    • Returns true if arg is a ReferenceError.

    • Returns false otherwise.

  • as.referenceError(arg):

    • Returns arg if it is a ReferenceError.

    • Throws TypeError if arg is not a ReferenceError.

Example:

SyntaxError

Description:

Checks if the provided argument is a SyntaxError.

  • is.syntaxError(arg):

    • Returns true if arg is a SyntaxError.

    • Returns false otherwise.

  • as.syntaxError(arg):

    • Returns arg if it is a SyntaxError.

    • Throws TypeError if arg is not a SyntaxError.

Example:

TypeError

Description:

Checks if the provided argument is a TypeError.

  • is.typeError(arg):

    • Returns true if arg is a TypeError.

    • Returns false otherwise.

  • as.typeError(arg):

    • Returns arg if it is a TypeError.

    • Throws TypeError if arg is not a TypeError.

Example:

Any

Description:

Checks if the provided argument is any value.

  • is.any(arg):

    • Returns true if arg is any value.

    • Returns false otherwise.

  • as.any(arg):

    • Returns arg if it is any value.

    • Throws TypeError if arg is not any value.

Example:

Enum type

Enum type Basic

Enum type Basic usage

Use increment

Use decrement

Use both

Use with step

Check the Enum type like this

Build-in validators

This list of validators works with a minimum set of methods.

Empty

Description:

Checks if the provided argument is empty.

  • is.empty(arg):

    • Returns true if arg is empty.

    • Returns false otherwise.

  • as.empty(arg):

    • Returns arg if it is empty.

    • Throws TypeError if arg is not empty.

Example:

NotEmpty

Description:

Checks if the provided argument is not empty.

  • is.notEmpty(arg):

    • Returns true if arg is not empty.

    • Returns false otherwise.

  • as.notEmpty(arg):

    • Returns arg if it is not empty.

    • Throws TypeError if arg is empty.

Example:

JSON

Description:

Checks if the provided argument is a valid JSON string.

  • is.json(arg):

    • Returns true if arg is a valid JSON string.

    • Returns false otherwise.

  • **

as.json(arg):**

  • Returns arg if it is a valid JSON string.

  • Throws TypeError if arg is not a valid JSON string.

Example:

JSON5

Description:

Checks if the provided argument is a valid JSON5 string.

  • is.json5(arg):

    • Returns true if arg is a valid JSON5 string.

    • Returns false otherwise.

  • as.json5(arg):

    • Returns arg if it is a valid JSON5 string.

    • Throws TypeError if arg is not a valid JSON5 string.

Example:

Null

Description:

Checks if the provided argument is null.

  • is.null(arg):

    • Returns true if arg is null.

    • Returns false otherwise.

  • as.null(arg):

    • Returns arg if it is null.

    • Throws TypeError if arg is not null.

Example:

Number Validators

Sure, I'll restructure the documentation as requested. Here is the first part of the updated documentation:

Zero

Description:

Checks if the provided argument is exactly zero.

  • is.zero(arg):

    • Returns true if arg is 0.

    • Returns false otherwise.

  • as.zero(arg):

    • Returns arg if it is 0.

    • Throws TypeError if arg is not 0.

Example:

Even

Description:

Checks if the provided argument is an even number.

  • is.even(arg):

    • Returns true if arg is even.

    • Returns false otherwise.

  • as.even(arg):

    • Returns arg if it is even.

    • Throws TypeError if arg is not even.

Example:

Odd

Description:

Checks if the provided argument is an odd number.

  • is.odd(arg):

    • Returns true if arg is odd.

    • Returns false otherwise.

  • as.odd(arg):

    • Returns arg if it is odd.

    • Throws TypeError if arg is not odd.

Example:

Positive

Description:

Checks if the provided argument is a positive number.

  • is.positive(arg):

    • Returns true if arg is positive.

    • Returns false otherwise.

  • as.positive(arg):

    • Returns arg if it is positive.

    • Throws TypeError if arg is not positive.

Example:

Negative

Description:

Checks if the provided argument is a negative number.

  • is.negative(arg):

    • Returns true if arg is negative.

    • Returns false otherwise.

  • as.negative(arg):

    • Returns arg if it is negative.

    • Throws TypeError if arg is not negative.

Example:

Positive Integer

Description:

Checks if the provided argument is a positive integer.

  • is.positiveInteger(arg):

    • Returns true if arg is a positive integer.

    • Returns false otherwise.

  • as.positiveInteger(arg):

    • Returns arg if it is a positive integer.

    • Throws TypeError if arg is not a positive integer.

Example:

Negative Integer

Description:

Checks if the provided argument is a negative integer.

  • is.negativeInteger(arg):

    • Returns true if arg is a negative integer.

    • Returns false otherwise.

  • as.negativeInteger(arg):

    • Returns arg if it is a negative integer.

    • Throws TypeError if arg is not a negative integer.

Example:

Finite

Description:

Checks if the provided argument is a finite number.

  • is.isFinite(arg):

    • Returns true if arg is finite.

    • Returns false otherwise.

  • as.isFinite(arg):

    • Returns arg if it is finite.

    • Throws TypeError if arg is not finite.

Example:

NaN

Description:

Checks if the provided argument is NaN (Not-a-Number).

  • is.NaN(arg):

    • Returns true if arg is NaN.

    • Returns false otherwise.

  • as.NaN(arg):

    • Returns arg if it is NaN.

    • Throws TypeError if arg is not NaN.

Example:

Between

Description:

Checks if the provided argument is between min and max values.

  • is.between({ arg, min, max }):

    • Returns true if arg is between min and max.

    • Returns false otherwise.

  • as.between({ arg, min, max }):

    • Returns arg if it is between min and max.

    • Throws TypeError if arg is not between min and max.

Example:

Greater

Description:

Checks if the provided argument is greater than the specified value.

  • is.greater({ arg, value }):

    • Returns true if arg is greater than value.

    • Returns `

false` otherwise.

  • as.greater({ arg, value }):

    • Returns arg if it is greater than value.

    • Throws TypeError if arg is not greater than value.

Example:

Less

Description:

Checks if the provided argument is less than the specified value.

  • is.less({ arg, value }):

    • Returns true if arg is less than value.

    • Returns false otherwise.

  • as.less({ arg, value }):

    • Returns arg if it is less than value.

    • Throws TypeError if arg is not less than value.

Example:

Equal or Greater

Description:

Checks if the provided argument is equal to or greater than the specified value.

  • is.equalGreater({ arg, value }):

    • Returns true if arg is equal to or greater than value.

    • Returns false otherwise.

  • as.equalGreater({ arg, value }):

    • Returns arg if it is equal to or greater than value.

    • Throws TypeError if arg is not equal to or greater than value.

Example:

Equal or Less

Description:

Checks if the provided argument is equal to or less than the specified value.

  • is.equalLess({ arg, value }):

    • Returns true if arg is equal to or less than value.

    • Returns false otherwise.

  • as.equalLess({ arg, value }):

    • Returns arg if it is equal to or less than value.

    • Throws TypeError if arg is not equal to or less than value.

Example:

Max

Description:

Checks if the provided argument is equal to the specified maximum value.

  • is.max({ arg, value }):

    • Returns true if arg is equal to the maximum value.

    • Returns false otherwise.

  • as.max({ arg, value }):

    • Returns arg if it is equal to the maximum value.

    • Throws TypeError if arg is not equal to the maximum value.

Example:

Min

Description:

Checks if the provided argument is equal to the specified minimum value.

  • is.min({ arg, value }):

    • Returns true if arg is equal to the minimum value.

    • Returns false otherwise.

  • as.min({ arg, value }):

    • Returns arg if it is equal to the minimum value.

    • Throws TypeError if arg is not equal to the minimum value.

Example:

Multiple

Description:

Checks if the provided argument is a multiple of the specified value.

  • is.multiple({ arg, value }):

    • Returns true if arg is a multiple of value.

    • Returns false otherwise.

  • as.multiple({ arg, value }):

    • Returns arg if it is a multiple of value.

    • Throws TypeError if arg is not a multiple of value.

Example:

Port

Description:

Checks if the provided argument is a valid port number (between 0 and 65535).

  • is.port(arg):

    • Returns true if arg is a valid port number.

    • Returns false otherwise.

  • as.port(arg):

    • Returns arg if it is a valid port number.

    • Throws TypeError if arg is not a valid port number.

Example:

Safe Integer

Description:

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 true if arg is a safe integer.

    • Returns false otherwise.

  • as.safe(arg):

    • Returns arg if it is a safe integer.

    • Throws TypeError if arg is not a safe integer.

Example:

Precision

Description:

Checks if the provided argument has the specified precision value.

  • is.precision({ arg, value }):

    • Returns `true

ifarghas the specified precisionvalue`.

  • Returns false otherwise.

  • as.precision({ arg, value }):

    • Returns arg if it has the specified precision value.

    • Throws TypeError if arg does not have the specified precision value.

Example:

Digits

Description:

Checks if the provided argument has the specified number of digits value.

  • is.digits({ arg, value }):

    • Returns true if arg has the specified number of digits value.

    • Returns false otherwise.

  • as.digits({ arg, value }):

    • Returns arg if it has the specified number of digits value.

    • Throws TypeError if arg does not have the specified number of digits value.

Example:

ISBN-10

Description:

Checks if the provided argument is a valid ISBN-10 number.

  • is.ISBN10(arg):

    • Returns true if arg is a valid ISBN-10 number.

    • Returns false otherwise.

  • as.ISBN10(arg):

    • Returns arg if it is a valid ISBN-10 number.

    • Throws TypeError if arg is not a valid ISBN-10 number.

Example:

ISBN-13

Description:

Checks if the provided argument is a valid ISBN-13 number.

  • is.ISBN13(arg):

    • Returns true if arg is a valid ISBN-13 number.

    • Returns false otherwise.

  • as.ISBN13(arg):

    • Returns arg if it is a valid ISBN-13 number.

    • Throws TypeError if arg is not a valid ISBN-13 number.

Example:

EAN

Description:

Checks if the provided argument is a valid EAN (European Article Number).

  • is.EAN(arg):

    • Returns true if arg is a valid EAN.

    • Returns false otherwise.

  • as.EAN(arg):

    • Returns arg if it is a valid EAN.

    • Throws TypeError if arg is not a valid EAN.

Example:

SSN

Description:

Checks if the provided argument is a valid SSN (Social Security Number).

  • is.SSN(arg):

    • Returns true if arg is a valid SSN.

    • Returns false otherwise.

  • as.SSN(arg):

    • Returns arg if it is a valid SSN.

    • Throws TypeError if arg is not a valid SSN.

Example:

VIN

Description:

Checks if the provided argument is a valid VIN (Vehicle Identification Number).

  • is.VIN(arg):

    • Returns true if arg is a valid VIN.

    • Returns false otherwise.

  • as.VIN(arg):

    • Returns arg if it is a valid VIN.

    • Throws TypeError if arg is not a valid VIN.

Example:

INN-10

Description:

Checks if the provided argument is a valid INN (Individual Taxpayer Number) with 10 digits.

  • is.INN10(arg):

    • Returns true if arg is a valid INN with 10 digits.

    • Returns false otherwise.

  • as.INN10(arg):

    • Returns arg if it is a valid INN with 10 digits.

    • Throws TypeError if arg is not a valid INN with 10 digits.

Example:

INN-12

Description:

Checks if the provided argument is a valid INN (Individual Taxpayer Number) with 12 digits.

  • is.INN12(arg):

    • Returns true if arg is a valid INN with 12 digits.

    • Returns false otherwise.

  • as.INN12(arg):

    • Returns arg if it is a valid INN with 12 digits.

    • Throws TypeError if arg is not a valid INN with 12 digits.

Example:

GLN

Description:

Checks if the provided argument is a valid GLN (Global Location Number).

  • is.GLN(arg):

    • Returns true if arg is

a valid GLN.

  • Returns false otherwise.

  • as.GLN(arg):

    • Returns arg if it is a valid GLN.

    • Throws TypeError if arg is not a valid GLN.

Example:

IMEI

Description:

Checks if the provided argument is a valid IMEI (International Mobile Equipment Identity).

  • is.IMEI(arg):

    • Returns true if arg is a valid IMEI.

    • Returns false otherwise.

  • as.IMEI(arg):

    • Returns arg if it is a valid IMEI.

    • Throws TypeError if arg is not a valid IMEI.

Example:

NPI

Description:

Checks if the provided argument is a valid NPI (National Provider Identifier).

  • is.NPI(arg):

    • Returns true if arg is a valid NPI.

    • Returns false otherwise.

  • as.NPI(arg):

    • Returns arg if it is a valid NPI.

    • Throws TypeError if arg is not a valid NPI.

Example:

String Validators

Here is the restructured documentation for the string validation methods:

Alphabetic

Description:

Checks if the provided argument is an alphabetic string.

  • is.alphabetic(arg):

    • Returns true if arg contains only alphabetic characters.

    • Returns false otherwise.

  • as.alphabetic(arg):

    • Returns arg if it contains only alphabetic characters.

    • Throws TypeError if arg does not contain only alphabetic characters.

Example:

Digit

Description:

Checks if the provided argument is an digit string.

  • is.digit(arg):

    • Returns true if arg contains only digit characters.

    • Returns false otherwise.

  • as.digit(arg):

    • Returns arg if it contains only digit characters.

    • Throws TypeError if arg does not contain only digit characters.

Example:

Lowercase

Description:

Checks if the provided argument is a lowercase string.

  • is.lowercase(arg):

    • Returns true if arg is a lowercase string.

    • Returns false otherwise.

  • as.lowercase(arg):

    • Returns arg if it is a lowercase string.

    • Throws TypeError if arg is not a lowercase string.

Example:

Uppercase

Description:

Checks if the provided argument is an uppercase string.

  • is.uppercase(arg):

    • Returns true if arg is an uppercase string.

    • Returns false otherwise.

  • as.uppercase(arg):

    • Returns arg if it is an uppercase string.

    • Throws TypeError if arg is not an uppercase string.

Example:

CamelCase

Description:

Checks if the provided argument is a camelCase string.

  • is.camelCase(arg):

    • Returns true if arg is a camelCase string.

    • Returns false otherwise.

  • as.camelCase(arg):

    • Returns arg if it is a camelCase string.

    • Throws TypeError if arg is not a camelCase string.

Example:

snakeCase

Description:

Checks if the provided argument is a snakeCase string.

  • is.snakeCase(arg):

    • Returns true if arg is a snakeCase string.

    • Returns false otherwise.

  • as.snakeCase(arg):

    • Returns arg if it is a snakeCase string.

    • Throws TypeError if arg is not a snakeCase string.

Example:

Kebab-Case

Description:

Checks if the provided argument is a kebab-case string.

  • is.kebabCase(arg):

    • Returns true if arg is a kebab-case string.

    • Returns false otherwise.

  • as.kebabCase(arg):

    • Returns arg if it is a kebab-case string.

    • Throws TypeError if arg is not a kebab-case string.

Example:

Train-Case

Description:

Checks if the provided argument is a train-Case string.

  • is.trainCase(arg):

    • Returns true if arg is a train-Case string.

    • Returns false otherwise.

  • as.trainCase(arg):

    • Returns arg if it is a train-Case string.

    • Throws TypeError if arg is not a train-Case string.

Example:

Path

Description:

Checks if the provided argument is a valid path.

  • is.path(arg):

    • Returns true if arg is a valid path.

    • Returns false otherwise.

  • as.path(arg):

    • Returns arg if it is a valid path.

    • Throws TypeError if arg is not a valid path.

Example:

UUID

Description:

Checks if the provided argument is a valid UUID.

  • is.uuid(arg):

    • Returns true if arg is a valid UUID.

    • Returns false otherwise.

  • as.uuid(arg):

    • Returns arg if it is a valid UUID.

    • Throws TypeError if arg is not a valid UUID.

Example:

HTTP URL

Description:

Checks if the provided argument is a valid HTTP URL.

  • is.http(arg):

    • Returns true if arg is a valid HTTP URL.

    • Returns false otherwise.

  • as.http(arg):

    • Returns arg if it is a valid HTTP URL.

    • Throws TypeError if arg is not a valid HTTP URL.

Example:

HTTPS URL

Description:

Checks if the provided argument is a valid HTTPS URL.

  • is.https(arg):

    • Returns true if arg is a valid HTTPS URL.

    • Returns false otherwise

.

  • as.https(arg):

    • Returns arg if it is a valid HTTPS URL.

    • Throws TypeError if arg is not a valid HTTPS URL.

Example:

URL

Description:

Checks if the provided argument is a valid URL.

  • is.url(arg):

    • Returns true if arg is a valid URL.

    • Returns false otherwise.

  • as.url(arg):

    • Returns arg if it is a valid URL.

    • Throws TypeError if arg is not a valid URL.

Example:

Email

Description:

Checks if the provided argument is a valid email address.

  • is.email(arg):

    • Returns true if arg is a valid email address.

    • Returns false otherwise.

  • as.email(arg):

    • Returns arg if it is a valid email address.

    • Throws TypeError if arg is not a valid email address.

Example:

IPv4

Description:

Checks if the provided argument is a valid IPv4 address.

  • is.ipv4(arg):

    • Returns true if arg is a valid IPv4 address.

    • Returns false otherwise.

  • as.ipv4(arg):

    • Returns arg if it is a valid IPv4 address.

    • Throws TypeError if arg is not a valid IPv4 address.

Example:

IPv6

Description:

Checks if the provided argument is a valid IPv6 address.

  • is.ipv6(arg):

    • Returns true if arg is a valid IPv6 address.

    • Returns false otherwise.

  • as.ipv6(arg):

    • Returns arg if it is a valid IPv6 address.

    • Throws TypeError if arg is not a valid IPv6 address.

Example:

IP

Description:

Checks if the provided argument is a valid IP address (either IPv4 or IPv6).

  • is.ip(arg):

    • Returns true if arg is a valid IP address.

    • Returns false otherwise.

  • as.ip(arg):

    • Returns arg if it is a valid IP address.

    • Throws TypeError if arg is not a valid IP address.

Example:

File Extension

Description:

Checks if the provided argument is a valid file extension.

  • is.fileExtension(arg):

    • Returns true if arg is a valid file extension.

    • Returns false otherwise.

  • as.fileExtension(arg):

    • Returns arg if it is a valid file extension.

    • Throws TypeError if arg is not a valid file extension.

Example:

Hex Color

Description:

Checks if the provided argument is a valid hex color code.

  • is.hexColor(arg):

    • Returns true if arg is a valid hex color code.

    • Returns false otherwise.

  • as.hexColor(arg):

    • Returns arg if it is a valid hex color code.

    • Throws TypeError if arg is not a valid hex color code.

Example:

Base64

Description:

Checks if the provided argument is a valid base64 encoded string.

  • is.base64(arg):

    • Returns true if arg is a valid base64 encoded string.

    • Returns false otherwise.

  • as.base64(arg):

    • Returns arg if it is a valid base64 encoded string.

    • Throws TypeError if arg is not a valid base64 encoded string.

Example:

Data URL

Description:

Checks if the provided argument is a valid data URL.

  • is.dataURL(arg):

    • Returns true if arg is a valid data URL.

    • Returns false otherwise.

  • as.dataURL(arg):

    • Returns arg if it is a valid data

URL.

  • Throws TypeError if arg is not a valid data URL.

Example:

Credit Card

Description:

Checks if the provided argument is a valid credit card number.

  • is.creditCard(arg):

    • Returns true if arg is a valid credit card number.

    • Returns false otherwise.

  • as.creditCard(arg):

    • Returns arg if it is a valid credit card number.

    • Throws TypeError if arg is not a valid credit card number.

Example:

MasterCard

Description:

Checks if the provided argument is a valid MasterCard number.

  • is.masterCard(arg):

    • Returns true if arg is a valid MasterCard number.

    • Returns false otherwise.

  • as.masterCard(arg):

    • Returns arg if it is a valid MasterCard number.

    • Throws TypeError if arg is not a valid MasterCard number.

Example:

Visa

Description:

Checks if the provided argument is a valid Visa number.

  • is.visa(arg):

    • Returns true if arg is a valid Visa number.

    • Returns false otherwise.

  • as.visa(arg):

    • Returns arg if it is a valid Visa number.

    • Throws TypeError if arg is not a valid Visa number.

Example:

American Express

Description:

Checks if the provided argument is a valid American Express number.

  • is.americanExpress(arg):

    • Returns true if arg is a valid American Express number.

    • Returns false otherwise.

  • as.americanExpress(arg):

    • Returns arg if it is a valid American Express number.

    • Throws TypeError if arg is not a valid American Express number.

Example:

Diners Club

Description:

Checks if the provided argument is a valid Diners Club number.

  • is.dinersClub(arg):

    • Returns true if arg is a valid Diners Club number.

    • Returns false otherwise.

  • as.dinersClub(arg):

    • Returns arg if it is a valid Diners Club number.

    • Throws TypeError if arg is not a valid Diners Club number.

Example:

Domain

Description:

Checks if the provided argument is a valid domain.

  • is.domain(arg):

    • Returns true if arg is a valid domain.

    • Returns false otherwise.

  • as.domain(arg):

    • Returns arg if it is a valid domain.

    • Throws TypeError if arg is not a valid domain.

Example:

GUID

Description:

Checks if the provided argument is a valid GUID.

  • is.guid(arg):

    • Returns true if arg is a valid GUID.

    • Returns false otherwise.

  • as.guid(arg):

    • Returns arg if it is a valid GUID.

    • Throws TypeError if arg is not a valid GUID.

Example:

Hostname

Description:

Checks if the provided argument is a valid hostname.

  • is.hostname(arg):

    • Returns true if arg is a valid hostname.

    • Returns false otherwise.

  • as.hostname(arg):

    • Returns arg if it is a valid hostname.

    • Throws TypeError if arg is not a valid hostname.

Example:

ISO Date

Description:

Checks if the provided argument is a valid ISO date.

  • is.isoDate(arg):

    • Returns true if arg is a valid ISO date.

    • Returns false otherwise.

  • as.isoDate(arg):

    • Returns arg if it is a valid ISO date.

    • Throws TypeError if arg is not a valid ISO date.

Example:

ISO Duration

Description:

Checks if the provided argument is a valid ISO duration.

  • is.isoDuration(arg):

    • Returns true if arg is a valid ISO duration.

    • Returns false otherwise.

  • as.isoDuration(arg):

    • Returns arg if it is a valid ISO duration.

    • Throws TypeError if arg is not a valid ISO duration.

Example:

JWT

Description:

Checks if the provided argument is a valid JWT.

  • is.jwt(arg):

    • Returns true if arg is a valid JWT.

    • Returns false otherwise.

  • as.jwt(arg):

    • Returns arg if it is a valid JWT.

    • Throws TypeError if arg is not a valid JWT.

Example:

Emoji

Description:

Checks if the provided argument is a valid emoji.

  • is.emoji(arg):

    • Returns true if arg is a valid emoji.

    • Returns false otherwise.

  • as.emoji(arg):

    • Returns arg if it is a valid emoji.

    • Throws TypeError if arg is not a valid emoji.

Example:

Nanoid

Description:

Checks if the provided argument is a valid nanoid.

  • is.nanoid(arg):

    • Returns true if arg is a valid nanoid.

    • Returns false otherwise.

  • as.nanoid(arg):

    • Returns arg if it is a valid nanoid.

    • Throws TypeError if arg is not a valid nanoid.

Example:

CUID

Description:

Checks if the provided argument is a valid CUID.

  • is.cuid(arg):

    • Returns true if arg is a valid CUID.

    • Returns false otherwise.

  • as.cuid(arg):

    • Returns arg if it is a valid CUID.

    • Throws TypeError if arg is not a valid CUID.

Example:

CUID2

Description:

Checks if the provided argument is a valid CUID2.

  • is.cuid2(arg):

    • Returns true if arg is a valid CUID2.

    • Returns false otherwise.

  • as.cuid2(arg):

    • Returns arg if it is a valid CUID2.

    • Throws TypeError if arg is not a valid CUID2.

Example:

Excludes

Description:

Checks if the provided argument excludes a given substring.

  • is.excludes({ arg, value }):

    • Returns true if arg excludes value.

    • Returns false otherwise.

  • as.excludes({ arg, value }):

    • Returns arg if it excludes value.

    • Throws TypeError if arg includes value.

Example:

Time (HH:MM:SS)

Description:

Checks if the provided argument is a valid time in the format HH:MM:SS.

  • is.time(arg):

    • Returns true if arg is a valid time.

    • Returns false otherwise.

  • as.time(arg):

    • Returns arg if it is a valid time.

    • Throws TypeError if arg is not a valid time.

Example:

DateTime (YYYY-MM-DDTHH:MM:SS)

Description:

Checks if the provided argument is a valid datetime in the format YYYY-MM-DDTHH:MM:SS.

  • is.datetime(arg):

    • Returns true if arg is a valid datetime.

    • Returns false otherwise.

  • as.datetime(arg):

    • Returns `

arg` if it is a valid datetime.

  • Throws TypeError if arg is not a valid datetime.

Example:

Date (YYYY-MM-DD)

Description:

Checks if the provided argument is a valid date in the format YYYY-MM-DD.

  • is.date(arg):

    • Returns true if arg is a valid date.

    • Returns false otherwise.

  • as.date(arg):

    • Returns arg if it is a valid date.

    • Throws TypeError if arg is not a valid date.

Example:

SHA-256 Hash

Description:

Checks if the provided argument is a valid SHA-256 hash.

  • is.hash(arg):

    • Returns true if arg is a valid SHA-256 hash.

    • Returns false otherwise.

  • as.hash(arg):

    • Returns arg if it is a valid SHA-256 hash.

    • Throws TypeError if arg is not a valid SHA-256 hash.

Example:

ISO Time with Seconds

Description:

Checks if the provided argument is a valid ISO time with seconds.

  • is.isoTimeSecond(arg):

    • Returns true if arg is a valid ISO time with seconds.

    • Returns false otherwise.

  • as.isoTimeSecond(arg):

    • Returns arg if it is a valid ISO time with seconds.

    • Throws TypeError if arg is not a valid ISO time with seconds.

Example:

ISO Timestamp

Description:

Checks if the provided argument is a valid ISO timestamp.

  • is.isoTimestamp(arg):

    • Returns true if arg is a valid ISO timestamp.

    • Returns false otherwise.

  • as.isoTimestamp(arg):

    • Returns arg if it is a valid ISO timestamp.

    • Throws TypeError if arg is not a valid ISO timestamp.

Example:

ISO Week

Description:

Checks if the provided argument is a valid ISO week.

  • is.isoWeek(arg):

    • Returns true if arg is a valid ISO week.

    • Returns false otherwise.

  • as.isoWeek(arg):

    • Returns arg if it is a valid ISO week.

    • Throws TypeError if arg is not a valid ISO week.

Example:

MAC Address

Description:

Checks if the provided argument is a valid MAC address.

  • is.mac(arg):

    • Returns true if arg is a valid MAC address.

    • Returns false otherwise.

  • as.mac(arg):

    • Returns arg if it is a valid MAC address.

    • Throws TypeError if arg is not a valid MAC address.

Example:

MAC-48 Address

Description:

Checks if the provided argument is a valid MAC-48 address.

  • is.mac48(arg):

    • Returns true if arg is a valid MAC-48 address.

    • Returns false otherwise.

  • as.mac48(arg):

    • Returns arg if it is a valid MAC-48 address.

    • Throws TypeError if arg is not a valid MAC-48 address.

Example:

MAC-64 Address

Description:

Checks if the provided argument is a valid MAC-64 address.

  • is.mac64(arg):

    • Returns true if arg is a valid MAC-64 address.

    • Returns false otherwise.

  • as.mac64(arg):

    • Returns arg if it is a valid MAC-64 address.

    • Throws TypeError if arg is not a valid MAC-64 address.

Example:

Past Date

Description:

Checks if the provided argument is a date in the past.

  • is.past(arg):

    • Returns true if `

arg` is a date in the past.

  • Returns false otherwise.

  • as.past(arg):

    • Returns arg if it is a date in the past.

    • Throws TypeError if arg is not a date in the past.

Example:

Future Date

Description:

Checks if the provided argument is a date in the future.

  • is.future(arg):

    • Returns true if arg is a date in the future.

    • Returns false otherwise.

  • as.future(arg):

    • Returns arg if it is a date in the future.

    • Throws TypeError if arg is not a date in the future.

Example:

ASCII String

Description:

Checks if the provided argument is an ASCII string.

  • is.ascii(arg):

    • Returns true if arg is an ASCII string.

    • Returns false otherwise.

  • as.ascii(arg):

    • Returns arg if it is an ASCII string.

    • Throws TypeError if arg is not an ASCII string.

Example:

Base32

Description:

Checks if the provided argument is a valid Base32 encoded string.

  • is.base32(arg):

    • Returns true if arg is a valid Base32 encoded string.

    • Returns false otherwise.

  • as.base32(arg):

    • Returns arg if it is a valid Base32 encoded string.

    • Throws TypeError if arg is not a valid Base32 encoded string.

Example:

Base58

Description:

Checks if the provided argument is a valid Base58 encoded string.

  • is.base58(arg):

    • Returns true if arg is a valid Base58 encoded string.

    • Returns false otherwise.

  • as.base58(arg):

    • Returns arg if it is a valid Base58 encoded string.

    • Throws TypeError if arg is not a valid Base58 encoded string.

Example:

Date Before Specific Date

Description:

Checks if the provided argument is a date before a specific date.

  • is.before({ arg, value }):

    • Returns true if arg is before value.

    • Returns false otherwise.

  • as.before({ arg, value }):

    • Returns arg if it is before value.

    • Throws TypeError if arg is not before value.

Example:

Date After Specific Date

Description:

Checks if the provided argument is a date after a specific date.

  • is.after({ arg, value }):

    • Returns true if arg is after value.

    • Returns false otherwise.

  • as.after({ arg, value }):

    • Returns arg if it is after value.

    • Throws TypeError if arg is not after value.

Example:

Maximum String Length

Description:

Checks if the provided argument's length is less than or equal to a specific value.

  • is.maxStr({ arg, value }):

    • Returns true if arg length is less than or equal to value.

    • Returns false otherwise.

  • as.maxStr({ arg, value }):

    • Returns arg if its length is less than or equal to value.

    • Throws TypeError if arg length is greater than value.

Example:

Minimum String Length

Description:

Checks if the provided argument's length is greater than or equal to a specific value.

  • is.minStr({ arg, value }):

    • Returns true if arg length is greater than or equal to value.

    • Returns false otherwise.

  • as.minStr({ arg, value }):

    • Returns arg if its length is greater than or equal to value.

    • Throws TypeError if arg length is less than value.

Example:

Has Uppercase

Description:

Checks if the provided string contains at least one uppercase letter (A-Z).

  • is.hasUppercase({ arg }):

    • Returns true if the string contains uppercase letters.

    • Returns false otherwise.

  • as.hasUppercase({ arg }):

    • Returns the original string arg if it contains at least one uppercase letter.

    • Throws TypeError (e.g., "String must contain at least one uppercase letter") otherwise.

Example:


Has Lowercase

Description:

Checks if the provided string contains at least one lowercase letter (a-z).

  • is.hasLowercase({ arg }):

    • Returns true if the string contains lowercase letters.

    • Returns false otherwise.

  • as.hasLowercase({ arg }):

    • Returns the original string arg if contains at least one lowercase letter.

    • Throws TypeError (e.g., "String must contain at least one lowercase letter") otherwise.

Example:

Has Digit

Description:

Checks if the provided string contains at least one digit (0-9).

  • is.hasDigit({ arg }):

    • Returns true if the string has a digit.

    • Returns false otherwise.

  • as.hasDigit({ arg }):

    • Returns the original string arg if string contains at least one digit.

    • Throws TypeError (e.g., "String must contain at least one digit") otherwise.

Example:


Has Special Character

Description:

Checks if the provided string contains at least one special character (example, !@#$%^&*(),.?":{}|<>).

  • is.hasSpecialCharacter({ arg }):

    • Returns true contains at least one special character.

    • Returns false otherwise.

  • 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:


Password

Description:

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 true string meets all of the following requirements

    • Returns false otherwise.

  • as.password({ arg }):

    • Returns arg, if string meets all of the following requirements.

    • Throws TypeError (example, "String must meet password requirements") otherwise.

Example:

Settings

To customize error messages, you can override Checker.errorMsg. To disable throwing errors, set checker.disabled = true.

Example:

Utility

A simple method to get the type of argument

Aliases

You can check the following types by "is", "as" or javascript, but this looks more readable

Micro-tests Basic usage

There are only five main methods: METHOD, PROPERTY, IS, passed and failed.

METHOD usage

METHOD strictly checks methods.

PROPERTY usage

PROPERTY strictly checks properties.

IS 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".

Any 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.

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.

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

Advanced techniques

Checking one repeated type

In object, array, set and map. All types ending in 's' will be checked.

Strict typing

Basics

Basic usage

Use strictObject.values() to get all values.

Once the strict instance has been created, you can do the following:

Strict has reserved variable names: get, set, values, types, variable, lastType. This means that you can do the following;

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.

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 :)

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

Basic usage

Macros

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

Object example

Objects method returns a callback

Only when the array is contain an object or a callback you can repeat the macro.

Weird syntax

If you need to make something weird or unusual you can try this working syntax.

Possible Errors

If you encounter the following error:

Ensure you have a semicolon ; at the end of the previous line.

No Dependencies

This library has no external dependencies.

Last updated