Skip to content

Commit

Permalink
Added missing array extension methods to the client version of crafit…
Browse files Browse the repository at this point in the history
…y.arrays
  • Loading branch information
gasl committed Feb 20, 2014
1 parent 4d4884b commit 21fd8ca
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions crafity.arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,87 @@
return !arrays.contains.apply(this, arrays.toArray(arguments));
};

/**
* Return an array, containing the common members of both input arrays.
* Example arrays.intersect([1,2,3], [3,4,5]) returns [3]
* @returns {Array} an array
*/

arrays.intersect = function (first, second) {
if (arguments.length === 0) {
return [].slice();
}
var a1 = first
, a2 = null
, a = []
, n = 1
, l, l2
, i, j;

while (n < arguments.length) {
a2 = arguments[n];
l = a1.length;
l2 = a2.length;
for (i = 0; i < l; i++) {
for (j = 0; j < l2; j++) {
if (a1[i] === a2[j] && a.indexOf(a1[i]) === -1) {
a.push(a1[i]);
}
}
}
n++;
}
return a;
};

/**
* Group values in an array.
* @param {Array} array The array to group
* @param {Function} fn (optional) A grouping function
* @return {Object} A object with grouped key/values
*
* @example A Simple group by example: groupBy([1, 2, 3, 2, 4, 4, 3, 5]).
* Result: { '1': [ 1 ],'2': [ 2, 2 ],'3': [ 3, 3 ],'4': [ 4, 4 ],'5': [ 5 ] }
*
* @example A simple group by and return length example: groupBy([1, 2, 3, 2, 4, 4, 3, 5],
* function (array) {
* return array.length;
* }));
* Result: { '1': 1, '2': 2, '3': 2, '4': 2, '5': 1 }
*
* @example A simple group by and sum example: groupBy([1, 2, 3, 2, 4, 4, 3, 5],
* function (array) {
* return array.reduce(function (seed, value) { return seed + value }, 0);
* }));
* Result: { '1': 1, '2': 4, '3': 6, '4': 8, '5': 5 }
*
*/
arrays.groupBy = function (array, fn) {
var result = {};
array.forEach(function (item) {
result[item] = result[item] || [];
result[item].push(item);
});
if (fn) {
Object.keys(result).forEach(function (key) {
result[key] = fn(result[key]);
});
}
return result;
};

/**
* Get all the unique values in an Array
* @param {Array} array The array to get the unique values from;
* @return {Array} A new array containing all the unique values
*/
arrays.distinct = function (array) {
var result = [];
array.forEach(function (value) {
if (arrays.contains.not(result, value)) {
result.push(value);
}
});
return result;
};
}(window.crafity = window.crafity || {}));

0 comments on commit 21fd8ca

Please sign in to comment.