assertsjs
Version:
ECMA6 Module for Javascript type validations
37 lines (26 loc) • 1.25 kB
Markdown
Validate all your Javascript types!
Install:
`npm i -S assertsjs`
This code uses ECMA6 modules, if you want ECMA5 support you need to build it with a JS builder like webpack, babelify and so on.
Example:
'use strict';
const Asserts = require('assertsjs');
/**
* This function will return an array of the same string with custom size
* @params {string} stringArgument
* @params {number} numberArgument
* @return {Array<string>}
*/
const repeaterFunction = (numberArgument, stringArgument) => {
Asserts.assertString(stringArgument); //This will throw the default message: 'Assertion fail: string must be provided'
Asserts.assert(typeof numberArgument === 'number', 'Must be a number'); //But this is throwing the custom given message
const stringArray = [];
for(let i = 0; i < numberArgument; i++){
stringArray.push(stringArgument);
}
return stringArray; //You could check output functions too
};
The code will check the arguments and throw errors if the type is not the correct so you can trust yourself or your team mates
they will care about the type of your variables.