Defined base/array.js

Utilities for working with arrays.

The comb.array namespace can be used to decorate arrays with additional chainable functionality.

  1. var arr = comb.array([1,3,2,5,4,6]);
  2. console.log(arr.sum()) //21
  3. console.log(arr.sort()) //[1,2,3,4,5,6]
  4. console.log(arr.min()) //[1]
  5. console.log(arr.max()) [6]

avg Static Function Public


Defined base/array.js

Averages an array of numbers.

Example
  1. comb.array.avg([1,2,3]); //2
Arguments Returns Throws Source
  1. function (arr){
  2. arr = arr || [];
  3. if (arr.length) {
  4. var total = sum(arr);
  5. if (number.isNumber(total)) {
  6. return total / arr.length;
  7. } else {
  8. throw new Error("Cannot average an array of non numbers.");
  9. }
  10. } else {
  11. return 0;
  12. }
  13. }

cartesian Static Function Public


Defined base/array.js

Find the cartesian product of two arrays

Example
  1. comb.array.cartesian([1,2], [2,3]) => [
  2. [1,2],
  3. [1,3],
  4. [2,2],
  5. [2,3]
  6. ]
  7. comb.array.cartesian([1,2], [2,3,4]) => [
  8. [1,2],
  9. [1,3],
  10. [1,4] ,
  11. [2,2],
  12. [2,3],
  13. [2,4]
  14. ]
  15. comb.array.cartesian([1,2,3], [2,3,4]) => [
  16. [1,2],
  17. [1,3],
  18. [1,4] ,
  19. [2,2],
  20. [2,3],
  21. [2,4] ,
  22. [3,2],
  23. [3,3],
  24. [3,4]
  25. ]
Arguments Source
  1. function (a,b){
  2. var ret = [];
  3. if (isArray(a) && isArray(b) && a.length && b.length) {
  4. ret = cross(a[0], b).concat(cartesian(a.slice(1), b));
  5. }
  6. return ret;
  7. }

compact Static Function Public


Defined base/array.js

Compacts an array removing null or undefined objects from the array.

Example
  1. var x;
  2. comb.array.compact([1,null,null,x,2]) => [1,2]
  3. comb.array.compact([1,2]) => [1,2]
Arguments Source
  1. function (arr){
  2. var ret = [];
  3. if (isArray(arr) && arr.length) {
  4. ret = filter(arr, function (item) {
  5. return !misc.isUndefinedOrNull(item);
  6. });
  7. }
  8. return ret;
  9. }

difference Static Function Public


Defined base/array.js

Finds the difference of the two arrays.

Example
  1. comb.array.difference([1,2,3], [2,3]); //[1]
  2. comb.array.difference(["a","b",3], [3]); //["a","b"]
Arguments Returns Source
  1. function (arr1,arr2){
  2. var ret = arr1, args = flatten(argsToArray(arguments, 1));
  3. if (isArray(arr1)) {
  4. ret = filter(arr1, function (a) {
  5. return indexOf(args, a) === -1;
  6. });
  7. }
  8. return ret;
  9. }

flatten Static Function Public


Defined base/array.js

Flatten multiple arrays into a single array

Example
  1. comb.array.flatten([1,2], [2,3], [3,4]) => [1,2,2,3,3,4]
  2. comb.array.flatten([1,"A"], [2,"B"], [3,"C"]) => [1,"A",2,"B",3,"C"]
Arguments Source
  1. function (arr){
  2. var set;
  3. var args = argsToArray(arguments);
  4. if (args.length > 1) {
  5. //assume we are intersections all the lists in the array
  6. set = args;
  7. } else {
  8. set = toArray(arr);
  9. }
  10. return reduce(set, function (a, b) {
  11. return a.concat(b);
  12. }, []);
  13. }

intersect Static Function Public


Defined base/array.js

Finds the intersection of arrays NOTE : this function accepts an arbitrary number of arrays

Example
  1. comb.array.intersect([1,2], [2,3], [2,3,5]) => [2]
  2. comb.array.intersect([1,2,3], [2,3,4,5], [2,3,5]) => [2,3]
  3. comb.array.intersect([1,2,3,4], [2,3,4,5], [2,3,4,5]) => [2,3,4]
  4. comb.array.intersect([1,2,3,4,5], [1,2,3,4,5], [1,2,3]) => [1,2,3]
  5. comb.array.intersect([[1,2,3,4,5],[1,2,3,4,5],[1,2,3]]) => [1,2,3]
