Files
kit/assets/collection/collection.js
T
2021-10-05 23:16:46 +03:00

211 lines
6.0 KiB
JavaScript

/** @module collection */
kit.PluginManager.register('collection',function(module, exports, require, global) {
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);
}
function getSimilarInstance(type) {
if(type === 'object') {
return new Collection({});
}
return new Collection([]);
}
/**
* @memberof kitCore
* @instance
*/
var Collection = function (data) {
this.data = data;
if(data instanceof NodeList) {
this.type = 'NodeList';
}
else if(Array.isArray(data)) {
this.type = 'Array';
}
else if(typeof data === 'object') {
this.type = 'object';
}
else {
this.type = 'Array';
this.data = [data];
this.type = undefined;
console.warn('collection: not support data' + typeof data);
}
};
Collection.prototype.all = function () {
return this.data;
};
/**
* @param key
* @param value
* @return {kitCore.Collection}
*/
Collection.prototype.set = Collection.prototype.put = function (key, value) {
this.data[key] = value;
return this;
};
/**
* @param value
* @return {kitCore.Collection}
*/
Collection.prototype.push = function (value) {
if(this.type === 'Array') {
this.data.push(value);
return this;
}
this.data[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);
};
Collection.prototype.has = function (key) {
return this.data.hasOwnProperty(key);
};
Collection.prototype.count = function () {
return Object.keys(this.data).length;
};
Collection.prototype.isEmpty = function () {
return this.count() === 0;
};
/**
* @param callback
* @return {kitCore.Collection}
*/
Collection.prototype.map = function (callback) {
var res = getSimilarInstance(this.type);
for (var key of Object.keys(this.data)) {
res.put(key, valueEx(callback, this.data[key], key, this.data));
}
return res;
};
/**
* @param callback
* @return {kitCore.Collection}
*/
Collection.prototype.each = function (callback = true) {
var BreakException = {}, keys, i, key;
try {
keys = Object.keys(this.data);
if(keys.length)
for (i = 0; i < keys.length; i++) {
key = keys[i];
if(valueEx(callback, this.data[key], key, this.data) === false) {
throw BreakException;
}
}
} catch (e) {
if (e !== BreakException) throw e;
}
return this;
};
/**
* @param callback
* @return {kitCore.Collection}
*/
Collection.prototype.eachReverse = function (callback = true) {
var BreakException = {}, keys, i, key;
try {
keys = Object.keys(this.data);
if(keys.length)
for (i = keys.length - 1; i >= 0; i--) {
key = keys[i];
if(valueEx(callback, this.data[key], key, this.data) === false) {
throw BreakException;
}
}
} catch (e) {
if (e !== BreakException) throw e;
}
return this;
};
/**
* @param callback
* @return {kitCore.Collection}
*/
Collection.prototype.filter = function (callback = filter) {
var res = getSimilarInstance(this.type);
this.each(function (value, key, arr) {
if(valueEx(callback, value, key, arr)) {
res.put(key, value);
}
});
return res;
};
Collection.prototype.every = function (callback) {
var res = true;
this.each(function (value, key, arr) {
res = res && !!valueEx(callback, value, key, arr);
});
return res;
};
Collection.prototype.first = function (callback = true) {
var res = undefined;
this.each(function (value, key, arr) {
if(valueEx(callback, value, key, arr)) {
res = value;
return false;
}
});
return res;
};
Collection.prototype.last = function (callback = true) {
var res = undefined;
this.eachReverse(function (value, key, arr) {
if(valueEx(callback, value, key, arr)) {
res = value;
return false;
}
});
return res;
};
/**
* @return {kitCore.Collection}
*/
Collection.prototype.keys = function () {
return new Collection(Object.keys(this.data));
};
/**
* @return {kitCore.Collection}
*/
Collection.prototype.dump = Collection.prototype.dd = function () {
console.log('Collection', this.data);
return this;
};
return module.exports = Collection;
});