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) {
return !support.empty(value);
}
function safeArrayKey(key){
return support.arr.isKey(key)?Number(key):(''+key);
}
/**
* @param type
* @return {kit.Collection}
*/
function getSimilarInstance(type) {
if(type === 'object') {
return new Collection({});
@@ -60,7 +68,7 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
for(key in this.data) {
if(!this.data.hasOwnProperty(key)) continue;
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]);
}
};
@@ -89,7 +97,7 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
return JSON.stringify(this.all());
};
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) {
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;
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;
setLastIndex.call(this, key);
return this;
@@ -132,13 +140,18 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
return this.count() === 0;
};
/**
* возвращает новый экземпляр коллекции; не изменяет вызываемую коллекцию
*
* @param callback
* @return {kit.Collection}
*/
Collection.prototype.map = function (callback) {
var res = getSimilarInstance(this.__type);
for (var key of Object.keys(this.data)) {
res.put(key, valueEx(callback, this.data[key], key, this.data));
var index, key, value;
for(index in this.__keys) {
key = this.__keys[index];
value = this.__values[index];
res.put(key, valueEx(callback, value, key, this.data));
}
return res;
};
@@ -147,13 +160,11 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
* @return {kit.Collection}
*/
Collection.prototype.each = function (callback = true) {
var BreakException = {}, keys, i, key;
var BreakException = {}, i;
try {
keys = Object.keys(this.data);
if(keys.length)
for (i = 0; i < keys.length; i++) {
key = keys[i];
if(valueEx(callback, this.data[key], key, this.data) === false) {
if(this.__keys.length)
for (i = 0; i < this.__keys.length; i++) {
if(valueEx(callback, this.__values[i], this.__keys[i], this.data) === false) {
throw BreakException;
}
}
@@ -187,12 +198,51 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
Collection.prototype.filter = function (callback = filter) {
var res = getSimilarInstance(this.__type);
this.each(function (value, key, arr) {
if(valueEx(callback, value, key, arr)) {
if(!!valueEx(callback, value, key, arr)) {
res.put(key, value);
}
});
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) {
var res = true;
this.each(function (value, key, arr) {