loopback-datasource-juggler/lib/list.js

239 lines
5.3 KiB
JavaScript
Raw Normal View History

2012-09-10 15:57:21 +00:00
module.exports = List;
2012-11-20 11:37:04 +00:00
/**
* 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
*/
2012-09-10 15:57:21 +00:00
function List(data, type, parent) {
2012-09-11 16:51:31 +00:00
var list = this;
2013-04-07 13:59:24 +00:00
if (!(list instanceof List)) {
2013-07-12 19:36:14 +00:00
return new List(data, type, parent);
2013-04-07 13:59:24 +00:00
}
if(typeof data === 'string') {
try {
data = JSON.parse(data);
} catch(e) {
throw new Error('could not create List from JSON string: ', data);
}
}
2013-04-07 13:59:24 +00:00
if (data && data instanceof List) data = data.items;
2012-09-11 16:51:31 +00:00
Object.defineProperty(list, 'parent', {
writable: false,
enumerable: false,
configurable: false,
value: parent
});
Object.defineProperty(list, 'nextid', {
writable: true,
enumerable: false,
value: 1
});
data = list.items = data || [];
var Item = list.ItemType = ListItem;
2012-09-10 15:57:21 +00:00
if (typeof type === 'object' && type.constructor.name === 'Array') {
2013-07-12 19:36:14 +00:00
Item = list.ItemType = type[0] || ListItem;
2012-09-10 15:57:21 +00:00
}
2012-11-20 11:37:04 +00:00
data.forEach(function(item, i) {
data[i] = Item(item, list);
2012-09-11 19:22:55 +00:00
Object.defineProperty(list, data[i].id, {
writable: true,
enumerable: false,
configurable: true,
2012-11-20 11:37:04 +00:00
value: data[i]
2012-09-11 19:22:55 +00:00
});
2012-09-11 16:51:31 +00:00
if (list.nextid <= data[i].id) {
list.nextid = data[i].id + 1;
}
2012-09-10 15:57:21 +00:00
});
2012-09-11 16:51:31 +00:00
Object.defineProperty(list, 'length', {
enumerable: false,
configurable: true,
2012-11-20 11:37:04 +00:00
get: function() {
2012-09-11 16:51:31 +00:00
return list.items.length;
}
});
return list;
2012-09-10 15:57:21 +00:00
}
2012-09-11 19:22:55 +00:00
var _;
try {
var underscore = 'underscore';
_ = require(underscore);
2012-09-11 19:22:55 +00:00
} catch (e) {
_ = false;
}
if (_) {
var _import = [
// collection methods
'each',
'map',
'reduce',
'reduceRight',
'find',
'filter',
'reject',
'all',
'any',
'include',
'invoke',
'pluck',
'max',
'min',
'sortBy',
'groupBy',
'sortedIndex',
'shuffle',
'toArray',
'size',
// array methods
'first',
'initial',
'last',
'rest',
'compact',
'flatten',
'without',
'union',
'intersection',
'difference',
'uniq',
'zip',
'indexOf',
'lastIndexOf',
'range'
];
2012-11-20 11:37:04 +00:00
_import.forEach(function(name) {
List.prototype[name] = function() {
2012-09-11 19:22:55 +00:00
var args = [].slice.call(arguments);
args.unshift(this.items);
return _[name].apply(_, args);
};
});
}
['slice', 'forEach', 'filter', 'reduce', 'map'].forEach(function (method) {
2013-02-18 08:18:09 +00:00
var slice = [].slice;
List.prototype[method] = function () {
return Array.prototype[method].apply(this.items, slice.call(arguments));
};
});
List.prototype.find = function(pattern, field) {
if (field) {
var res;
this.items.forEach(function(o) {
if (o[field] == pattern) res = o;
});
return res;
} else {
return this.items[this.items.indexOf(pattern)];
}
};
2013-02-18 08:18:09 +00:00
List.prototype.toObject = function(onlySchema) {
var items = [];
this.items.forEach(function(item) {
if(item.toObject) {
items.push(item.toObject(onlySchema));
} else {
items.push(item);
}
});
return items;
2012-09-10 15:57:21 +00:00
};
2012-11-20 11:37:04 +00:00
List.prototype.toJSON = function() {
return this.toObject(true);
2012-09-11 19:22:55 +00:00
};
2012-11-20 11:37:04 +00:00
List.prototype.toString = function() {
2012-09-11 19:22:55 +00:00
return JSON.stringify(this.items);
};
2012-11-20 11:37:04 +00:00
List.prototype.autoincrement = function() {
2012-09-10 15:57:21 +00:00
return this.nextid++;
};
2012-11-20 11:37:04 +00:00
List.prototype.push = function(obj) {
2012-09-10 15:57:21 +00:00
var item = new ListItem(obj, this);
this.items.push(item);
return item;
};
2012-11-20 11:37:04 +00:00
List.prototype.remove = function(obj) {
2012-09-11 16:51:31 +00:00
var id = obj.id ? obj.id : obj;
var found = false;
2012-11-20 11:37:04 +00:00
this.items.forEach(function(o, i) {
if (id && o.id == id) {
found = i;
if (o.id !== id) {
console.log('WARNING! Type of id not matched');
}
}
2012-09-10 15:57:21 +00:00
});
2012-09-11 16:51:31 +00:00
if (found !== false) {
delete this[id];
this.items.splice(found, 1);
2012-09-10 15:57:21 +00:00
}
};
2012-11-20 11:37:04 +00:00
List.prototype.sort = function(cb) {
2012-09-10 15:57:21 +00:00
return this.items.sort(cb);
};
2012-11-20 11:37:04 +00:00
List.prototype.map = function(cb) {
2012-09-10 15:57:21 +00:00
if (typeof cb === 'function') return this.items.map(cb);
2012-11-20 11:37:04 +00:00
if (typeof cb === 'string') return this.items.map(function(el) {
2012-09-10 15:57:21 +00:00
if (typeof el[cb] === 'function') return el[cb]();
if (el.hasOwnProperty(cb)) return el[cb];
});
};
function ListItem(data, parent) {
if(!(this instanceof ListItem)) {
return new ListItem(data, parent);
}
2013-02-19 08:33:09 +00:00
if (typeof data === 'object') {
for (var i in data) this[i] = data[i];
} else {
this.id = data;
}
2012-09-10 15:57:21 +00:00
Object.defineProperty(this, 'parent', {
writable: false,
enumerable: false,
configurable: true,
value: parent
});
2012-09-11 19:22:55 +00:00
if (!this.id) {
this.id = parent.autoincrement();
}
if (parent.ItemType) {
this.__proto__ = parent.ItemType.prototype;
if (parent.ItemType !== ListItem) {
parent.ItemType.apply(this);
}
}
2012-11-20 11:37:04 +00:00
this.save = function(c) {
2012-09-11 19:22:55 +00:00
parent.parent.save(c);
};
2012-09-10 15:57:21 +00:00
}