(function(root, undefined) {(function(root, undefined) { var ArrayProto = Array.prototype,
ObjProto = Object.prototype,
toString = ObjProto.toString,
hasOwnProp = ObjProto.hasOwnProperty;The native implementations of the array functions, which will be used if they're available.
var nativeEach = ArrayProto.forEach,
nativeMap = ArrayProto.map,
nativeFilter = ArrayProto.filter,
nativeReduce = ArrayProto.reduce,
nativeSome = ArrayProto.some,
nativeEvery = ArrayProto.every,
nativeIndexOf = ArrayProto.indexOf,
nativeIsArray = Array.isArray,
nativeKeys = Object.keys; var Chunks = {};Execute the iterator on every item in the obj.
The base function used in nearly all of Chunks's other functions.
obj is either an array or an object.iterator is a function.scope is the value of this. Chunks.forEach = Chunks.each = function(obj, iterator, scope) {
if (nativeEach && obj.nativeEach) {
obj.forEach(iterator, scope);
} else if (obj.length !== undefined) {
for (var i = 0, length = obj.length; i < length; i++) {
iterator.call(scope, obj[i], i, obj);
}
} else {
var keys = Chunks.keys(obj);
for (var i = 0, length = keys.length; i < length; i++) {
iterator.call(scope, obj[keys[i]], keys[i], obj);
}
}
};Return a new and transformed array by executing the iterator
on every item in the obj.
obj is either an array or an object.iterator is a function.scope is the value of this. Chunks.map = function(obj, iterator, scope) {
if (nativeMap && obj.nativeMap) { return obj.map(iterator, scope); }
var results = [];
Chunks.forEach(obj, function(val, i, obj) {
results.push(iterator.call(scope, val, i, obj));
});
return results;
};Return a new array by executing the iterator on every item in
the obj. Only the items that evalute to true in the iterator
will be added to the array.
obj is either an array or an object.iterator is a function.scope is the value of this. Chunks.filter = function(obj, iterator, scope) {
if (nativeFilter && obj.nativeFilter) { return obj.filter(iterator, scope); }
var results = [];
Chunks.forEach(obj, function(val, i, obj) {
if (iterator.call(scope, val, i, obj)) {
results.push(val);
}
});
return results;
};Return a single value by executing the iterator on every item
in the obj and passing the previous value, memo, to the iterator.
iterator is a function.memo is the initial value.scope is the value of this. Chunks.reduce = function(obj, iterator, memo, scope) {
if (nativeReduce && obj.nativeReduce) { return obj.reduce(iterator, memo); }
Chunks.forEach(obj, function(val, i, obj) {
memo = iterator.call(scope, memo, val, i, obj);
});
return memo;
};Return either true or false by executing the iterator on
every item in the obj. It'll return true if one of the items
in the obj matches the iterator.
obj is either an array or an object.iterator is a function.scope is the value of this. Chunks.some = function(obj, iterator, scope) {
if (nativeSome && obj.nativeSome) { return obj.some(iterator, scope); }
var results = false;
Chunks.forEach(obj, function(val, i, obj) {
if (iterator.call(scope, val, i, obj)) {
results = true;
}
});
return results;
};Return either true or false by executing the iterator on
every item in the obj. It'll return true if all of the items
in the obj matches the iterator.
obj is either an array or an object.iterator is a function.scope is the value of this. Chunks.every = function(obj, iterator, scope) {
if (nativeEvery && obj.nativeEvery) { return obj.every(iterator, scope); }
var results = true;
Chunks.forEach(obj, function(val, i, obj) {
if (!iterator.call(scope, val, i, obj)) {
results = false;
}
});
return results;
};Return the index of the item in the obj. It delegates to
the native indexOf implementation whenever it can.
obj is either an array or an object.item is a value. Chunks.index = function(obj, item) {
if (nativeIndexOf && obj.nativeIndexOf) { return obj.indexOf(item); }
var results = -1;
Chunks.forEach(obj, function(val, i) {
if (val === item) { results = i; }
});
return results;
};Determine if the obj contains the item.
obj is either an array or an object.item is a value. Chunks.contains = function(obj, item) {
var results = Chunks.some(obj, function(val) {
return item === val;
});
return results;
}; Chunks.size = function(obj) {
var results = null;
if (Chunks.isArray(obj)) {
results = obj.length;
} else {
results = Chunks.keys(obj).length;
}
return results;
}; Chunks.clone = function(obj) {
var results = null;
if (Chunks.isArray(obj)) {
results = obj.slice();
} else {
results = Chunks.extend({}, obj);
}
return results;
};Return a new array with the values from the obj sorted by the comp.
obj is either an array or an object.comp is a function. Chunks.sort = function(obj, comp) {
var values = [],
results = null;
Chunks.forEach(obj, function(val) {
values.push(val);
});
if (comp) {
results = values.sort(comp);
} else {
results = values.sort();
}
return results;
}; Chunks.isArray = (nativeIsArray || function(obj) {
return (toString.call(obj) === '[object Array]');
}); Chunks.isObject = function(obj) {
return (toString.call(obj) === '[object Object]');
}; Chunks.first = function(arr) {
return arr[0];
}; Chunks.last = function(arr) {
return arr[arr.length - 1];
};Return a new and transformed array by flattening the nested arrays
in the arr. The new array will use the base as a starting point
if it's provided.
arr is an array.base is an array. Chunks.flatten = function(arr, base) {
base = (base || []);
var results = Chunks.reduce(arr, function(memo, val) {
return memo.concat(val);
}, base);
return results;
}; Chunks.keys = (nativeKeys || function(obj) {
var results = [];
for (var key in obj) {
if (Chunks.has(obj, key)) {
results.push(key);
}
}
return results;
}); Chunks.values = function(obj) {
var results = [];
Chunks.forEach(obj, function(val) {
results.push(val);
});
return results;
}; Chunks.has = function(obj, key) {
return (hasOwnProp.call(obj, key));
};Merge the properties in the obj together with the properties
in the src.
obj is an object.src is an object. Chunks.extend = function(obj, src) {
for (var key in src) {
if (Chunks.has(src, key)) {
if (src[key] !== undefined) {
obj[key] = src[key];
}
}
}
return obj;
};Export Chunks as a CommonJS module or expose it to
the global scope (window) for browser usage.
if (typeof module !== 'undefined' && module.exports) {
module.exports = Chunks;
} else {
root.Chunks = Chunks;
}
})(this);