Files
kit/assets/collection/collection.js
T
2021-10-03 20:06:12 +03:00

170 lines
5.8 KiB
JavaScript

/** @module collection */
kit.PluginManager.register('collection',function(module, exports, require, global) {
function valueEx(value, item, key) {
if(typeof value === 'function') {
return value(item, key);
}
return value;
}
function uniqueId (length = 32, firstLetter = true) {
var idStr = '';
if(length < 1) return idStr;
if(firstLetter) {
length && (idStr = (Math.floor((Math.random() * 25)) + 10).toString(36));
if (length === 1) return idStr;
}
do {
idStr += (Math.floor((Math.random() * 35))).toString(36);
} while (idStr.length < length);
return (idStr);
}
var ArrayCollection = function (data){
this.data = data;
};
ArrayCollection.prototype.all = function () {
return this.data;
};
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);
};
ArrayCollection.prototype.get = function (key, defValue = undefined) {
if(this.data.hasOwnProperty(key)) {
return this.data[key];
}
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){
this.data = data;
};
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(data instanceof NodeList) {
this.__driver = new NodeCollection(data);
}
else if(Array.isArray(data)) {
this.__driver = 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;
});