Arguments Source
  1. function (a,b){
  2. var collect = [], set;
  3. var args = argsToArray(arguments);
  4. if (args.length > 1) {
  5. //assume we are intersections all the lists in the array
  6. set = args;
  7. } else {
  8. set = args[0];
  9. }
  10. if (isArray(set)) {
  11. var x = set.shift();
  12. collect = reduce(set, function (a, b) {
  13. return intersection(a, b);
  14. }, x);
  15. }
  16. return removeDuplicates(collect);
  17. }

invoke Static Function Public


Defined base/array.js

Invokes the method specified in the scope of each item in the array. If you supply addional arguments they will be applied to the function.

  1. function person(name, age){
  2. return {
  3. getName : function(){
  4. return name;
  5. },
  6. setName : function(newName){
  7. name = newName;
  8. },
  9. getOlder : function(){
  10. age++;
  11. return this;
  12. },
  13. getAge : function(){
  14. return age;
  15. }
  16. };
  17. }
  18. var arr = [person("Bob", 40), person("Alice", 35), person("Fred", 50), person("Johnny", 56)];
  19. console.log(comb.array.invoke(arr, "getName")); //["Bob", "Alice", "Fred", "Johnny"]
  20. console.log(comb.array(arr).invoke("getOlder").invoke("getAge")); //[41, 36, 51, 57];

Arguments Returns Source
  1. function (arr,func,args){
  2. args = argsToArray(arguments, 2);
  3. return map(arr, function (item) {
  4. var exec = isString(func) ? item[func] : func;
  5. return exec.apply(item, args);
  6. });
  7. }

max Static Function Public


Defined base/array.js

Finds that max value of an array. If a second argument is provided and it is a function it will be used as a comparator function. If the second argument is a string then it will be used as a property look up on each item.

Example
  1. comb.array.max([{a : 1}, {a : 2}, {a : -2}], "a"); //{a : 2}
  2. comb.array.max([{a : 1}, {a : 2}, {a : -2}], function(a,b){
  3. return a.a - b.a
  4. }); //{a : 2}
Arguments Returns Source
  1. function (arr,cmp){
  2. return _sort(arr, cmp)[arr.length - 1];
  3. }

min Static Function Public


Defined base/array.js

Finds that min value of an array. If a second argument is provided and it is a function it will be used as a comparator function. If the second argument is a string then it will be used as a property look up on each item.

Example
  1. comb.array.min([{a : 1}, {a : 2}, {a : -2}], "a"); //{a : -2}
  2. comb.array.min([{a : 1}, {a : 2}, {a : -2}], function(a,b){
  3. return a.a - b.a
  4. }); //{a : -2}
Arguments Returns Source
  1. function (arr,cmp){
  2. return _sort(arr, cmp)[0];
  3. }

multiply Static Function Public


Defined base/array.js

Creates a new array that is the result of the array concated the number of times. If times is not specified or it equals 0 then it defaults to 1.

Arguments Returns Source
  1. function (arr,times){
  2. times = number.isNumber(times) ? times : 1;
  3. if (!times) {
  4. //make sure times is greater than zero if it is zero then dont multiply it
  5. times = 1;
  6. }
  7. arr = toArray(arr || []);
  8. var ret = [], i = 0;
  9. while (++i <= times) {
  10. ret = ret.concat(arr);
  11. }
  12. return ret;
  13. }

partition Static Function Public


Defined base/array.js

Paritition an array.

  1. var arr = [1,2,3,4,5];
  2. comb.array.partition(arr, 1); //[[1], [2], [3], [4], [5]]
  3. comb.array.partition(arr, 2); //[[1,2], [3,4], [5]]
  4. comb.array.partition(arr, 3); //[[1,2,3], [4,5]]
  5. comb.array.partition(arr); //[[1, 2, 3, 4, 5]]
  6. comb.array.partition(arr, 0); //[[1, 2, 3, 4, 5]]

Arguments Returns Source
  1. function (array,partitionSize){
  2. partitionSize = partitionSize || array.length;
  3. var ret = [];
  4. for (var i = 0, l = array.length; i < l; i += partitionSize) {
  5. ret.push(array.slice(i, i + partitionSize));
  6. }
  7. return ret;
  8. }

