From dbe5a25c2c9c70bd845e9c5f0519e8edc1f6ec34 Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Fri, 9 Nov 2012 10:57:17 -0800 Subject: [PATCH 1/2] merged matthewmueller/array support to collection --- component.json | 9 +++-- index.js | 107 ++++++++++++++++++++++++++++++++++++++++++++++--- test.html | 28 +++++++++++++ 3 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 test.html diff --git a/component.json b/component.json index 1aee2ff..ae5531d 100644 --- a/component.json +++ b/component.json @@ -5,11 +5,14 @@ "version": "0.0.1", "keywords": ["enumerable", "data", "model", "db"], "dependencies": { - "component/enumerable": "*" + "component/enumerable": "*", + "component/emitter": "*" + }, + "development": { + "component/model": "*" }, - "development": {}, "license": "MIT", "scripts": [ "index.js" ] -} \ No newline at end of file +} diff --git a/index.js b/index.js index 96f8077..3fc752d 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,8 @@ * Module dependencies. */ -var Enumerable = require('enumerable'); +var Enumerable = require('enumerable'), + Emitter = require('emitter'); /** * Expose `Collection`. @@ -28,6 +29,12 @@ function Collection(models) { Enumerable(Collection.prototype); +/** + * Mixin emitter + */ + +Emitter(Collection.prototype); + /** * Iterator implementation. */ @@ -52,13 +59,101 @@ Collection.prototype.length = function(){ }; /** - * Add `model` to the collection and return the index. + * Removes the last element from an array and returns that element * - * @param {Object} model + * @return {Mixed} removed element + */ + +Collection.prototype.pop = function() { + var ret = this.models.pop.apply(this, arguments); + this.emit('pop', ret); + this.emit('remove', ret); + return ret; +}; + +/** + * Push a value onto the end of the array, + * returning the length of the array + * + * @param {Mixed, ...} elements * @return {Number} - * @api public */ -Collection.prototype.push = function(model){ - return this.models.push(model); +Collection.prototype.push = function() { + var ret = this.models.push.apply(this, arguments), + args = [].slice.call(arguments); + this.emit('push', ret); + for(var i = 0, len = args.length; i < len; i++) this.emit('add', args[i]); + return ret; +}; + +/** + * Reverses an array in place. + * + * @return {Array} + */ + +Collection.prototype.reverse = function() { + var ret = this.models.reverse.apply(this, arguments); + this.emit('reverse', ret); + return ret; +}; + +/** + * Removes the first element from an array and returns that element. + * + * @return {Mixed} + */ + +Collection.prototype.shift = function() { + var ret = this.models.shift.apply(this, arguments); + this.emit('shift', ret); + this.emit('remove', ret); + return ret; +}; + +/** + * Sorts the elements of an array. + * + * @return {Array} + */ + +Collection.prototype.sort = function() { + var ret = this.models.sort.apply(this, arguments); + this.emit('sort', ret); + return ret; +}; + +/** + * Adds and/or removes elements from an array. + * + * @param {Number} index + * @param {Number} howMany + * @param {Mixed, ...} elements + * @return {Array} removed elements + */ + +Collection.prototype.splice = function() { + var ret = this.models.splice.apply(this, arguments), + added = [].slice.call(arguments, 2); + this.emit('splice', ret); + for(var i = 0, len = ret.length; i < len; i++) this.emit('remove', ret[i]); + for( i = 0, len = added.length; i < len; i++) this.emit('add', added[i]); + return ret; +}; + +/** + * Adds one or more elements to the front of an array + * and returns the new length of the array. + * + * @param {Mixed, ...} elements + * @return {Number} length + */ + +Collection.prototype.unshift = function() { + var ret = this.models.unshift.apply(this, arguments), + args = [].slice.call(arguments); + this.emit('unshift', ret); + for(var i = 0, len = args.length; i < len; i++) this.emit('add', args[i]); + return ret; }; diff --git a/test.html b/test.html new file mode 100644 index 0000000..d76b43f --- /dev/null +++ b/test.html @@ -0,0 +1,28 @@ + + + collection component + + + +

collection component

+ + + + From 224f0bedddd0cdb4db05f996cc9159475bd4962f Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Sun, 11 Nov 2012 12:48:01 -0800 Subject: [PATCH 2/2] fixed --- index.js | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 3fc752d..73e7e89 100644 --- a/index.js +++ b/index.js @@ -65,7 +65,8 @@ Collection.prototype.length = function(){ */ Collection.prototype.pop = function() { - var ret = this.models.pop.apply(this, arguments); + var models = this.models, + ret = models.pop.apply(models, arguments); this.emit('pop', ret); this.emit('remove', ret); return ret; @@ -80,7 +81,8 @@ Collection.prototype.pop = function() { */ Collection.prototype.push = function() { - var ret = this.models.push.apply(this, arguments), + var models = this.models, + ret = models.push.apply(models, arguments), args = [].slice.call(arguments); this.emit('push', ret); for(var i = 0, len = args.length; i < len; i++) this.emit('add', args[i]); @@ -94,7 +96,8 @@ Collection.prototype.push = function() { */ Collection.prototype.reverse = function() { - var ret = this.models.reverse.apply(this, arguments); + var models = this.models, + ret = models.reverse.apply(models, arguments); this.emit('reverse', ret); return ret; }; @@ -106,7 +109,8 @@ Collection.prototype.reverse = function() { */ Collection.prototype.shift = function() { - var ret = this.models.shift.apply(this, arguments); + var models = this.models, + ret = this.models.shift.apply(this, arguments); this.emit('shift', ret); this.emit('remove', ret); return ret; @@ -119,7 +123,8 @@ Collection.prototype.shift = function() { */ Collection.prototype.sort = function() { - var ret = this.models.sort.apply(this, arguments); + var models = this.models, + ret = models.sort.apply(models, arguments); this.emit('sort', ret); return ret; }; @@ -134,7 +139,8 @@ Collection.prototype.sort = function() { */ Collection.prototype.splice = function() { - var ret = this.models.splice.apply(this, arguments), + var models = this.models, + ret = models.splice.apply(models, arguments), added = [].slice.call(arguments, 2); this.emit('splice', ret); for(var i = 0, len = ret.length; i < len; i++) this.emit('remove', ret[i]); @@ -151,9 +157,22 @@ Collection.prototype.splice = function() { */ Collection.prototype.unshift = function() { - var ret = this.models.unshift.apply(this, arguments), + var models = this.models, + ret = models.unshift.apply(models, arguments), args = [].slice.call(arguments); this.emit('unshift', ret); for(var i = 0, len = args.length; i < len; i++) this.emit('add', args[i]); return ret; }; + +/** + * Clear a list + * + * @return {Collection} + */ + +Collection.prototype.clear = function() { + this.models = []; + this.emit('clear'); + return this; +};