Docs and style in lib/list

This commit is contained in:
Anatoliy Chakkaev 2012-11-20 18:37:04 +07:00
parent 26b299eb62
commit c252926f34
1 changed files with 25 additions and 17 deletions

View File

@ -1,6 +1,14 @@
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) {
var list = this;
@ -24,13 +32,13 @@ function List(data, type, parent) {
list.ItemType = type[0] || ListItem;
}
data.forEach(function (item, i) {
data.forEach(function(item, i) {
data[i] = new Item(item, list);
Object.defineProperty(list, data[i].id, {
writable: true,
enumerable: false,
configurable: true,
value: data[i]
value: data[i]
});
if (list.nextid <= data[i].id) {
list.nextid = data[i].id + 1;
@ -40,7 +48,7 @@ function List(data, type, parent) {
Object.defineProperty(list, 'length', {
enumerable: false,
configurable: true,
get: function () {
get: function() {
return list.items.length;
}
});
@ -97,8 +105,8 @@ if (_) {
'range'
];
_import.forEach(function (name) {
List.prototype[name] = function () {
_import.forEach(function(name) {
List.prototype[name] = function() {
var args = [].slice.call(arguments);
args.unshift(this.items);
return _[name].apply(_, args);
@ -106,32 +114,32 @@ if (_) {
});
}
List.prototype.toObject = function () {
List.prototype.toObject = function() {
return this.items;
};
List.prototype.toJSON = function () {
List.prototype.toJSON = function() {
return this.items;
};
List.prototype.toString = function () {
List.prototype.toString = function() {
return JSON.stringify(this.items);
};
List.prototype.autoincrement = function () {
List.prototype.autoincrement = function() {
return this.nextid++;
};
List.prototype.push = function (obj) {
List.prototype.push = function(obj) {
var item = new ListItem(obj, this);
this.items.push(item);
return item;
};
List.prototype.remove = function (obj) {
List.prototype.remove = function(obj) {
var id = obj.id ? obj.id : obj;
var found = false;
this.items.forEach(function (o, i) {
this.items.forEach(function(o, i) {
if (id && o.id == id) {
found = i;
if (o.id !== id) {
@ -145,17 +153,17 @@ List.prototype.remove = function (obj) {
}
};
List.prototype.forEach = function (cb) {
List.prototype.forEach = function(cb) {
this.items.forEach(cb);
};
List.prototype.sort = function (cb) {
List.prototype.sort = function(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 === '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 (el.hasOwnProperty(cb)) return el[cb];
});
@ -179,7 +187,7 @@ function ListItem(data, parent) {
}
}
this.save = function (c) {
this.save = function(c) {
parent.parent.save(c);
};
}