Common Utilities

comb includes a lot of function that are used commonly when developing application. Here we'll go over a few of the most commonly used ones.

is* functions

comb includes a number of is function to test if something is of the type.

String utilities

format

Formats a string with the specified format.

  1. // "Hello, World";
  2. comb.string.format("%s, %s", ["Hello", "World"]);
  3. //" Hello, World "
  4. comb.string.format("%[ 10]s, %[- 10]s", ["Hello", "World"])
  5. //"apple!!!!!, ####orange, bananas and watermelon"
  6. comb.string.format("%-!10s, %#10s, %10s and %-10s", "apple", "orange", "bananas", "watermelons")
  7. //"+1, -2, 0000000001, 2000000000, +3########, 1000000000"
  8. comb.string.format("%+d, %+d, %10d, %-10d, %-+#10d, %10d", 1,-2, 1, 2, 3, 100000000000)
  9. //formats in local time
  10. //7:32 PM
  11. comb.string.format("%[h:mm a]D", [date])
  12. //format in UTC
  13. //12:32 PM
  14. comb.string.format("%[h:mm a]Z", [date])
  15. //When using object formats they must be in an array otherwise
  16. //format will try to interpolate the properties into the string.
  17. //'{"a":"b"}'
  18. comb.string.format("%j", [{a : "b"}])
  19. //'{\n "a": "b"\n},\n{\n "a": "b"\n}'
  20. comb.string.format("%1j, %4j", [{a : "b"}, {a : "b"}])
  21. //"Hello, World"
  22. comb.string.format("{hello}, {world}", {hello : "Hello", world : "World")
  23. //applesssss, ####orange, bananas and watermelon
  24. comb.string.format("{[-s10]apple}, {[%#10]orange}, {[10]banana} and {[-10]watermelons}", {
  25. apple:"apple",
  26. orange:"orange",
  27. banana:"bananas",
  28. watermelons:"watermelons"
  29. });

style

Styles a string for terminal output.

  1. //style a string red
  2. comb.string.style('myStr', 'red');
  3. //style a string red and bold
  4. comb.string.style('myStr', ['red', bold]);

The style options include

Others

Array utilities

avg

Averages an array of numbers.

  1. comb.array.avg([1,2,3]); //2

compact

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

  1. var x;
  2. comb.array.compact([1,null,null,x,2]) => [1,2]
  3. comb.array.compact([1,2]) => [1,2]

difference

Finds the difference of the two arrays.

  1. comb.array.difference([1,2,3], [2,3]); //[1]
  2. comb.array.difference(["a","b",3], [3]); //["a","b"]

flatten

Flatten multiple arrays into a single array

  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"]

intersect

Finds the intersection of arrays.

  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]

max

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.

  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}

min

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.

  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}

sort

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!

  1. comb.array.sort([{a : 1}, {a : 2}, {a : -2}], "a"); //[{a : -2}, {a : 1}, {a : 2}];

removeDuplicates

Removes duplicates from an array.

  1. comb.array.removeDuplicates([1,1,1]) => [1]
  2. comb.array.removeDuplicates([1,2,3,2]) => [1,2,3]

sum

Finds the sum of an array

  1. comb.array.sum([1,2,3]) => 6

toArray

Converts anything to an array. Useful if you want to covert a hash into an array.

  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"]];

union

Finds the union of two arrays

  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"]

zip

Zips multiple arrays into a single array.

  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]]

Number utilities

round

Rounds a number to the specified places.

  1. comb.number.round(10.000009, 2); //10
  2. comb.number.round(10.000009, 5); //10.00001
  3. comb.number.round(10.0009, 3); //10.001
  4. comb.number.round(10.0009, 2); //10
  5. comb.number.round(10.0009, 3); //10.001

roundCeil

