Skip to content

Commit

Permalink
Collections: make methods mixin.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed May 17, 2015
1 parent add19d1 commit 084b028
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 74 deletions.
3 changes: 2 additions & 1 deletion lib/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var util = require("util");
// var os = require("os");
var chalk = require("chalk");
var _ = require("lodash");
var collections = require("../lib/mixins/collections");
var __ = require("../lib/fn.js");
var Repl = require("../lib/repl.js");
var Options = require("../lib/board.options.js");
Expand Down Expand Up @@ -1012,7 +1013,7 @@ Board.Array = function(ports) {

util.inherits(Board.Array, Emitter);

Board.Array.prototype.each = Array.prototype.forEach;
Object.assign(Board.Array.prototype, collections);


if (IS_TEST_MODE) {
Expand Down
24 changes: 3 additions & 21 deletions lib/esc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ var Board = require("../lib/board");
var Pins = Board.Pins;
var Emitter = require("events").EventEmitter;
var util = require("util");
var collections = require("../lib/mixins/collections");
var __ = require("./fn");
var nanosleep = require("../lib/sleep.js").nano;
var nanosleep = require("./sleep").nano;

var priv = new Map();

Expand Down Expand Up @@ -456,26 +457,7 @@ ESC.Array = function(numsOrObjects) {
}, this);
};

/**
* each Execute callbackFn for each active esc instance
*
* eg.
* array.each(function( esc, index ) {
* `this` refers to the current esc instance
* });
*
* @param {[type]} callbackFn [description]
* @return {[type]} [description]
*/
ESC.Array.prototype.each = function(callbackFn) {
var length = this.length;

for (var i = 0; i < length; i++) {
callbackFn.call(this[i], this[i], i);
}

return this;
};
Object.assign(ESC.Array.prototype, collections);

/**
*
Expand Down
26 changes: 8 additions & 18 deletions lib/led/leds.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Led = require("./led");
var collections = require("../mixins/collections");

/**
* Leds()
Expand All @@ -9,11 +10,15 @@ var Led = require("./led");
* @constructor
* @return {Leds}
*/
var Leds = function(numsOrObjects) {
function Leds(numsOrObjects) {
if (!(this instanceof Leds)) {
return new Leds(numsOrObjects);
}

Object.defineProperty(this, "type", {
value: Led
});

var pins = [];

if (numsOrObjects) {
Expand All @@ -31,24 +36,9 @@ var Leds = function(numsOrObjects) {
pins.forEach(function(pin, index) {
this[index] = pin;
}, this);
};


/**
* each Execute callbackFn for each active led instance in an Leds
* @param {Function} callbackFn
* @return {Leds}
*/
Leds.prototype.each = function(callbackFn) {
var length = this.length;

for (var i = 0; i < length; i++) {
callbackFn.call(this[i], this[i], i);
}

return this;
};
}

Object.assign(Leds.prototype, collections);

[

Expand Down
25 changes: 25 additions & 0 deletions lib/mixins/collections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var mixins = {
push: function() {
var length = this.length;
var aLen = arguments.length;

for (var i = 0; i < aLen; i++) {
if (arguments[i] instanceof this.type) {
this[length++] = arguments[i];
}
}

return (this.length = length);
},
each: function(callbackFn) {
var length = this.length;

for (var i = 0; i < length; i++) {
callbackFn.call(this[i], this[i], i);
}

return this;
}
};

module.exports = mixins;
20 changes: 6 additions & 14 deletions lib/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var Board = require("../lib/board.js");
var Descriptor = require("descriptor");
var Emitter = require("events").EventEmitter;
var util = require("util");
var collections = require("../lib/mixins/collections");

var priv = new Map();
var modes = {
Expand Down Expand Up @@ -301,6 +302,10 @@ Pin.Array = function(numsOrObjects) {
return new Pin.Array(numsOrObjects);
}

Object.defineProperty(this, "type", {
value: Pin
});

var items = [];

if (numsOrObjects) {
Expand All @@ -322,20 +327,7 @@ Pin.Array = function(numsOrObjects) {
}, this);
};

/**
* each Execute callbackFn for each active led instance in an Pin.Array
* @param {Function} callbackFn
* @return {Pin.Array}
*/
Pin.Array.prototype.each = function(callbackFn) {
var length = this.length;

for (var i = 0; i < length; i++) {
callbackFn.call(this[i], this[i], i);
}

return this;
};
Object.assign(Pin.Array.prototype, collections);

[

Expand Down
26 changes: 6 additions & 20 deletions lib/servo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var Board = require("../lib/board.js");
var Pins = Board.Pins;
var Emitter = require("events").EventEmitter;
var util = require("util");
var collections = require("../lib/mixins/collections");
var __ = require("../lib/fn.js");
var nanosleep = require("../lib/sleep.js").nano;
var Animation = require("../lib/animation.js");
Expand Down Expand Up @@ -591,6 +592,10 @@ Servo.Array = function(numsOrObjects) {
return new Servo.Array(numsOrObjects);
}

Object.defineProperty(this, "type", {
value: Servo
});

var items = [];

if (numsOrObjects) {
Expand All @@ -612,26 +617,7 @@ Servo.Array = function(numsOrObjects) {
}, this);
};

/**
* each Execute callbackFn for each active servo instance
*
* eg.
* array.each(function( servo, index ) {
* `this` refers to the current servo instance
* });
*
* @param {[type]} callbackFn [description]
* @return {[type]} [description]
*/
Servo.Array.prototype.each = function(callbackFn) {
var length = this.length;

for (var i = 0; i < length; i++) {
callbackFn.call(this[i], this[i], i);
}

return this;
};
Object.assign(Servo.Array.prototype, collections);

/**
* Servo.Array, center()
Expand Down
64 changes: 64 additions & 0 deletions test/collections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var collections = require("../lib/mixins/collections.js");

function Component(num) {
this.num = num;
}

function Components(numsOrObjects) {
if (!(this instanceof Components)) {
return new Components(numsOrObjects);
}

Object.defineProperty(this, "type", {
value: Component
});

var k = 0;

if (numsOrObjects) {
while (numsOrObjects.length) {
var numOrObject = numsOrObjects.shift();
if (!(numOrObject instanceof Component)) {
numOrObject = new Component(numOrObject);
}
this[k] = numOrObject;
k++;
}
}

this.length = k;
}

Object.assign(Components.prototype, collections);

exports["Collections"] = {
setUp: function(done) {
this.components = new Components([0, 1, 2]);
done();
},
tearDown: function(done) {
done();
},
push: function(test) {
test.expect(3);

test.equal(this.components.length, 3);

this.components.push(null);
test.equal(this.components.length, 3);

this.components.push(new Component(5));
test.equal(this.components.length, 4);

test.done();
},
each: function(test) {
test.expect(3);

this.components.each(function(component) {
test.equal(component, this);
});

test.done();
}
};

0 comments on commit 084b028

Please sign in to comment.