node package manager
Stop writing boring code. Discover, share, and reuse within your team. Create a free org ยป

utility-belt

What It Is

This package adds ruby-like chained casting methods to javascript objects.

Simple Example

Following is an example that loads utilityBelt and tests to see if a value is boolean.

utilityBelt = require('utility-belt');

var test = true;
console.log test.is_bool(); // true

When successful, the data logged in the console would be a javascript object like { media_count: 10863, name: 'blue' }.

Installation

$ npm install utility-belt

Setup

To use the library, all you need to do is require it.

utilityBelt = require('utility-belt');

Available Methods

All methods are available on all datatypes covered. As long as there isn't a method previously existing, these methods will be added to the core datatype classes.

Datatypes

  • Array
  • Boolean
  • Date
  • Function
  • Number (integer or float)
  • Object (hash)
  • RegExp
  • String

Methods

Testing Methods

These methods will test if the input value matches the requested type.

value.is_array()
value.is_ary() // alias for is_array()
value.is_boolean()
value.is_bool() // alias for is_bool()
value.is_date()
value.is_float()
value.is_function()
value.is_func() // alias for is_func()
value.is_hash()
value.is_integer()
value.is_int() // alias for is_integer()
value.is_null()
value.is_number()
value.is_object()
value.is_obj() // alias for is_object()
value.is_regexp()
value.is_string()
value.is_str() // alias for is_string()

Valuation Methods

This method tests if the input value is empty. The method will return true if the input value is any of: undefined, null, an empty date, or an empty object.

value.is_empty()

Casting Methods

These methods will (one way or another) force your input value into the requested type.

value.to_array()
value.to_boolean()
value.to_float()
value.to_integer()
value.to_string()

Developers

If you have suggestions please email me, register an issue, fork and branch, etc. If you add additional functionality, your pull request must have corresponding additional tests and supporting documentation. I've used CoffeeScript to write this library. Refer to the CoffeeScript docs for installation and usage.

Tests

There is a test suite in the /tests folder with the tests I used to ensure the library functions as intended. If you're adding or changing functionality, please add to or update the corresponding tests before issuing a pull request. The tests require Express, Expresso and Should:

npm install express
npm install expresso
npm install should