mirror of
https://bitbucket.org/bellord13/kit.git
synced 2026-11-13 19:06:11 +03:00
230 lines
6.4 KiB
JavaScript
230 lines
6.4 KiB
JavaScript
/** @module collection */
|
|
/**
|
|
* в разработке
|
|
*/
|
|
kit.PluginManager.register('Collection',function(module, exports, require, global) {
|
|
|
|
var support = require('support');
|
|
var valueEx = support.valueEx;
|
|
|
|
function filter(value, key, arr) {
|
|
return !support.empty(value);
|
|
}
|
|
function getSimilarInstance(type) {
|
|
if(type === 'object') {
|
|
return new Collection({});
|
|
}
|
|
return new Collection([]);
|
|
}
|
|
/**
|
|
* @class Collection
|
|
* @memberOf kit
|
|
*/
|
|
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 {kit.Collection}
|
|
*/
|
|
Collection.prototype.set = Collection.prototype.put = function (key, value) {
|
|
this.data[key] = value;
|
|
return this;
|
|
};
|
|
/**
|
|
* @param value
|
|
* @return {kit.Collection}
|
|
*/
|
|
Collection.prototype.push = function (value) {
|
|
if(this.type === 'Array') {
|
|
this.data.push(value);
|
|
return this;
|
|
}
|
|
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 defValue;
|
|
};
|
|
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 {kit.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 {kit.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 {kit.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 {kit.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.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) {
|
|
if(valueEx(callback, value, key, arr)) {
|
|
res = value;
|
|
return false;
|
|
}
|
|
});
|
|
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;
|
|
};
|
|
Collection.prototype.forget = function (key) {
|
|
key = '' + valueEx(key);
|
|
if(!this.has(key)) {
|
|
return this;
|
|
}
|
|
switch (this.type) {
|
|
case 'Array':
|
|
case 'object':
|
|
delete this.data[key];
|
|
break;
|
|
case 'NodeList':
|
|
var newCollection = this.filter(function (v, k, arr) {
|
|
return key !== Number(k).toString();
|
|
});
|
|
this.data = newCollection.data;
|
|
break;
|
|
default:
|
|
}
|
|
return this;
|
|
};
|
|
/**
|
|
* @return {kit.Collection}
|
|
*/
|
|
Collection.prototype.keys = function () {
|
|
return new Collection(Object.keys(this.data));
|
|
};
|
|
/**
|
|
* @return {kit.Collection}
|
|
*/
|
|
Collection.prototype.dump = Collection.prototype.dd = function () {
|
|
console.log('Collection', this.data);
|
|
return this;
|
|
};
|
|
|
|
return module.exports = Collection;
|
|
});
|
|
|
|
|