1.1.0 • Published 8 years ago

library-extensions v1.1.0

Weekly downloads
4
License
ISC
Repository
github
Last release
8 years ago

Library extensions

This are simple utilities used to extend an existing library.

Installation

$ npm install --save library-extensions

Usage

Use the create method to create a new extension function:

const libExt = require('library-extensions');
const test = {value: 1};

const sumExt = libExt.create('sum', (object, sum) => object.value + sum);

Use the bundle method to create the bundle object with all your extensions

const MyExtension = libExt.bundle([sumExt]);

With the generated bundle you can call your method statically, e.g.:

console.log(MyExtension.sum(test, 3)); // 4

Or you can extend an object with the bundled methods:

MyExtension.extend(test);
console.log(test.sum(3)); // 4

You can also use the extension with a class' prototype to extend all instanced objects.

Warning: if a method already exists with the extension name on the object you are extending an Error will be thrown.

You can reset an extended object (i.e. remove all the added methods) by running

MyExtension.reset(test);
console.log(test.sum(3)); // sum is not a function

If you need more examples on how to use this look at: