This commit is contained in:
2021-11-21 22:33:57 +03:00
parent 5f02b7a453
commit cafa01c3a5
9 changed files with 311 additions and 164 deletions
+29 -25
View File
@@ -22,27 +22,26 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
return new Collection([]);
}
function setLastIndex(key){
var n = Number(key);
if (support.arr.isKey(key) && n > this.__lastIndex) {
this.__lastIndex = n;
}
return this.__lastIndex;
}
function actualParams() {
var index;
this.length = this.__keys.length;
this.__lastIndex = 0;
for(index in this.__keys) {
setLastIndex.call(this, this.__keys[index]);
}
}
/**
* @class Collection
* @property length
* @memberOf kit
*/
var Collection = function (data = []) {
var self = this;
this.data = data;
support.definePropertyGet(this, 'length', function () {
return self.__keys.length;
});
if(data instanceof Collection) {
this.__type = data.__type;
this.__keys = data.__keys;
this.__values = data.__values;
return;
}
if(Array.isArray(data)) {
this.__type = 'array';
}
@@ -58,18 +57,28 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
console.warn('collection: not support data' + typeof data);
}
var key;
this.__lastIndex = 0;
this.length = 0;
this.__keys = [];
this.__values = [];
for(key in this.data) {
if(!this.data.hasOwnProperty(key)) continue;
setLastIndex.call(this, key);
this.length = this.__keys.push(safeArrayKey(key));
this.__keys.push(safeArrayKey(key));
this.__values.push(this.data[key]);
}
};
/**
* @private
*/
Collection.prototype.__getLastIndex = function () {
var lastIndex = -1, n = 0, index;
for(index in this.__keys) {
n = this.__keys[index];
if (support.isNumber(n) && n > lastIndex) {
lastIndex = n;
}
}
return lastIndex;
};
Collection.prototype.all = function () {
if(this.__type === 'array' || this.__type === 'arrayObject') {
return this.toArray();
@@ -115,7 +124,6 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
}
this.length = this.__keys.push(safeArrayKey(key));
this.data[key] = this.__values[this.length-1] = value;
setLastIndex.call(this, key);
return this;
};
/**
@@ -123,7 +131,7 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
* @return {kit.Collection}
*/
Collection.prototype.push = function (value) {
this.set(this.__lastIndex + 1, value);
this.set(this.__getLastIndex() + 1, value);
return this;
};
Collection.prototype.get = function (key, defValue = undefined) {
@@ -296,7 +304,6 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
var index = this.indexKey(key);
this.__keys.splice(index, 1);
this.__values.splice(index, 1);
actualParams.call(this);
delete this.data[key];
return this;
};
@@ -315,7 +322,6 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
var value = this.__values[index];
this.__keys.splice(index, 1);
this.__values.splice(index, 1);
actualParams.call(this);
delete this.data[key];
return value;
};
@@ -331,7 +337,6 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
var value = this.__values[index];
this.__keys.splice(index, 1);
this.__values.splice(index, 1);
actualParams.call(this);
delete this.data[key];
return value;
};
@@ -347,7 +352,6 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
var value = this.__values[index];
this.__keys.splice(index, 1);
this.__values.splice(index, 1);
actualParams.call(this);
delete this.data[key];
return value;
};