JSPM

ens

0.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q61605F
  • License MIT

Easily ensure data types

Package Exports

  • ens

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ens) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ens

Easily ensure data types.

Build Status Coverage Status Inline docs Codacy Badge Code Climate Dependency Status devDependency Status


Why

// Ensure variable `obj` is an object, the traditional way
obj = (typeof obj === 'object' && !(obj instanceof Array)) ? obj : {};
// Ensure variable obj is an object, using ens.obj
obj = ens.obj(obj);

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 = require('ens');

ens.arr();       // []
ens.arr({});     // []
ens.arr([1, 2]); // [1, 2]

ens.boo();      // true
ens.boo({});    // true
ens.boo(false); // false

ens.fun();                   // function () {}
ens.fun({});                 // function () {}
ens.fun(function test() {}); // function test () {}

ens.num();   // 1
ens.num({}); // 1
ens.num(5);  // 5

ens.obj();         // {}
ens.obj([]);       // {}
ens.obj({a: 'b'}); // {a: 'b'}

ens.str();       // ''
ens.str({});     // ''
ens.str('test'); // 'test'