Rounds a number to the specified places, rounding up.

  1. comb.number.roundCeil(10.000001, 2); //10.01
  2. comb.number.roundCeil(10.000002, 5); //10.00001
  3. comb.number.roundCeil(10.0003, 3); //10.001
  4. comb.number.roundCeil(10.0004, 2); //10.01
  5. comb.number.roundCeil(10.0005, 3); //10.001
  6. comb.number.roundCeil(10.0002, 2); //10.01

Date utilities

add

Adds a specified interval and amount to a date

  1. var dtA = new Date(2005, 11, 27);
  2. comb.date.add(dtA, "year", 1); //new Date(2006, 11, 27);
  3. comb.date.add(dtA, "years", 1) //new Date(2006, 11, 27);
  4. dtA = new Date(2000, 0, 1);
  5. comb.date.add(dtA, "quarter", 1); //new Date(2000, 3, 1);
  6. comb.date.add(dtA, "quarters", 1); //new Date(2000, 3, 1);
  7. dtA = new Date(2000, 0, 1);
  8. comb.date.add(dtA, "month", 1); //new Date(2000, 1, 1);
  9. comb.date.add(dtA, "months", 1); //new Date(2000, 1, 1);
  10. dtA = new Date(2000, 0, 31);
  11. comb.date.add(dtA, "month", 1); //new Date(2000, 1, 29);
  12. comb.date.add(dtA, "months", 1); //new Date(2000, 1, 29);
  13. dtA = new Date(2000, 0, 1);
  14. comb.date.add(dtA, "week", 1); //new Date(2000, 0, 8);
  15. comb.date.add(dtA, "weeks", 1); //new Date(2000, 0, 8);
  16. dtA = new Date(2000, 0, 1);
  17. comb.date.add(dtA, "day", 1); //new Date(2000, 0, 2);
  18. dtA = new Date(2000, 0, 1);
  19. comb.date.add(dtA, "weekday", 1); //new Date(2000, 0, 3);
  20. dtA = new Date(2000, 0, 1, 11);
  21. comb.date.add(dtA, "hour", 1); //new Date(2000, 0, 1, 12);
  22. dtA = new Date(2000, 11, 31, 23, 59);
  23. comb.date.add(dtA, "minute", 1); //new Date(2001, 0, 1, 0, 0);
  24. dtA = new Date(2000, 11, 31, 23, 59, 59);
  25. comb.date.add(dtA, "second", 1); //new Date(2001, 0, 1, 0, 0, 0);
  26. dtA = new Date(2000, 11, 31, 23, 59, 59, 999);
  27. comb.date.add(dtA, "millisecond", 1); //new Date(2001, 0, 1, 0, 0, 0, 0);

compare

Compares two dates

  1. var d1 = new Date();
  2. d1.setHours(0);
  3. comb.date.compare(d1, d1); //0
  4. var d1 = new Date();
  5. d1.setHours(0);
  6. var d2 = new Date();
  7. d2.setFullYear(2005);
  8. d2.setHours(12);
  9. comb.date.compare(d1, d2, "date"); //1
  10. comb.date.compare(d1, d2, "datetime"); //1
  11. var d1 = new Date();
  12. d1.setHours(0);
  13. var d2 = new Date();
  14. d2.setFullYear(2005);
  15. d2.setHours(12);
  16. comb.date.compare(d2, d1, "date"); //-1
  17. comb.date.compare(d1, d2, "time"); //-1

difference

