neat-contract
yet another one contract library, data flow focused
If contract is satisfied, returns value, otherwise, throwing TypeError
exception.
Well suited for promises as well.
Install
npm install --save neat-contract
Usage
Basic
; ; // 'unicorns'; // new TypeError('`input` should be an `String`, but got `Number`: 2') // several types; // 'unicorns'; // ['unicorns']; // new TypeError('`input` should be an `String|Array`, but got `Number`: 2') // curried'unicorns'; // 'unicorns'2; // new TypeError('`input` should be an `String`, but got `Number`: 2') // contracting array; // ['unicorns', 'cats']; // new TypeError('`input` should be an `Array`, but got `Number`: 2')'unicorns' 'cats'; // ['unicorns', 'cats']'unicorns' 2; // new TypeError('`input[item]` should be an `String`, but got `Number`: 2')
Data-flow
Sync data-flow
;; // sync data flowconst prefixEslintPlugin = R; ; // 'eslint-plugin-import'; // new TypeError('`pluginName` should be an `String`, but got `Boolean`: true'); // new TypeError('`pluginName` should be an `String`, but got `RegExp`: /reg/')
Async data-flow
;; // helpersconst toPromise = Promiseresolve;const _log = consolelog;const _throw = { throw err; }; // async data flowconst prefixEslintPluginAsync = R; ; // 'eslint-plugin-import'; // new TypeError('`pluginName` should be an `String`, but got `Boolean`: true'); // new TypeError('`pluginName` should be an `String`, but got `RegExp`: /reg/')
Arrays in data-flow
;; // contracting arrayconst prefixArrayOfEslintPlugins = R; ; // ['eslint-plugin-import', 'eslint-plugin-export']; // throw new TypeError('`plugins` should be an `Array`, but got `Number`: 2'); // throw new TypeError('`plugins[item]` should be an `String`, but got `Number`: 2')
Contracting arrays in asynchronous functions goes, the same way as in async example from above.
API
contract(name, ctor, param)
// contract :: String -> Constructor -> a -> a|throw new TypeError
name
Required
Type: String
Parameter name.
ctor
Required
Type: Constructor
Example: String
, Number
or Function
Parameter Constructor for type check
param
Required
Type: any
Actual parameter itself to type check with Constructor
.
License
MIT © Vladimir Starkov