Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/p-baleine/node-forwardable

node-forwardable
https://github.com/p-baleine/node-forwardable

Last synced: 21 days ago
JSON representation

node-forwardable

Awesome Lists containing this project

README

        

node-forwardable
================

Ruby's forwardable inspired module which defines delegatable functionality of methods for a class.

## Install
npm install forwardable

## Example

var util = require('util')
, forwardable = require('../').forwardable
, log = console.log.bind(console);

var Queue = function() {
this.q = []; // prepare delegate object
};

util._extend(Queue, forwardable);

// setup preferred interface, enq() and deq()...
Queue.defDelegator('q', 'push', 'enq');
Queue.defDelegator('q', 'shift', 'deq');

// support some general Array methods that fit Queues well
Queue.defDelegators('q', 'push', 'shift');

var q = new Queue;
q.enq(1, 2, 3, 4, 5);
q.push(6);

log(q.shift()); //=> 1
while (q.q.length > 0) {
log(q.deq()); //=> 2, 3, 4, 5, 6
}