bem
Version:
159 lines (118 loc) • 4.54 kB
JavaScript
(function() {
var ptp = Array.prototype,
toStr = Object.prototype.toString,
methods = {
/**
* Finds the index of an element in an array
* @param {Object} item
* @param {Number} [fromIdx] Starting from index (length - 1 - fromIdx, if fromIdx < 0)
* @returns {Number} Element index or -1, if not found
*/
indexOf : function(item, fromIdx) {
fromIdx = +(fromIdx || 0);
var t = this, len = t.length;
if(len > 0 && fromIdx < len) {
fromIdx = fromIdx < 0? Math.ceil(fromIdx) : Math.floor(fromIdx);
fromIdx < -len && (fromIdx = 0);
fromIdx < 0 && (fromIdx = fromIdx + len);
while(fromIdx < len) {
if(fromIdx in t && t[fromIdx] === item)
return fromIdx;
++fromIdx;
}
}
return -1;
},
/**
* Calls the callback for each element
* @param {Function} callback Called for each element
* @param {Object} [ctx=null] Callback context
*/
forEach : function(callback, ctx) {
var i = -1, t = this, len = t.length;
while(++i < len) i in t &&
(ctx? callback.call(ctx, t[i], i, t) : callback(t[i], i, t));
},
/**
* Creates array B from array A so that B[i] = callback(A[i])
* @param {Function} callback Called for each element
* @param {Object} [ctx=null] Callback context
* @returns {Array}
*/
map : function(callback, ctx) {
var i = -1, t = this, len = t.length,
res = new Array(len);
while(++i < len) i in t &&
(res[i] = ctx? callback.call(ctx, t[i], i, t) : callback(t[i], i, t));
return res;
},
/**
* Creates an array containing only the elements from the source array that the callback returns true for.
* @param {Function} callback Called for each element
* @param {Object} [ctx] Callback context
* @returns {Array}
*/
filter : function(callback, ctx) {
var i = -1, t = this, len = t.length,
res = [];
while(++i < len) i in t &&
(ctx? callback.call(ctx, t[i], i, t) : callback(t[i], i, t)) && res.push(t[i]);
return res;
},
/**
* Wraps the array using an accumulator
* @param {Function} callback Called for each element
* @param {Object} [initialVal] Initial value of the accumulator
* @returns {Object} Accumulator
*/
reduce : function(callback, initialVal) {
var i = -1, t = this, len = t.length,
res;
if(arguments.length < 2) {
while(++i < len) {
if(i in t) {
res = t[i];
break;
}
}
}
else {
res = initialVal;
}
while(++i < len) i in t &&
(res = callback(res, t[i], i, t));
return res;
},
/**
* Checks whether at least one element in the array meets the condition in the callback
* @param {Function} callback
* @param {Object} [ctx=this] Callback context
* @returns {Boolean}
*/
some : function(callback, ctx) {
var i = -1, t = this, len = t.length;
while(++i < len)
if(i in t && (ctx ? callback.call(ctx, t[i], i, t) : callback(t[i], i, t)))
return true;
return false;
},
/**
* Checks whether every element in the array meets the condition in the callback
* @param {Function} callback
* @param {Object} [ctx=this] Context of the callback call
* @returns {Boolean}
*/
every : function(callback, ctx) {
var i = -1, t = this, len = t.length;
while(++i < len)
if(i in t && !(ctx ? callback.call(ctx, t[i], i, t) : callback(t[i], i, t)))
return false;
return true;
}
};
for(var name in methods)
ptp[name] || (ptp[name] = methods[name]);
Array.isArray || (Array.isArray = function(obj) {
return toStr.call(obj) === '[object Array]';
});
})();