es6-javascript-validators
Stop for a second. Do you see this a lot in your code?:
if (typeof var1 === 'string' && var1.length > 0) {
}
I see it all the time -- and in tons of places too. Or, my favorite:
if (typeof var2 === 'object' && var2 !== null) {
}
Well, lets collectively stop that. Lets use simple functions to do that for us! es6-javascript-validators is a collection of functions which will do all of this for you and keep your code consistent and clean.
Available Methods
- isNullOrUndefined: Is it null or undefined?
- isStr: Is it a string?
- isArr: Is it an array?
- isNum: Is it a number?
- isObj: Is it an object?
- isFnc: Is it a function?
- validStr: Is it a string and not an empty one?
- validArr: Is it an array and not an empty one?
- validNum: Is it a number and greater than 0?
- validObj: Is it an object and not an empty one?
- ...Or, if you want all of them, just export the default object which contains all of the above.
Import specific functions
import { isStr, isObj } from 'your/path/to/module.js';
console.log(isStr(''));
console.log(isStr([]));
console.log(isStr(null));
console.log(isStr(0));
console.log(isObj(''));
console.log(isObj([]));
console.log(isObj(null));
console.log(isObj({}));
Import all utils
import Util from 'your/path/to/module.js';
console.log(Util.isObj({}));
console.log(Util.validObj({}));
console.log(Util.validObj({ a:1, b:2 }));
console.log(Util.isStr(''));
console.log(Util.validStr(''));
console.log(Util.isArr([]));
console.log(Util.validArr([1, 2, 3]));