Variable wrapper utility
JavaScript
Latest commit f90af12 Dec 16, 2016 @ryanmorr Build v0.1.0
Permalink
Failed to load latest commit information.
dist
src
test
.babelrc
.editorconfig
.eslintrc
.gitattributes
.gitignore
.travis.yml
README.md
UNLICENSE
gulpfile.babel.js
package.json

README.md

wrap

Version Badge Build Status Dependencies License File Size

Variable wrapper utility

Usage

Wrap a variable to provide abstracted utilities:

import wrap from 'wrap';

const value = wrap('foo');

Get and set the internal variable:

const value = wrap();

value.set('bar');
value.get(); //=> "bar"

Get the type of the variable or type check it:

const value = wrap(false);

value.type(); //=> "boolean"
value.is('number'); //=> false

Make assertions on the value:

const value = wrap(100);

value.assert((val) => val < 200); //=> true

Watch for changes:

const value = wrap('foo');

value.observe((val) => console.log(val));

// Setting the value triggers all observers
value.set('bar');

Determine if the internal variable is strictly equal to another value:

const value = wrap('foo');

value.equals('foo'); //=> true

Implements the iterable protocol for arrays and objects, enabling for...of loops:

const object = wrap({foo: 1, bar: 2, baz: 3});
for (const [key, value] of object) {
    // do something
}

const array = wrap([1, 2, 3]);
for (const value of array) {
    // do something
}

Convert the value to JSON:

const value = wrap({foo: 1, bar: 2});

value.toJSON(); //=> '{"foo":1,"bar":2}'
JSON.stringify(value); //=> "{\"foo\":1,\"bar\":2}"

Generate a unique integer representation of the internal variable based on its value:

const value = wrap([1, 2, 3]);

value.hashCode(); //=> 5055

Clone the variable:

const object = {foo: 1, bar: 2};
const value = wrap(object);

const copy = value.clone();

object === copy; //=> false;
object.foo === copy.foo; //=> true;
object.bar === copy.bar; //=> true;

Log to the console and throw errors:

const value = wrap('foo');

value.log('Initialized');
value.error('Error!');

Turn on debugging mode (injects the debugger statement):

const value = wrap(123);

// Turn on debugging mode
value.debug()

// Creates a breakpoint anytime the value is read or changed
value.set(456);
value.get();

// Turn off debugging mode
value.debug(false);

Get a string representation of the internal variable or coerce into a primitive value (returns same as hashCode):

const value = wrap('foo');

value.toString(); //=> "foo"
value + ""; //=> "foo"

value.valueOf(); //=> 101574
value + 10; // 101584

Nullify the reference to the internal variable

const value = wrap('foo');

value.get(); //=> "foo"
value.release();
value.get(); //=> null

Installation

Wrap is CommonJS and AMD compatible with no dependencies. You can download the development or minified version, or install it in one of the following ways:

npm install ryanmorr/wrap

bower install ryanmorr/wrap

Tests

Run unit tests by issuing the following commands:

npm install
npm install -g gulp
gulp test

License

This project is dedicated to the public domain as described by the Unlicense.