-
Notifications
You must be signed in to change notification settings - Fork 0
List [Retired]
Malexion edited this page Jan 13, 2017
·
1 revision
Note that list has been retired from lib, but feel free to use the code below if you need to create your own modification.
A significantly less efficient array object.
var List = __.class(function(items) {
__.lib.Enumerable.call(this);
this.addRange(items);
}, {
add: function(item, options) {
var hasOptions = options && options.start,
start = hasOptions ? options.start : 0;
while (this.hasOwnProperty(start)) {
start++;
}
this[start] = item;
if (hasOptions) options.start = start;
return this;
},
addRange: function(items) {
if (__.is.array(items) || items instanceof Enumerable) {
var self = this,
opt = { start: 0 };
__.all(items, function (x) {
return self.add(x, opt);
});
}
return this;
},
clear: function() {
var self = this;
__.all(self, function (x, y) {
delete self[y];
});
return self;
},
contains: function(func) {
if (__.is.function(func))
return __.contains(this, func);
return __.contains(this, function (x) {
return x == func;
});
},
count: {
get: function() {
return this.getKeys.length;
},
set: function(value) {
var count = this.count;
if (count > value) this.removeRange(value - 1);
}
},
distinct: function(func) {
var x = this.toArray();
x = __.distinct(x, func);
return new List(x);
},
indexOf: function(item) {
return __.search(this, function (x) {
return x == item;
}, { getKey: true });
},
insert: function(key, item) {
var self = this,
idx = parseInt(key),
keys = __.sort(__.filter(__.map(this.getKeys, function (x) {
return parseInt(x);
}), function (x) {
return x >= idx;
}), { dir: 'desc' });
__.all(keys, function (x) {
self[x + 1] = self[x];
});
self[idx] = item;
return this;
},
insertRange: function(key, items) {
var self = this,
idx = parseInt(key),
count = items.length != undefined ? items.length : items.count,
keys = __.sort(__.filter(__.map(this.getKeys, function (x) {
return parseInt(x);
}), function (x) {
return x >= idx;
}), { dir: 'desc' });
__.all(keys, function (x) {
self[x + count] = self[x];
});
__.all(items, function (x) {
self[idx] = x;idx++;
});
return this;
},
getRange: function(start, end) {
var self = this,
key = 0,
keys = __.map(self.getKeys, function (x) {
return parseInt(x);
}),
begin = parseInt(start),
end = end ? parseInt(end) : keys[keys.length - 1];
return __.map(keys, function (x, y, z) {
if (y >= begin && y <= end) return x;
z.skip = true;
});
},
remove: function(item) {
var idx = this.indexOf(item);
if (idx != null) this.removeAt(idx);
return this;
},
removeAt: function(key) {
var self = this,
idx = parseInt(key),
keys = __.filter(__.map(this.getKeys, function (x) {
return parseInt(x);
}), function (x) {
return x > idx;
});
delete this[idx];
__.all(keys, function (x) {
return self[x - 1] = self[x];
}); // shift all after keys down one
delete this[keys[keys.length - 1]]; // remove tail copy
return this;
},
removeRange: function(start, end) {
var self = this,
keys = __.map(this.getKeys, function (x) {
return parseInt(x);
}),
begin = parseInt(start),
end = end ? parseInt(end) : keys[keys.length - 1];
__.all(__.filter(keys, function (x) {
return x >= begin && x <= end;
}), function (x) {
return delete self[x];
});
if (end < keys[keys.length - 1]) {
__.all(__.filter(keys, function (x) {
return x > end;
}), function (x) {
return self[begin++] = self[x];
});
delete self[keys[keys.length - 1]]; // remove tail copy
}
return this;
},
search: function(func) {
if (__.is.function(func))
return __.search(this, func);
return __.search(this, function (x) {
return x == func;
});
},
select: function(func) {
var x = this.toArray();
x = __.map(x, func);
return new List(x);
},
sort: function(options) {
var x = this.toArray();
x = __.sort(x, options);
var self = this;
__.all(x, function (v, k) {
return self[k] = x;
});
return this;
},
where: function(func) {
var x = this.toArray();
x = __.filter(x, func);
return new List(x);
},
}, __.lib.Enumerable);