permutations Static Function Public


Defined base/array.js

Finds all permutations of an array

Example
  1. var arr = [1,2,3];
  2. comb.array.permutations(arr) => [[ 1, 2, 3 ],[ 1, 3, 2 ],[ 2, 3, 1 ],
  3. [ 2, 1, 3 ],[ 3, 1, 2 ],[ 3, 2, 1 ]]
  4. comb.array.permutations(arr, 2) => [[ 1, 2],[ 1, 3],[ 2, 3],[ 2, 1],[ 3, 1],[ 3, 2]]
  5. comb.array.permutations(arr, 1) => [[1],[2],[3]]
  6. comb.array.permutations(arr, 0) => [[]]
  7. comb.array.permutations(arr, 4) => []
Arguments Source
  1. function (arr,length){
  2. var ret = [];
  3. if (isArray(arr)) {
  4. var copy = arr.slice(0);
  5. if (typeof length !== "number") {
  6. length = arr.length;
  7. }
  8. if (!length) {
  9. ret = [
  10. []
  11. ];
  12. } else if (length <= arr.length) {
  13. ret = reduce(arr, function (a, b, i) {
  14. var ret;
  15. if (length > 1) {
  16. ret = permute(b, rotate(copy, i).slice(1), length);
  17. } else {
  18. ret = [
  19. [b]
  20. ];
  21. }
  22. return a.concat(ret);
  23. }, []);
  24. }
  25. }
  26. return ret;
  27. }

pluck Static Function Public


Defined base/array.js

Plucks values from an array. Effectevily the same as using a array.map implementation. However the porperty specified supports a "dot" notation to access nested properties.

Example
  1. var arr = [
  2. {name:{first:"Fred", last:"Jones"}, age:50, roles:["a", "b", "c"]},
  3. {name:{first:"Bob", last:"Yukon"}, age:40, roles:["b", "c"]},
  4. {name:{first:"Alice", last:"Palace"}, age:35, roles:["c"]},
  5. {name:{first:"Johnny", last:"P."}, age:56, roles:[]}
  6. ];
  7. console.log(comb.array.pluck(arr, "name.first")); //["Fred", "Bob", "Alice", "Johnny"]
  8. console.log(comb.array.pluck(arr, "age")); //[50, 40, 35, 56]
  9. console.log(comb.array.pluck(arr, "roles.length")); //[3, 2, 1, 0]
  10. console.log(comb.array.pluck(arr, "roles.0")); //["a", "b", "c", undefined]
Arguments Returns Source
  1. function (arr,prop){
  2. prop = prop.split(".");
  3. var result = arr.slice(0);
  4. forEach(prop, function (prop) {
  5. var exec = prop.match(/(\w+)\(\)$/);
  6. result = map(result, function (item) {
  7. return exec ? item[exec[1]]() : item[prop];
  8. });
  9. });
  10. return result;
  11. }

powerSet Static Function Public


Defined base/array.js

Finds the powerset of an array

Example
  1. comb.array.powerSet([1,2]) => [
  2. [],
  3. [ 1 ],
  4. [ 2 ],
  5. [ 1, 2 ]
  6. ]
  7. comb.array.powerSet([1,2,3]) => [
  8. [],
  9. [ 1 ],
  10. [ 2 ],
  11. [ 1, 2 ],
  12. [ 3 ],
  13. [ 1, 3 ],
  14. [ 2, 3 ],
  15. [ 1, 2, 3 ]
  16. ]
  17. comb.array.powerSet([1,2,3,4]) => [
  18. [],
  19. [ 1 ],
  20. [ 2 ],
  21. [ 1, 2 ],
  22. [ 3 ],
  23. [ 1, 3 ],
  24. [ 2, 3 ],
  25. [ 1, 2, 3 ],
  26. [ 4 ],
  27. [ 1, 4 ],
  28. [ 2, 4 ],
  29. [ 1, 2, 4 ],
  30. [ 3, 4 ],
  31. [ 1, 3, 4 ],
  32. [ 2, 3, 4 ],
  33. [ 1, 2, 3, 4 ]
  34. ]
Arguments Source
  1. function (arr){
  2. var ret = [];
  3. if (isArray(arr) && arr.length) {
  4. ret = reduce(arr, function (a, b) {
  5. var ret = map(a, function (c) {
  6. return c.concat(b);
  7. });
  8. return a.concat(ret);
  9. }, [
  10. []
  11. ]);
  12. }
  13. return ret;
  14. }

