This commit is contained in:
2021-10-11 22:33:07 +03:00
parent 076141ad80
commit 7adfe55ea5
2 changed files with 99 additions and 12 deletions
+62 -12
View File
@@ -10,6 +10,14 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
function filter(value, key, arr) { function filter(value, key, arr) {
return !support.empty(value); return !support.empty(value);
} }
function safeArrayKey(key){
return support.arr.isKey(key)?Number(key):(''+key);
}
/**
* @param type
* @return {kit.Collection}
*/
function getSimilarInstance(type) { function getSimilarInstance(type) {
if(type === 'object') { if(type === 'object') {
return new Collection({}); return new Collection({});
@@ -60,7 +68,7 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
for(key in this.data) { for(key in this.data) {
if(!this.data.hasOwnProperty(key)) continue; if(!this.data.hasOwnProperty(key)) continue;
setLastIndex.call(this, key); setLastIndex.call(this, key);
this.length = this.__keys.push(support.arr.isKey(key)?Number(key):(''+key)); this.length = this.__keys.push(safeArrayKey(key));
this.__values.push(this.data[key]); this.__values.push(this.data[key]);
} }
}; };
@@ -89,7 +97,7 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
return JSON.stringify(this.all()); return JSON.stringify(this.all());
}; };
Collection.prototype.has = function (key) { Collection.prototype.has = function (key) {
return this.data.hasOwnProperty(key) && this.__keys.includes(support.arr.isKey(key)?Number(key):(''+key)); return this.data.hasOwnProperty(key) && this.__keys.includes(safeArrayKey(key));
}; };
Collection.prototype.indexKey = function (key) { Collection.prototype.indexKey = function (key) {
return this.__keys.indexOf(key); return this.__keys.indexOf(key);
@@ -105,7 +113,7 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
this.data[key] = this.__values[index = this.indexKey(key)] = value; this.data[key] = this.__values[index = this.indexKey(key)] = value;
return this; return this;
} }
this.length = this.__keys.push(support.arr.isKey(key)?Number(key):(''+key)); this.length = this.__keys.push(safeArrayKey(key));
this.data[key] = this.__values[this.length-1] = value; this.data[key] = this.__values[this.length-1] = value;
setLastIndex.call(this, key); setLastIndex.call(this, key);
return this; return this;
@@ -132,13 +140,18 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
return this.count() === 0; return this.count() === 0;
}; };
/** /**
* возвращает новый экземпляр коллекции; не изменяет вызываемую коллекцию
*
* @param callback * @param callback
* @return {kit.Collection} * @return {kit.Collection}
*/ */
Collection.prototype.map = function (callback) { Collection.prototype.map = function (callback) {
var res = getSimilarInstance(this.__type); var res = getSimilarInstance(this.__type);
for (var key of Object.keys(this.data)) { var index, key, value;
res.put(key, valueEx(callback, this.data[key], key, this.data)); for(index in this.__keys) {
key = this.__keys[index];
value = this.__values[index];
res.put(key, valueEx(callback, value, key, this.data));
} }
return res; return res;
}; };
@@ -147,13 +160,11 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
* @return {kit.Collection} * @return {kit.Collection}
*/ */
Collection.prototype.each = function (callback = true) { Collection.prototype.each = function (callback = true) {
var BreakException = {}, keys, i, key; var BreakException = {}, i;
try { try {
keys = Object.keys(this.data); if(this.__keys.length)
if(keys.length) for (i = 0; i < this.__keys.length; i++) {
for (i = 0; i < keys.length; i++) { if(valueEx(callback, this.__values[i], this.__keys[i], this.data) === false) {
key = keys[i];
if(valueEx(callback, this.data[key], key, this.data) === false) {
throw BreakException; throw BreakException;
} }
} }
@@ -187,12 +198,51 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
Collection.prototype.filter = function (callback = filter) { Collection.prototype.filter = function (callback = filter) {
var res = getSimilarInstance(this.__type); var res = getSimilarInstance(this.__type);
this.each(function (value, key, arr) { this.each(function (value, key, arr) {
if(valueEx(callback, value, key, arr)) { if(!!valueEx(callback, value, key, arr)) {
res.put(key, value); res.put(key, value);
} }
}); });
return res; return res;
}; };
/**
* Метод возвращает все элементы коллекции, кроме тех у кого ключи соответствуют заданным
* @param keys
* @return {kit.Collection}
*/
Collection.prototype.except = function (keys) {
var __keys = [], iKey;
if(!Array.isArray(keys)) {
keys = [keys];
}
for(iKey in keys) {
__keys.push(safeArrayKey(keys[iKey]));
}
return this.filter(function (value, key) {
return !__keys.includes(key);
});
};
/**
* Метод возвращает элементы коллекции с заданными ключами
* @param keys
* @return {kit.Collection}
*/
Collection.prototype.only = function (keys) {
var __keys = [], iKey;
if(!Array.isArray(keys)) {
keys = [keys];
}
for(iKey in keys) {
__keys.push(safeArrayKey(keys[iKey]));
}
return this.filter(function (value, key) {
return __keys.includes(key);
});
};
Collection.prototype.every = function (callback) { Collection.prototype.every = function (callback) {
var res = true; var res = true;
this.each(function (value, key, arr) { this.each(function (value, key, arr) {
+37
View File
@@ -177,6 +177,43 @@ kit.ready(['collection'], function () {
assertSame(x.length, 2, 'length after shift'); assertSame(x.length, 2, 'length after shift');
assertSame(x.last(), 10, 'last after shift'); assertSame(x.last(), 10, 'last after shift');
xOb = {a: 'a10', b: 'b11'};
x = new kit.Collection(xOb);
x2 = x.map(function (value, key) {
return value + '!';
});
assertSame(x2.get('a'), 'a10!', 'map object');
xOb = [10, 20, 30];
x = new kit.Collection(xOb);
x2 = x.map(function (value, key) {
return value * 2;
});
assertSame(x2.last(), 60, 'map array');
xOb = {a: 'a10', b: 'b11', 10: 101};
x = new kit.Collection(xOb);
x.set(11, 111);
x2 = x.except([10]);
assertSame(x2.last(), 111, 'except last+');
assertSame(x2.length, 3, 'except length');
x3 = x2.except('11');
assertSame(x3.last(), 'b11', 'except last+');
assertSame(x3.length, 2, 'except length');
xOb = {a: 'a10', b: 'b11', 10: 101};
x = new kit.Collection(xOb);
x.set(11, 111);
x2 = x.only(['a', 'b', '11']);
assertSame(x2.last(), 111, 'only last+');
assertSame(x2.length, 3, 'only length');
x3 = x2.only('11');
assertSame(x3.last(), 111, 'only last+');
assertSame(x3.length, 1, 'only length');
x.
}); });