Skip to content

Commit

Permalink
Docs and style in lib/list
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Nov 20, 2012
1 parent 26b299e commit c252926
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions lib/list.js
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,14 @@


module.exports = List; module.exports = List;


/**
* List class provides functionality of nested collection
*
* @param {Array} data - array of items.
* @param {Crap} type - array with some type information? TODO: rework this API.
* @param {AbstractClass} parent - owner of list.
* @constructor
*/
function List(data, type, parent) { function List(data, type, parent) {
var list = this; var list = this;


Expand All @@ -24,13 +32,13 @@ function List(data, type, parent) {
list.ItemType = type[0] || ListItem; list.ItemType = type[0] || ListItem;
} }


data.forEach(function (item, i) { data.forEach(function(item, i) {
data[i] = new Item(item, list); data[i] = new Item(item, list);
Object.defineProperty(list, data[i].id, { Object.defineProperty(list, data[i].id, {
writable: true, writable: true,
enumerable: false, enumerable: false,
configurable: true, configurable: true,
value: data[i] value: data[i]
}); });
if (list.nextid <= data[i].id) { if (list.nextid <= data[i].id) {
list.nextid = data[i].id + 1; list.nextid = data[i].id + 1;
Expand All @@ -40,7 +48,7 @@ function List(data, type, parent) {
Object.defineProperty(list, 'length', { Object.defineProperty(list, 'length', {
enumerable: false, enumerable: false,
configurable: true, configurable: true,
get: function () { get: function() {
return list.items.length; return list.items.length;
} }
}); });
Expand Down Expand Up @@ -97,41 +105,41 @@ if (_) {
'range' 'range'
]; ];


_import.forEach(function (name) { _import.forEach(function(name) {
List.prototype[name] = function () { List.prototype[name] = function() {
var args = [].slice.call(arguments); var args = [].slice.call(arguments);
args.unshift(this.items); args.unshift(this.items);
return _[name].apply(_, args); return _[name].apply(_, args);
}; };
}); });
} }


List.prototype.toObject = function () { List.prototype.toObject = function() {
return this.items; return this.items;
}; };


List.prototype.toJSON = function () { List.prototype.toJSON = function() {
return this.items; return this.items;
}; };


List.prototype.toString = function () { List.prototype.toString = function() {
return JSON.stringify(this.items); return JSON.stringify(this.items);
}; };


List.prototype.autoincrement = function () { List.prototype.autoincrement = function() {
return this.nextid++; return this.nextid++;
}; };


List.prototype.push = function (obj) { List.prototype.push = function(obj) {
var item = new ListItem(obj, this); var item = new ListItem(obj, this);
this.items.push(item); this.items.push(item);
return item; return item;
}; };


List.prototype.remove = function (obj) { List.prototype.remove = function(obj) {
var id = obj.id ? obj.id : obj; var id = obj.id ? obj.id : obj;
var found = false; var found = false;
this.items.forEach(function (o, i) { this.items.forEach(function(o, i) {
if (id && o.id == id) { if (id && o.id == id) {
found = i; found = i;
if (o.id !== id) { if (o.id !== id) {
Expand All @@ -145,17 +153,17 @@ List.prototype.remove = function (obj) {
} }
}; };


List.prototype.forEach = function (cb) { List.prototype.forEach = function(cb) {
this.items.forEach(cb); this.items.forEach(cb);
}; };


List.prototype.sort = function (cb) { List.prototype.sort = function(cb) {
return this.items.sort(cb); return this.items.sort(cb);
}; };


List.prototype.map = function (cb) { List.prototype.map = function(cb) {
if (typeof cb === 'function') return this.items.map(cb); if (typeof cb === 'function') return this.items.map(cb);
if (typeof cb === 'string') return this.items.map(function (el) { if (typeof cb === 'string') return this.items.map(function(el) {
if (typeof el[cb] === 'function') return el[cb](); if (typeof el[cb] === 'function') return el[cb]();
if (el.hasOwnProperty(cb)) return el[cb]; if (el.hasOwnProperty(cb)) return el[cb];
}); });
Expand All @@ -179,7 +187,7 @@ function ListItem(data, parent) {
} }
} }


this.save = function (c) { this.save = function(c) {
parent.parent.save(c); parent.parent.save(c);
}; };
} }
Expand Down

0 comments on commit c252926

Please sign in to comment.