Finds the difference between two dates based on the specified interval

  1. var dtA, dtB;
  2. dtA = new Date(2005, 11, 27);
  3. dtB = new Date(2006, 11, 27);
  4. comb.date.difference(dtA, dtB, "year") => 1
  5. dtA = new Date(2000, 1, 29);
  6. dtB = new Date(2001, 2, 1);
  7. comb.date.difference(dtA, dtB, "quarter") => 4
  8. comb.date.difference(dtA, dtB, "month") => 13
  9. dtA = new Date(2000, 1, 1);
  10. dtB = new Date(2000, 1, 8);
  11. comb.date.difference(dtA, dtB, "week") => 1
  12. dtA = new Date(2000, 1, 29);
  13. dtB = new Date(2000, 2, 1);
  14. comb.date.difference(dtA, dtB, "day") => 1
  15. dtA = new Date(2006, 7, 3);
  16. dtB = new Date(2006, 7, 11);
  17. comb.date.difference(dtA, dtB, "weekday") => 6
  18. dtA = new Date(2000, 11, 31, 23);
  19. dtB = new Date(2001, 0, 1, 0);
  20. comb.date.difference(dtA, dtB, "hour") => 1
  21. dtA = new Date(2000, 11, 31, 23, 59);
  22. dtB = new Date(2001, 0, 1, 0, 0);
  23. comb.date.difference(dtA, dtB, "minute") => 1
  24. dtA = new Date(2000, 11, 31, 23, 59, 59);
  25. dtB = new Date(2001, 0, 1, 0, 0, 0);
  26. comb.date.difference(dtA, dtB, "second") => 1
  27. dtA = new Date(2000, 11, 31, 23, 59, 59, 999);
  28. dtB = new Date(2001, 0, 1, 0, 0, 0, 0);
  29. comb.date.difference(dtA, dtB, "millisecond") => 1

format

Formats a date to the specidifed format string

  1. var date = new Date(2006, 7, 11, 0, 55, 12, 345);
  2. //"Friday, August 11, 2006"
  3. comb.date.format(date, "EEEE, MMMM dd, yyyy");
  4. //"8/11/06"
  5. comb.date.format(date, "M/dd/yy");
  6. //"6"
  7. comb.date.format(date, "E");
  8. //"12:55 AM"
  9. comb.date.format(date, "h:m a");
  10. //"12:55:12"
  11. comb.date.format(date, 'h:m:s');
  12. //"12:55:12.35"
  13. comb.date.format(date, 'h:m:s.SS');
  14. //"24:55:12.35"
  15. comb.date.format(date, 'k:m:s.SS');
  16. //"0:55:12.35"
  17. comb.date.format(date, 'H:m:s.SS');
  18. //"11082006"
  19. comb.date.format(date, "ddMMyyyy");

Format specifiers include

parse

Parses a date string into a date object

  1. var aug_11_2006 = new Date(2006, 7, 11, 0);
  2. //all of the following will parse to aug_11_2006
  3. comb.date.parse("08/11/06", "MM/dd/yy");
  4. comb.date.parse("11Aug2006", 'ddMMMyyyy');
  5. comb.date.parse("Aug 11, 2006", "MMM dd, yyyy"); //aug_11_2006
  6. comb.date.parse("August 11, 2006", "MMMM dd, yyyy"); //aug_11_2006
  7. comb.date.parse("Friday, August 11, 2006", "EEEE, MMMM dd, yyyy"); //aug_11_2006

Format specifiers include

ago and FromNow

comb also has the following methods as a convenience to adding time to Date.now()

Object utilities

merge

Merges objects together. This method will only change the frist object passed in.

  1. var myObj = {};
  2. comb.merge(myObj, {test : true});
  3. myObj.test; //true
  4. comb.merge(myObj, {test : false}, {test2 : false}, {test3 : "hello", test4 : "world"});
  5. myObj.test; //false
  6. myObj.test2; //false
  7. myObj.test3; //"hello"
  8. myObj.test4; //"world"

deepMerge

Merges objects together only overriding properties that are different.

  1. var myObj = {my : {cool : {property1 : 1, property2 : 2}}};
  2. comb.deepMerge(myObj, {my : {cool : {property3 : 3}}});
  3. myObj.my.cool.property1; \\1
  4. myObj.my.cool.property2; \\2
  5. myObj.my.cool.property3; \\3

deepEqual

Determines if an two things are deep equal. This is a reimplementation of assert.deepEqual so you do not have to use a try/catch.

  1. comb.deepEqual({a : 1, b : 2}, {a : 1, b : 2}); \\ true
  2. comb.deepEqual({a : 1}, {a : 1, b : 2}); \\false

License

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

Meta