This commit is contained in:
2021-10-03 20:06:12 +03:00
parent dee5c19f48
commit a0a6a9b710
11 changed files with 459 additions and 168 deletions
+106 -10
View File
@@ -1,9 +1,9 @@
/** @module collection */
kit.PluginManager.register('collection',function(module, exports, require, global) {
function valueEx(value) {
function valueEx(value, item, key) {
if(typeof value === 'function') {
return value();
return value(item, key);
}
return value;
}
@@ -18,7 +18,8 @@ kit.PluginManager.register('collection',function(module, exports, require, globa
idStr += (Math.floor((Math.random() * 35))).toString(36);
} while (idStr.length < length);
return (idStr);
};
}
var ArrayCollection = function (data){
this.data = data;
@@ -28,7 +29,9 @@ kit.PluginManager.register('collection',function(module, exports, require, globa
};
ArrayCollection.prototype.put = function (key, value) {
this.data[key] = value;
return this;
};
ArrayCollection.prototype.set = ArrayCollection.prototype.put;
ArrayCollection.prototype.push = function (value) {
this.data.push(value);
};
@@ -36,11 +39,36 @@ kit.PluginManager.register('collection',function(module, exports, require, globa
if(this.data.hasOwnProperty(key)) {
return this.data[key];
}
return valueEx(defValue);
return valueEx(defValue, this.data[key], key);
};
ArrayCollection.prototype.has = function (key) {
return this.data.hasOwnProperty(key);
};
ArrayCollection.prototype.count = function () {
return Object.keys(this.data).length;
};
ArrayCollection.prototype.isEmpty = function () {
return this.count() === 0;
};
ArrayCollection.prototype.map = function (callback) {
for (var key of Object.keys(this.data)) {
this.data[key] = callback(this.data[key], key, this.data);
}
return this;
};
ArrayCollection.prototype.each = function (callback) {
var BreakException = {};
try {
for (var key of Object.keys(this.data)) {
if(callback(this.data[key], key, this.data) === false) {
throw BreakException;
}
}
} catch (e) {
if (e !== BreakException) throw e;
}
return this;
};
var ObjectCollection = function (data){
@@ -48,26 +76,94 @@ kit.PluginManager.register('collection',function(module, exports, require, globa
};
ObjectCollection.prototype.all = ArrayCollection.prototype.all;
ObjectCollection.prototype.put = ArrayCollection.prototype.put;
ObjectCollection.prototype.set = ArrayCollection.prototype.put;
ObjectCollection.prototype.push = function (value) {
this.data[uniqueId(8)] = value;
return this;
};
ObjectCollection.prototype.get = ArrayCollection.prototype.get;
ObjectCollection.prototype.has = ArrayCollection.prototype.has;
ObjectCollection.prototype.count = ArrayCollection.prototype.count;
ObjectCollection.prototype.isEmpty = ArrayCollection.prototype.isEmpty;
ObjectCollection.prototype.map = ArrayCollection.prototype.map;
ObjectCollection.prototype.each = ArrayCollection.prototype.each;
var NodeCollection = function (data){
this.data = data;
};
NodeCollection.prototype.all = ArrayCollection.prototype.all;
NodeCollection.prototype.put = ArrayCollection.prototype.put;
NodeCollection.prototype.set = ArrayCollection.prototype.put;
NodeCollection.prototype.push = ArrayCollection.prototype.push;
NodeCollection.prototype.get = ArrayCollection.prototype.get;
NodeCollection.prototype.has = ArrayCollection.prototype.has;
NodeCollection.prototype.count = ArrayCollection.prototype.count;
NodeCollection.prototype.isEmpty = ArrayCollection.prototype.isEmpty;
NodeCollection.prototype.map = ArrayCollection.prototype.map;
NodeCollection.prototype.each = ArrayCollection.prototype.each;
function collectionCall(method, args) {
if(this.__driver.__proto__.hasOwnProperty(method)) {
var r = this.__driver[method].apply(this.__driver, args);
return r === this.__driver ? this : r;
}
return this;
}
/**
* @extends ArrayCollection
* @memberof kitCore
* @instance
*/
var Collection = function (data) {
if(typeof data === 'object') {
return new ObjectCollection(data);
if(data instanceof NodeList) {
this.__driver = new NodeCollection(data);
}
if(Array.isArray(data)) {
return new ArrayCollection(data);
else if(Array.isArray(data)) {
this.__driver = new ArrayCollection(data);
}
return new ArrayCollection([data]);
}
else if(typeof data === 'object') {
this.__driver = new ObjectCollection(data);
}
else {
console.warn('collection: not support data', data);
}
};
Collection.prototype.all = function () {
return collectionCall.call(this, 'all', arguments);
};
Collection.prototype.put = function (key, value) {
return collectionCall.call(this, 'put', arguments);
};
Collection.prototype.set = function (key, value) {
return collectionCall.call(this, 'set', arguments);
};
Collection.prototype.push = function (value) {
return collectionCall.call(this, 'push', arguments);
};
Collection.prototype.get = function (key, defValue = undefined) {
return collectionCall.call(this, 'get', arguments);
};
Collection.prototype.has = function (key) {
return collectionCall.call(this, 'has', arguments);
};
Collection.prototype.count = function () {
return collectionCall.call(this, 'count', arguments);
};
Collection.prototype.isEmpty = function () {
return collectionCall.call(this, 'isEmpty', arguments);
};
Collection.prototype.map = function (callback) {
return collectionCall.call(this, 'map', arguments);
};
Collection.prototype.each = function (callback) {
return collectionCall.call(this, 'each', arguments);
};
return module.exports = Collection;
});