Home - NeoGalaxy/Tycker GitHub Wiki

Welcome to the API

If you want to learn using Tycker, you're at the right place.

Tycker is a runtime JS type checker. In other words, it can tell you if a variable is of a given type. The main interest of this library is that a type has many ways to be defined, it does NOT have to be a native type or an object from a class.

For example, you can define a type for a positive number in order to check if a given variable is a positive number as such:

Tycker.def({name:'pos_num', baseType:'number', checker: n => n > 0})
// Creates the type positive number

So, whenever a type check is executed to check if the variable is of type pos_num, Tycker will check if the variable is of type number, and then run the given checker if it is indeed a number. The type will be saved within Tycker, so that you can check if a variable is of this type with Tycker.check(variable, 'pos_num'), but you have to be cautious that there is no naming conflict (see how to make multiple Tycker instances to avoid naming conflict).

Alternatively, one can define a type as such:

const PosNum = Tycker.def({baseType:'number', checker: n => n > 0})
// Puts the type in a variable and does not register it within Tycker

This will NOT save the type within the Tycker instance, but will instead return it and save it within PosNum. Then, Tycker.check(variable, PosNum) will do the same as the precedent paragraph.
You can also save it within the Tycker instance AND put it in a variable by specifying a name and putting the return value in a variable:

const PosNum = Tycker.def({name:'pos_num', baseType:'number', checker: n => n > 0} )
// Puts the type in a variable and registers it within Tycker

But that's not all, you can also create types on the fly. For instance, you can check if a variable is one of two types by separating the types by a vertical bar '|':

// check if we have a positive number or an array.
Tycker.check(variable, "pos_num | array")

or check if it is an object with specific fields:

Tycker.check(variable, {
    name: 'string',
    'id?': 'number' // optionnal, but cannot be anything else than a number
})

By the way, on types created on the fly, the name that are within strings have to be registered or basic types. Here, we used "pos_num", "array", "string" and "number". "string" and "number" are basic types, but it is not the case of "pos_num" and "array". But the type "array" is pre-defined by Tycker (it uses Array.isArray to check), and the type "pos_num" is defined above (just take one of the code blocks with the name specified in the parameters).

There will be a whole section on what are the types and how to use them.

Sections :