From 7adfe55ea579c64b6cabc1591245dd0bb7e16539 Mon Sep 17 00:00:00 2001 From: bellord13 Date: Mon, 11 Oct 2021 22:33:07 +0300 Subject: [PATCH] fix --- assets/collection/collection.js | 74 +++++++++++++++++++++++++++------ tests/kit.Collection.php | 37 +++++++++++++++++ 2 files changed, 99 insertions(+), 12 deletions(-) diff --git a/assets/collection/collection.js b/assets/collection/collection.js index 04670ae..d51304f 100644 --- a/assets/collection/collection.js +++ b/assets/collection/collection.js @@ -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) { diff --git a/tests/kit.Collection.php b/tests/kit.Collection.php index 5843061..2503cf2 100644 --- a/tests/kit.Collection.php +++ b/tests/kit.Collection.php @@ -177,6 +177,43 @@ kit.ready(['collection'], function () { assertSame(x.length, 2, 'length 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. });