removeDuplicates Static Function Public


Defined base/array.js

Removes duplicates from an array

Example
  1. comb.array.removeDuplicates([1,1,1]) => [1]
  2. comb.array.removeDuplicates([1,2,3,2]) => [1,2,3]
Arguments Source
  1. function (arr){
  2. if (isArray(arr)) {
  3. return reduce(arr, function (a, b) {
  4. if (indexOf(a, b) === -1) {
  5. return a.concat(b);
  6. } else {
  7. return a;
  8. }
  9. }, []);
  10. }
  11. }

rotate Static Function Public


Defined base/array.js

Rotates an array the number of specified positions

Example
  1. var arr = ["a", "b", "c", "d"];
  2. comb.array.rotate(arr) => ["b", "c", "d", "a"]
  3. comb.array.rotate(arr, 2) => ["c", "d", "a", "b"]);
  4. comb.array.rotate(arr, 3) => ["d", "a", "b", "c"]);
  5. comb.array.rotate(arr, 4) => ["a", "b", "c", "d"]);
  6. comb.array.rotate(arr, -1) => ["d", "a", "b", "c"]);
  7. comb.array.rotate(arr, -2) => ["c", "d", "a", "b"]);
  8. comb.array.rotate(arr, -3) => ["b", "c", "d", "a"]);
  9. comb.array.rotate(arr, -4) => ["a", "b", "c", "d"]);
Arguments Source
  1. function (arr,numberOfTimes){
  2. var ret = arr.slice();
  3. if (typeof numberOfTimes !== "number") {
  4. numberOfTimes = 1;
  5. }
  6. if (numberOfTimes && isArray(arr)) {
  7. if (numberOfTimes > 0) {
  8. ret.push(ret.shift());
  9. numberOfTimes--;
  10. } else {
  11. ret.unshift(ret.pop());
  12. numberOfTimes++;
  13. }
  14. return rotate(ret, numberOfTimes);
  15. } else {
  16. return ret;
  17. }
  18. }

sort Static Function Public


Defined base/array.js

Allows the sorting of an array based on a property name instead. This can also act as a sort that does not change the original array.

NOTE: this does not change the original array!

Example
  1. comb.array.sort([{a : 1}, {a : 2}, {a : -2}], "a"); //[{a : -2}, {a : 1}, {a : 2}];
Arguments Returns Source
  1. function (arr,cmp){
  2. return _sort(arr, cmp);
  3. }

sum Static Function Public


Defined base/array.js

Sums all items in an array

Example
  1. comb.array.sum([1,2,3]) => 6
  2. comb.array.sum(["A","B","C"]) => "ABC";
  3. var d1 = new Date(1999), d2 = new Date(2000), d3 = new Date(3000);
  4. comb.array.sum([d1,d2,d3]) => "Wed Dec 31 1969 18:00:01 GMT-0600 (CST)"
  5. + "Wed Dec 31 1969" 18:00:02 GMT-0600 (CST)"
  6. + "Wed Dec 31 1969 18:00:03 GMT-0600 (CST)"
  7. comb.array.sum([{},{},{}]) => "[object Object][object Object][object Object]";
Arguments Source
  1. function (array){
  2. array = array || [];
  3. if (array.length) {
  4. return reduce(array, function (a, b) {
  5. return a + b;
  6. });
  7. } else {
  8. return 0;
  9. }
  10. }

toArray Static Function Public


Defined base/array.js

converts anything to an array

Example
  1. comb.array.toArray({a : "b", b : "c"}) => [["a","b"], ["b","c"]];
  2. comb.array.toArray("a") => ["a"]
  3. comb.array.toArray(["a"]) => ["a"];
  4. comb.array.toArray() => [];
  5. comb.array.toArray("a", {a : "b"}) => ["a", ["a", "b"]];
Arguments Source
  1. function (o){
  2. var ret = [];
  3. if (o != null) {
  4. var args = argsToArray(arguments);
  5. if (args.length === 1) {
  6. if (isArray(o)) {
  7. ret = o;
  8. } else if (obj.isHash(o)) {
  9. for (var i in o) {
  10. if (o.hasOwnProperty(i)) {
  11. ret.push([i, o[i]]);
  12. }
  13. }
  14. } else {
  15. ret.push(o);
  16. }
  17. } else {
  18. forEach(args, function (a) {
  19. ret = ret.concat(toArray(a));
  20. });
  21. }
  22. }
  23. return ret;
  24. }

