This commit is contained in:
2021-10-11 11:14:25 +03:00
parent 46ef6b48ec
commit 076141ad80
2 changed files with 16 additions and 16 deletions
+16 -14
View File
@@ -19,15 +19,15 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
function setLastIndex(key){ function setLastIndex(key){
var n = Number(key); var n = Number(key);
if (support.arr.isKey(key) && n > this.lastIndex) { if (support.arr.isKey(key) && n > this.__lastIndex) {
this.lastIndex = n; this.__lastIndex = n;
} }
return this.lastIndex; return this.__lastIndex;
} }
function actualParams() { function actualParams() {
var index; var index;
this.length = this.__keys.length; this.length = this.__keys.length;
this.lastIndex = 0; this.__lastIndex = 0;
for(index in this.__keys) { for(index in this.__keys) {
setLastIndex.call(this, this.__keys[index]); setLastIndex.call(this, this.__keys[index]);
} }
@@ -39,21 +39,21 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
var Collection = function (data = []) { var Collection = function (data = []) {
this.data = data; this.data = data;
if(Array.isArray(data)) { if(Array.isArray(data)) {
this.type = 'array'; this.__type = 'array';
} }
else if(typeof data === 'object') { else if(typeof data === 'object') {
this.type = 'object'; this.__type = 'object';
if(data.hasOwnProperty('length') && !Object.keys(data).includes('length')) { if(data.hasOwnProperty('length') && !Object.keys(data).includes('length')) {
this.type = 'arrayObject'; this.__type = 'arrayObject';
} }
} }
else { else {
this.type = 'array'; this.__type = 'array';
this.data = [data]; this.data = [data];
console.warn('collection: not support data' + typeof data); console.warn('collection: not support data' + typeof data);
} }
var key; var key;
this.lastIndex = 0; this.__lastIndex = 0;
this.length = 0; this.length = 0;
this.__keys = []; this.__keys = [];
this.__values = []; this.__values = [];
@@ -66,7 +66,7 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
}; };
Collection.prototype.all = function () { Collection.prototype.all = function () {
if(this.type === 'array' || this.type === 'arrayObject') { if(this.__type === 'array' || this.__type === 'arrayObject') {
return this.toArray(); return this.toArray();
} }
return this.toObject(); return this.toObject();
@@ -115,7 +115,7 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
* @return {kit.Collection} * @return {kit.Collection}
*/ */
Collection.prototype.push = function (value) { Collection.prototype.push = function (value) {
this.set(this.lastIndex + 1, value); this.set(this.__lastIndex + 1, value);
return this; return this;
}; };
Collection.prototype.get = function (key, defValue = undefined) { Collection.prototype.get = function (key, defValue = undefined) {
@@ -136,7 +136,7 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
* @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)) { for (var key of Object.keys(this.data)) {
res.put(key, valueEx(callback, this.data[key], key, this.data)); res.put(key, valueEx(callback, this.data[key], key, this.data));
} }
@@ -185,7 +185,7 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
* @return {kit.Collection} * @return {kit.Collection}
*/ */
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);
@@ -322,7 +322,9 @@ kit.PluginManager.register('Collection', function(module, exports, require, glob
console.log('Collection', this.data); console.log('Collection', this.data);
return this; return this;
}; };
/**
* @return {kit.Collection}
*/
Collection.prototype.dd = function () { Collection.prototype.dd = function () {
this.dump(); this.dump();
debugger; debugger;
-2
View File
@@ -179,8 +179,6 @@ kit.ready(['collection'], function () {
}); });
</script> </script>