ens
Easily ensure data types.
Why
// Ensure variable `obj` is an object, the traditional wayobj = typeof obj === 'object' && !obj instanceof Array ? obj : {};
// Ensure variable obj is an object, using ens.objobj = ens;
ens
makes it easy to ensure JS data types. With JS, ensuring data types requires a lot of ugly code. It's easy to specify variable defaults though: obj = obj || {}
. The problem with this pattern is, when obj
is set to a value which evaluates to true, the default value {}
won't be bound to obj
. This is where ens
comes in: obj = ens.obj(obj)
. Which results in the binding of an object to obj
no mather what type of data obj
is.
Install
npm install ens
Use
var ens = ; ens; // []ens; // []ens; // [1, 2] ens; // trueens; // trueens; // false ens; // function () {}ens; // function () {}ens; // function test () {} ens; // 1ens; // 1ens; // 5 ens; // {}ens; // {}ens; // {a: 'b'} ens; // ''ens; // ''ens; // 'test'