Insist
Insist on types! Make your code more readable by explicitly requiring type for function parameters.
Install
npm install insist-types
Supports both Node and the browser. On the browser, exported as window.insist
.
Basic Usage
var insist = ; var { insist; // ...}; ; // works!; // throws error
More interesting stuff
Insist can understand multiple types, optional arguments, nullable types, typed arrays, and classes.
Multiple Types
Just put the desired types in an array.
var insist = ; var { insist; // ...}; ; // works!; // works!; // works!; // throws error
Optional Types
insist.args
always returns an array of the arguments and shifts each argument into the proper
index when dealing with optional types.
var insist = ; var { var args = insist; // args will be [{number}, {object or null}, {function}]}; ; // works!; // works! // throws error // throws error
Nullable Types
var insist = ; var { insist; // ...}; ; // works!; // works!; // throws error // throws error
Both insist.optional
and insist.nullable
really just augment the type. In the above example, it would have worked to use [Function, null]
instead of insist.nullable(Function)
. If you want to include undefined
make sure you use insist.optional
instead of insist.nullable
.
Typed Arrays
var insist = ; var { insist; // ...}; ; // works!; // works!; // works!; // throws error
You can also nest arrays.
var insist = ; var { insist; // ...}; ; // works!; // works!; // works!; // throws error
To handle the last case (foo([1, [2, 3, [4]]]);
), you can just use an array instead.
var insist = ; var { insist; // ...}; ; // works!; // works!; // works!; // works!
Classes
var insist = ; {}; var { insist; // ...}; ; // works!; // throws error
Sublasses work too!
var insist = ;var util = ;var events = ; { eventsEventEmitter;};util; var { insist; // ...}; ; // works!
Anything
Sometimes it's handy to have a definition that'll take anything.
var insist = ; { insist; // ...}; ; // works!; // works!; // works!; // works!; // works!; // works!; // throws error
I've debated a lot about whether null
and undefined
should be include in insist.anything
. Currently, null
is acceptable and undefined
is not because I've found this to be the most useful anything
. Open an issue if you have some input!
Type
It can be valuable to assert that a parameter is actually a type.
var insist = ; { insist; // ...}; ; // works!; // works!; // works!; // works!; // throws error
Enum
var insist = ; Colors = RED: "red" GREEN: "green" BLUE: "blue" ; { insist; // ...}; ; // works!; // works!; // throws error; // throws error
Options
You can set options on insist.
;// orvar insist = isDisabled: true;
Available Options
isDisabled
When set to true
, all asserts are set to noops, except for insist.args
, which checks for optional expected types (so as to not break argument shifting). In Node, when NODE_ENV
is set to production
, isDisabled is set to true by default, unless INSIST_IN_PROD
is set to true
.
Remover
It's recommended that insist is removed from production code, especially client-side JavaScript, for both size and efficiency. The Remover class makes this simple. It is worth noting, before removing a reference, the reference is checked to make sure that it is not being used to shift arguments. If it is, the reference will not be removed.
var insist = ;var source = fs;var remover = ;var newSource remover;fs;
Options
Currently, there is only one option for the remover: aliases. This option allows for when insist methods of been aliased to something else in a codebase.
var insist = ;var source = fs;var remover = aliases: args: 'assertArgs' ofType: 'assertOfType' ;var newSource remover;fs;
CLI
The remover is also available through the CLI.
usage: insist-types [-h] [-v] -i [DIR|FILE [DIR|FILE ...]] [-a STRING] [-t STRING] Removes insist-types from matching files. Optional arguments: -h, --help Show this help message and exit. -v, --version Show program's version number and exit. -i [DIR|FILE [DIR|FILE ...]], --include [DIR|FILE [DIR|FILE ...]] Adds the directory or file to be included in the processing. At least one required. -a STRING, --argsAlias STRING Specifies the alias for insist.args. Defaults to 'insist.args'. -t STRING, --ofTypeAlias STRING Specifies the alias for insist.ofType. Defaults to 'insist.ofType'.
Full API
insist // asserts the type of an arguments object and returns the shifted argumentsinsist // asserts the type of a valueinsist // asserts that the supplied type is actually a typeinsist // returns true|false for whether the type is actually a typeinsist // returns true|flase for whether the type is an optional typeinsist // returns true|false for whether the value is of the typeinsist // returns the name of the typeinsist // returns the name of the value (ex. String, Anonymous function)insist // used for creating an array typeinsist // used for creating a nullable typeinsist // used for creating an optional typeinsist // used for a type that can be anythinginsisttype // used for a type that expects a typeinsist // used for creating an enum type insistRemover // used to remove references from sourceinsistRemoverprototype // removes references
Changelist
1.4.3
- Fixed bug where references that were not calls were still being removed.
1.4.2
- Fixed bug with cyclical references
1.4.1
- Properly exported insist.Remover
1.4.0
- Added CLI for insist.Remover
1.3.0
- Adds insist.Remover for removing references from source. Recommended to be used before shipping code to production.
- Relaxed restriction that types must be functions. Fixes #2.
1.2.1
- Fixed bug with exporting to browser
1.2.0
- Added browser support, now exports to window.insist when module doesn't exist
- Added option support
- Added isDisabled option to explicitly enable or disable (useful for browser environments)
1.1.0
- Added support for checking for types (insist.type)
- Added support for enums