This commit is contained in:
2021-10-06 22:13:08 +03:00
parent 51fe7cf2ff
commit e6eda2d0be
14 changed files with 165 additions and 54 deletions
+30 -34
View File
@@ -1,35 +1,11 @@
/** @module collection */
kit.PluginManager.register('collection',function(module, exports, require, global) {
kit.PluginManager.register('Collection',function(module, exports, require, global) {
var support = require('support');
var valueEx = support.valueEx;
function valueEx(value, item, key, arr) {
if(typeof value === 'function') {
return value(item, key, arr);
}
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);
}
function empty(val){
return arguments.length === 0 ||
val === undefined ||
val === null ||
Number.isNaN(val) === true ||
val === '' ||
val === 0 ||
val === false;
}
function filter(value, key, arr) {
return !empty(value);
return !support.empty(value);
}
function getSimilarInstance(type) {
if(type === 'object') {
@@ -38,10 +14,10 @@ kit.PluginManager.register('collection',function(module, exports, require, globa
return new Collection([]);
}
/**
* @memberof kitCore
* @instance
* @class Collection
* @memberOf kit
*/
var Collection = function (data) {
var Collection = function (data = []) {
this.data = data;
if(data instanceof NodeList) {
this.type = 'NodeList';
@@ -81,14 +57,14 @@ kit.PluginManager.register('collection',function(module, exports, require, globa
this.data.push(value);
return this;
}
this.data[uniqueId(8)] = value;
this.data[support.uniqueId(8)] = value;
return this;
};
Collection.prototype.get = function (key, defValue = undefined) {
if(this.data.hasOwnProperty(key)) {
return this.data[key];
}
return valueEx(defValue, this.data[key], key);
return defValue;
};
Collection.prototype.has = function (key) {
return this.data.hasOwnProperty(key);
@@ -180,6 +156,16 @@ kit.PluginManager.register('collection',function(module, exports, require, globa
});
return res;
};
Collection.prototype.firstKey = function (callback = true) {
var res = undefined;
this.each(function (value, key, arr) {
if(valueEx(callback, value, key, arr)) {
res = key;
return false;
}
});
return res;
};
Collection.prototype.last = function (callback = true) {
var res = undefined;
this.eachReverse(function (value, key, arr) {
@@ -190,6 +176,16 @@ kit.PluginManager.register('collection',function(module, exports, require, globa
});
return res;
};
Collection.prototype.lastKey = function (callback = true) {
var res = undefined;
this.eachReverse(function (value, key, arr) {
if(valueEx(callback, value, key, arr)) {
res = key;
return false;
}
});
return res;
};
/**
* @return {kitCore.Collection}
*/