2014-02-14 07:42:21 +00:00
|
|
|
var util = require('util');
|
|
|
|
|
2012-09-10 15:57:21 +00:00
|
|
|
module.exports = List;
|
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
function List(items, itemType, parent) {
|
2014-01-24 17:09:53 +00:00
|
|
|
var list = this;
|
|
|
|
if (!(list instanceof List)) {
|
2014-02-14 07:42:21 +00:00
|
|
|
return new List(items, itemType, parent);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
if (typeof items === 'string') {
|
2014-01-24 17:09:53 +00:00
|
|
|
try {
|
2014-02-14 07:42:21 +00:00
|
|
|
items = JSON.parse(items);
|
2014-01-24 17:09:53 +00:00
|
|
|
} catch (e) {
|
2014-02-14 07:42:21 +00:00
|
|
|
throw new Error('could not create List from JSON string: ', items);
|
2013-07-01 20:16:51 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
var arr = [];
|
|
|
|
arr.__proto__ = List.prototype;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
items = items || [];
|
|
|
|
if (!Array.isArray(items)) {
|
|
|
|
throw new Error('Items must be an array: ' + items);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!itemType) {
|
|
|
|
itemType = items[0] && items[0].constructor;
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
if (Array.isArray(itemType)) {
|
|
|
|
itemType = itemType[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperty(arr, 'itemType', {
|
2014-01-24 17:09:53 +00:00
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
2014-02-14 07:42:21 +00:00
|
|
|
value: itemType
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
if (parent) {
|
|
|
|
Object.defineProperty(arr, 'parent', {
|
2014-01-24 17:09:53 +00:00
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
2014-02-14 07:42:21 +00:00
|
|
|
value: parent
|
2012-09-11 16:51:31 +00:00
|
|
|
});
|
2014-02-14 07:42:21 +00:00
|
|
|
}
|
2012-09-10 15:57:21 +00:00
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
items.forEach(function (item, i) {
|
|
|
|
if (itemType && !(item instanceof itemType)) {
|
|
|
|
arr[i] = itemType(item);
|
|
|
|
} else {
|
|
|
|
arr[i] = item;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
});
|
2012-09-11 16:51:31 +00:00
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
return arr;
|
2012-09-10 15:57:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
util.inherits(List, Array);
|
2012-09-11 19:22:55 +00:00
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
var _push = List.prototype.push;
|
2012-09-11 19:22:55 +00:00
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
List.prototype.push = function (obj) {
|
|
|
|
var item = this.itemType && (obj instanceof this.itemType) ? obj : this.itemType(obj);
|
|
|
|
_push.call(this, item);
|
|
|
|
return item;
|
2013-04-03 19:52:19 +00:00
|
|
|
};
|
2013-02-18 08:18:09 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
List.prototype.toObject = function (onlySchema) {
|
|
|
|
var items = [];
|
2014-02-14 07:42:21 +00:00
|
|
|
this.forEach(function (item) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (item.toObject) {
|
|
|
|
items.push(item.toObject(onlySchema));
|
|
|
|
} else {
|
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return items;
|
2012-09-10 15:57:21 +00:00
|
|
|
};
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
List.prototype.toJSON = function () {
|
|
|
|
return this.toObject(true);
|
2012-09-11 19:22:55 +00:00
|
|
|
};
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
List.prototype.toString = function () {
|
2014-02-14 07:42:21 +00:00
|
|
|
return JSON.stringify(this.toJSON());
|
2012-09-11 19:22:55 +00:00
|
|
|
};
|
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
/*
|
|
|
|
var strArray = new List(['1', 2], String);
|
|
|
|
strArray.push(3);
|
|
|
|
console.log(strArray);
|
|
|
|
console.log(strArray.length);
|
2012-09-10 15:57:21 +00:00
|
|
|
|
2014-02-14 07:42:21 +00:00
|
|
|
console.log(strArray.toJSON());
|
|
|
|
console.log(strArray.toString());
|
|
|
|
*/
|