.
This commit is contained in:
243
root/res/zui/lib/array/zui.array.js
Normal file
243
root/res/zui/lib/array/zui.array.js
Normal file
@@ -0,0 +1,243 @@
|
||||
/*!
|
||||
* ZUI: 数组辅助方法 - v1.8.1 - 2018-01-18
|
||||
* http://zui.sexy
|
||||
* GitHub: https://github.com/easysoft/zui.git
|
||||
* Copyright (c) 2018 cnezsoft.com; Licensed MIT
|
||||
*/
|
||||
|
||||
/* ========================================================================
|
||||
* ZUI: array.js
|
||||
* Array Polyfill.
|
||||
* http://zui.sexy
|
||||
* ========================================================================
|
||||
* Copyright (c) 2014-2016 cnezsoft.com; Licensed MIT
|
||||
* ======================================================================== */
|
||||
|
||||
// Some polyfills copy from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
|
||||
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
var STR_FUNCTION = 'function';
|
||||
|
||||
/**
|
||||
* Calls a function for each element in the array.
|
||||
*/
|
||||
if(!Array.prototype.forEach) {
|
||||
Array.prototype.forEach = function(fun /*, thisp*/ ) {
|
||||
var len = this.length;
|
||||
if(typeof fun != STR_FUNCTION)
|
||||
throw new TypeError();
|
||||
|
||||
var thisp = arguments[1];
|
||||
for(var i = 0; i < len; i++) {
|
||||
if(i in this) {
|
||||
fun.call(thisp, this[i], i, this);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Judge an object is an real array
|
||||
*/
|
||||
if(!Array.isArray) {
|
||||
Array.isArray = function(obj) {
|
||||
return Object.toString.call(obj) === '[object Array]';
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
|
||||
*/
|
||||
if(!Array.prototype.lastIndexOf) {
|
||||
Array.prototype.lastIndexOf = function(elt /*, from*/ ) {
|
||||
var len = this.length;
|
||||
|
||||
var from = Number(arguments[1]);
|
||||
if(isNaN(from)) {
|
||||
from = len - 1;
|
||||
} else {
|
||||
from = (from < 0) ? Math.ceil(from) : Math.floor(from);
|
||||
if(from < 0)
|
||||
from += len;
|
||||
else if(from >= len)
|
||||
from = len - 1;
|
||||
}
|
||||
|
||||
for(; from > -1; from--) {
|
||||
if(from in this &&
|
||||
this[from] === elt)
|
||||
return from;
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if every element in this array satisfies the provided testing function.
|
||||
*/
|
||||
if(!Array.prototype.every) {
|
||||
Array.prototype.every = function(fun /*, thisp*/ ) {
|
||||
var len = this.length;
|
||||
if(typeof fun != STR_FUNCTION)
|
||||
throw new TypeError();
|
||||
|
||||
var thisp = arguments[1];
|
||||
for(var i = 0; i < len; i++) {
|
||||
if(i in this &&
|
||||
!fun.call(thisp, this[i], i, this))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new array with all of the elements of this array for which the provided filtering function returns true.
|
||||
*/
|
||||
if(!Array.prototype.filter) {
|
||||
Array.prototype.filter = function(fun /*, thisp*/ ) {
|
||||
var len = this.length;
|
||||
if(typeof fun != STR_FUNCTION)
|
||||
throw new TypeError();
|
||||
|
||||
var res = [];
|
||||
var thisp = arguments[1];
|
||||
for(var i = 0; i < len; i++) {
|
||||
if(i in this) {
|
||||
var val = this[i]; // in case fun mutates this
|
||||
if(fun.call(thisp, val, i, this))
|
||||
res.push(val);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
|
||||
*/
|
||||
if(!Array.prototype.indexOf) {
|
||||
Array.prototype.indexOf = function(elt /*, from*/ ) {
|
||||
var len = this.length;
|
||||
|
||||
var from = Number(arguments[1]) || 0;
|
||||
from = (from < 0) ? Math.ceil(from) : Math.floor(from);
|
||||
if(from < 0)
|
||||
from += len;
|
||||
|
||||
for(; from < len; from++) {
|
||||
if(from in this &&
|
||||
this[from] === elt)
|
||||
return from;
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new array with the results of calling a provided function on every element in this array.
|
||||
*/
|
||||
if(!Array.prototype.map) {
|
||||
Array.prototype.map = function(fun /*, thisp*/ ) {
|
||||
var len = this.length;
|
||||
if(typeof fun != STR_FUNCTION)
|
||||
throw new TypeError();
|
||||
|
||||
var res = new Array(len);
|
||||
var thisp = arguments[1];
|
||||
for(var i = 0; i < len; i++) {
|
||||
if(i in this)
|
||||
res[i] = fun.call(thisp, this[i], i, this);
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new array with the results match the condistions
|
||||
* @param {plain object or function} conditions
|
||||
* @param {array} result
|
||||
* @return {array}
|
||||
*/
|
||||
if(!Array.prototype.where) {
|
||||
Array.prototype.where = function(conditions, result) {
|
||||
result = result || [];
|
||||
var cdt, ok, objVal;
|
||||
this.forEach(function(val) {
|
||||
ok = true;
|
||||
for(var key in conditions) {
|
||||
cdt = conditions[key];
|
||||
if(typeof cdt === STR_FUNCTION) {
|
||||
ok = cdt(val);
|
||||
} else {
|
||||
objVal = val[key];
|
||||
ok = (objVal && objVal === cdt);
|
||||
}
|
||||
if(!ok) break;
|
||||
}
|
||||
if(ok) result.push(val);
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a object contains grouped result as object key
|
||||
* @param {string} key
|
||||
* @return {Object}
|
||||
*/
|
||||
if(!Array.prototype.groupBy) {
|
||||
Array.prototype.groupBy = function(key) {
|
||||
var result = {};
|
||||
this.forEach(function(val) {
|
||||
var keyName = val[key];
|
||||
if(!keyName) {
|
||||
keyName = 'unkown';
|
||||
}
|
||||
|
||||
if(!result[keyName]) {
|
||||
result[keyName] = [];
|
||||
}
|
||||
result[keyName].push(val);
|
||||
});
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if at least one element in this array satisfies the provided testing conditions.
|
||||
* @param {function or plain object} conditions
|
||||
* @return {Boolean}
|
||||
*/
|
||||
if(!Array.prototype.has) {
|
||||
Array.prototype.has = function(conditions) {
|
||||
var result = false,
|
||||
cdt, ok, objVal;
|
||||
this.forEach(function(val) {
|
||||
ok = true;
|
||||
for(var key in conditions) {
|
||||
cdt = conditions[key];
|
||||
if(typeof cdt === STR_FUNCTION) {
|
||||
ok = cdt(val);
|
||||
} else {
|
||||
objVal = val[key];
|
||||
ok = (objVal && objVal === cdt);
|
||||
}
|
||||
if(!ok) break;
|
||||
}
|
||||
if(ok) {
|
||||
result = true;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
}());
|
7
root/res/zui/lib/array/zui.array.min.js
vendored
Normal file
7
root/res/zui/lib/array/zui.array.min.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
/*!
|
||||
* ZUI: 数组辅助方法 - v1.8.1 - 2018-01-18
|
||||
* http://zui.sexy
|
||||
* GitHub: https://github.com/easysoft/zui.git
|
||||
* Copyright (c) 2018 cnezsoft.com; Licensed MIT
|
||||
*/
|
||||
!function(){"use strict";var r="function";Array.prototype.forEach||(Array.prototype.forEach=function(t){var o=this.length;if(typeof t!=r)throw new TypeError;for(var e=arguments[1],i=0;i<o;i++)i in this&&t.call(e,this[i],i,this)}),Array.isArray||(Array.isArray=function(r){return"[object Array]"===Object.toString.call(r)}),Array.prototype.lastIndexOf||(Array.prototype.lastIndexOf=function(r){var t=this.length,o=Number(arguments[1]);for(isNaN(o)?o=t-1:(o=o<0?Math.ceil(o):Math.floor(o),o<0?o+=t:o>=t&&(o=t-1));o>-1;o--)if(o in this&&this[o]===r)return o;return-1}),Array.prototype.every||(Array.prototype.every=function(t){var o=this.length;if(typeof t!=r)throw new TypeError;for(var e=arguments[1],i=0;i<o;i++)if(i in this&&!t.call(e,this[i],i,this))return!1;return!0}),Array.prototype.filter||(Array.prototype.filter=function(t){var o=this.length;if(typeof t!=r)throw new TypeError;for(var e=[],i=arguments[1],n=0;n<o;n++)if(n in this){var a=this[n];t.call(i,a,n,this)&&e.push(a)}return e}),Array.prototype.indexOf||(Array.prototype.indexOf=function(r){var t=this.length,o=Number(arguments[1])||0;for(o=o<0?Math.ceil(o):Math.floor(o),o<0&&(o+=t);o<t;o++)if(o in this&&this[o]===r)return o;return-1}),Array.prototype.map||(Array.prototype.map=function(t){var o=this.length;if(typeof t!=r)throw new TypeError;for(var e=new Array(o),i=arguments[1],n=0;n<o;n++)n in this&&(e[n]=t.call(i,this[n],n,this));return e}),Array.prototype.where||(Array.prototype.where=function(t,o){o=o||[];var e,i,n;return this.forEach(function(a){i=!0;for(var y in t)if(e=t[y],typeof e===r?i=e(a):(n=a[y],i=n&&n===e),!i)break;i&&o.push(a)}),o}),Array.prototype.groupBy||(Array.prototype.groupBy=function(r){var t={};return this.forEach(function(o){var e=o[r];e||(e="unkown"),t[e]||(t[e]=[]),t[e].push(o)}),t}),Array.prototype.has||(Array.prototype.has=function(t){var o,e,i,n=!1;return this.forEach(function(a){e=!0;for(var y in t)if(o=t[y],typeof o===r?e=o(a):(i=a[y],e=i&&i===o),!e)break;if(e)return n=!0,!1}),n})}();
|
Reference in New Issue
Block a user