transpose Static Function Public


Defined base/array.js

Transposes an array of arrays

Example
  1. comb.array.transpose([[1,2,3], [4,5,6]]) => [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
  2. comb.array.transpose([[1,2], [3,4], [5,6]]) => [ [ 1, 3, 5 ], [ 2, 4, 6 ] ]
  3. comb.array.transpose([[1], [3,4], [5,6]]) => [[1]])
Arguments Source
  1. function (arr){
  2. var ret = [];
  3. if (isArray(arr) && arr.length) {
  4. var last;
  5. forEach(arr, function (a) {
  6. if (isArray(a) && (!last || a.length === last.length)) {
  7. forEach(a, function (b, i) {
  8. if (!ret[i]) {
  9. ret[i] = [];
  10. }
  11. ret[i].push(b);
  12. });
  13. last = a;
  14. }
  15. });
  16. }
  17. return ret;
  18. }

union Static Function Public


Defined base/array.js

Union a variable number of arrays together

Example
  1. comb.array.union(['a','b','c'], ['b','c', 'd']) => ["a", "b", "c", "d"]
  2. comb.array.union(["a"], ["b"], ["c"], ["d"], ["c"]) => ["a", "b", "c", "d"]
Arguments Source
  1. function (){
  2. var ret = [];
  3. var arrs = argsToArray(arguments);
  4. if (arrs.length > 1) {
  5. ret = removeDuplicates(reduce(arrs, function (a, b) {
  6. return a.concat(b);
  7. }, []));
  8. }
  9. return ret;
  10. }

unique Static Function Public


Defined base/array.js

See comb.array.removeDuplicates

Arguments Source
  1. function (arr){
  2. return removeDuplicates(arr);
  3. }

valuesAt Static Function Public


Defined base/array.js

Retrieves values at specified indexes in the array

Example
  1. var arr =["a", "b", "c", "d"]
  2. comb.array.valuesAt(arr, 1,2,3) => ["b", "c", "d"];
  3. comb.array.valuesAt(arr, 1,2,3, 4) => ["b", "c", "d", null];
  4. comb.array.valuesAt(arr, 0,3) => ["a", "d"];
Arguments Source
  1. function (arr,indexes){
  2. var ret = [];
  3. indexes = argsToArray(arguments);
  4. arr = indexes.shift();
  5. var l = arr.length;
  6. if (isArray(arr) && indexes.length) {
  7. for (var i = 0; i < indexes.length; i++) {
  8. ret.push(arr[indexes[i]] || null);
  9. }
  10. }
  11. return ret;
  12. }

zip Static Function Public


Defined base/array.js

Zips to arrays together

Example
  1. var a = [ 4, 5, 6 ], b = [ 7, 8, 9 ]
  2. comb.array.zip([1], [2], [3]) => [[ 1, 2, 3 ]]);
  3. comb.array.zip([1,2], [2], [3]) => [[ 1, 2, 3 ],[2, null, null]]
  4. comb.array.zip([1,2,3], a, b) => [[1, 4, 7],[2, 5, 8],[3, 6, 9]]
  5. comb.array.zip([1,2], a, b) => [[1, 4, 7],[2, 5, 8]]
  6. comb.array.zip(a, [1,2], [8]) => [[4,1,8],[5,2,null],[6,null,null]]
Arguments Source
  1. function (){
  2. var ret = [];
  3. var arrs = argsToArray(arguments);
  4. if (arrs.length > 1) {
  5. var arr1 = arrs.shift();
  6. if (isArray(arr1)) {
  7. ret = reduce(arr1, function (a, b, i) {
  8. var curr = [b];
  9. for (var j = 0; j < arrs.length; j++) {
  10. var currArr = arrs[j];
  11. if (isArray(currArr) && !misc.isUndefined(currArr[i])) {
  12. curr.push(currArr[i]);
  13. } else {
  14. curr.push(null);
  15. }
  16. }
  17. a.push(curr);
  18. return a;
  19. }, []);
  20. }
  21. }
  22. return ret;
  23. }

License

MIT https://github.com/C2FO/comb/raw/master/LICENSE

Meta