Skip to content

Instantly share code, notes, and snippets.

@dsherret
Forked from fatso83/Collection.ts
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsherret/986845d801ded5759515 to your computer and use it in GitHub Desktop.
Save dsherret/986845d801ded5759515 to your computer and use it in GitHub Desktop.
var expect = require('chai').expect;
var sinon = require('sinon');
var Collection = require('./Collection');
describe('constructor', function() {
it('should be instantiated with its arguments', function() {
var c = new Collection(1,2,3,4);
expect(c.length).to.equal(4);
});
});
describe('add', function() {
var c1;
beforeEach(function() {
c1 = new Collection();
});
it('should add only the first argument', function() {
c1.add(1,2,3);
expect(c1.length).to.equal(1);
});
it('should add all the elements of an array', function() {
c1.add([1,2,3]);
expect(c1.length).to.equal(3);
});
});
describe('addAll', function() {
var c2, stub;
beforeEach(function() {
c2 = new Collection();
});
afterEach(function() {
if(stub && stub.restore) stub.restore();
});
it('should add elements using its internal add', function() {
stub = sinon.stub(c2, 'add');
c2.addAll(1,2,3);
expect(stub.called).to.be.true;
});
it('should add all arguments', function() {
c2.addAll(1,2,3,[100,200]);
expect(c2.length).to.equal(5);
expect(c2.pop()).to.equal(200);
});
});
/* Typescript conversion of Ben Nadel's Collection class.
Based on the fact that subclassing Array directly does not work very well,
at least in Ecmascript versions < 6. Instead, this is based on creating a
collection from an array, where the actual methods are added to the array
after creation.
It works exactly like an array, while at the same time having additional
functionality.
*/
// compile with `tsc --module commonjs` to test with mocha
class Collection {
constructor(...initialItems: any[]) {
var collection = Object.create(Array.prototype);
Collection.initialize(collection, initialItems, Collection.prototype);
return collection;
}
static initialize(collection, initialItems: any[], prototype) {
for (var method in prototype) {
if (prototype.hasOwnProperty(method)) {
collection[ method ] = prototype[ method ];
}
}
Array.prototype.push.apply(collection, initialItems);
return collection;
}
add(value): Collection {
if (Array.isArray(value)) {
Array.prototype.push.apply(this, value);
} else {
Array.prototype.push.call(this, value);
}
return this;
}
addAll(...items: any[]): Collection {
for (var i = 0, l = items.length; i < l; i++) {
this.add(items[i]);
}
return this;
}
}
export = Collection;
{
"name": "Collection.ts",
"description": "Typescript conversion of Ben Nadel's original Collection class (http://www.bennadel.com/blog/2292-extending-javascript-arrays-while-keeping-native-bracket-notation-functionality.htm)",
"version": "0.0.1",
"devDependencies": {
"mocha": "^1.21.4",
"chai": "^1.9.1",
"sinon": "^1.10.3"
},
"scripts" : {
"test" : "mocha collection-test.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment