mirror of
https://bitbucket.org/bellord13/kit.git
synced 2026-11-13 19:06:11 +03:00
fix
This commit is contained in:
+147
-106
@@ -1,9 +1,9 @@
|
||||
/** @module collection */
|
||||
kit.PluginManager.register('collection',function(module, exports, require, global) {
|
||||
|
||||
function valueEx(value, item, key) {
|
||||
function valueEx(value, item, key, arr) {
|
||||
if(typeof value === 'function') {
|
||||
return value(item, key);
|
||||
return value(item, key, arr);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -19,149 +19,190 @@ kit.PluginManager.register('collection',function(module, exports, require, globa
|
||||
} while (idStr.length < length);
|
||||
return (idStr);
|
||||
}
|
||||
|
||||
|
||||
var ArrayCollection = function (data){
|
||||
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);
|
||||
}
|
||||
};
|
||||
ArrayCollection.prototype.all = function () {
|
||||
|
||||
Collection.prototype.all = function () {
|
||||
return this.data;
|
||||
};
|
||||
ArrayCollection.prototype.put = function (key, value) {
|
||||
/**
|
||||
* @param key
|
||||
* @param value
|
||||
* @return {kitCore.Collection}
|
||||
*/
|
||||
Collection.prototype.set = Collection.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);
|
||||
/**
|
||||
* @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;
|
||||
};
|
||||
ArrayCollection.prototype.get = function (key, defValue = undefined) {
|
||||
Collection.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) {
|
||||
Collection.prototype.has = function (key) {
|
||||
return this.data.hasOwnProperty(key);
|
||||
};
|
||||
ArrayCollection.prototype.count = function () {
|
||||
Collection.prototype.count = function () {
|
||||
return Object.keys(this.data).length;
|
||||
};
|
||||
ArrayCollection.prototype.isEmpty = function () {
|
||||
Collection.prototype.isEmpty = function () {
|
||||
return this.count() === 0;
|
||||
};
|
||||
ArrayCollection.prototype.map = function (callback) {
|
||||
/**
|
||||
* @param callback
|
||||
* @return {kitCore.Collection}
|
||||
*/
|
||||
Collection.prototype.map = function (callback) {
|
||||
var res = getSimilarInstance(this.type);
|
||||
for (var key of Object.keys(this.data)) {
|
||||
this.data[key] = callback(this.data[key], key, this.data);
|
||||
res.put(key, callback(this.data[key], key, this.data));
|
||||
}
|
||||
return this;
|
||||
return res;
|
||||
};
|
||||
ArrayCollection.prototype.each = function (callback) {
|
||||
var BreakException = {};
|
||||
/**
|
||||
* @param callback
|
||||
* @return {kitCore.Collection}
|
||||
*/
|
||||
Collection.prototype.each = function (callback = true) {
|
||||
var BreakException = {}, keys, i, key;
|
||||
try {
|
||||
for (var key of Object.keys(this.data)) {
|
||||
if(callback(this.data[key], key, this.data) === false) {
|
||||
throw BreakException;
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
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
|
||||
* @param callback
|
||||
* @return {kitCore.Collection}
|
||||
*/
|
||||
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.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;
|
||||
};
|
||||
Collection.prototype.all = function () {
|
||||
return collectionCall.call(this, 'all', arguments);
|
||||
/**
|
||||
* @param callback
|
||||
* @return {kitCore.Collection}
|
||||
*/
|
||||
Collection.prototype.filter = function (callback = filter) {
|
||||
var res = getSimilarInstance(this.type);
|
||||
this.each(function (value, key, arr) {
|
||||
if(callback(value, key, arr)) {
|
||||
res.put(key, value);
|
||||
}
|
||||
});
|
||||
return res;
|
||||
};
|
||||
Collection.prototype.put = function (key, value) {
|
||||
return collectionCall.call(this, 'put', arguments);
|
||||
Collection.prototype.every = function (callback) {
|
||||
var res = true;
|
||||
this.each(function (value, key, arr) {
|
||||
res = res && !!callback(value, key, arr);
|
||||
});
|
||||
return res;
|
||||
};
|
||||
Collection.prototype.set = function (key, value) {
|
||||
return collectionCall.call(this, 'set', arguments);
|
||||
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.push = function (value) {
|
||||
return collectionCall.call(this, 'push', arguments);
|
||||
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.get = function (key, defValue = undefined) {
|
||||
return collectionCall.call(this, 'get', arguments);
|
||||
/**
|
||||
* @return {kitCore.Collection}
|
||||
*/
|
||||
Collection.prototype.keys = function () {
|
||||
return new Collection(Object.keys(this.data));
|
||||
};
|
||||
Collection.prototype.has = function (key) {
|
||||
return collectionCall.call(this, 'has', arguments);
|
||||
/**
|
||||
* @return {kitCore.Collection}
|
||||
*/
|
||||
Collection.prototype.dump = Collection.prototype.dd = function () {
|
||||
console.log('Collection', this.data);
|
||||
return this;
|
||||
};
|
||||
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;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user