wink-helpers
Advanced tools
Comparing version 1.0.1 to 1.1.0
{ | ||
"name": "wink-helpers", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Low level helper functions for Javascript arrays and objects", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -42,2 +42,5 @@ | ||
// -> [ { "name": "marry", "age": 37 }, { "name": "john", "age": 42 } ] | ||
console.log( helpers.array.product( [ [ 9, 8 ], [ 1, 2 ] ] ) ); | ||
// -> [ [ 9, 1 ], [ 9, 2 ], [ 8, 1 ], [ 8, 2 ] ] | ||
``` | ||
@@ -72,2 +75,15 @@ | ||
#### product( array ) | ||
Returns the *cartesian product* of the arrays present inside the `array` argument. For example, if the `array` argument is `[ [ 1, 2, 3 ], [ 4 ], [ 5, 6 ] ]`, then the return value will be: | ||
```javascript | ||
[ | ||
[ 1, 4, 5 ], | ||
[ 1, 4, 6 ], | ||
[ 2, 4, 5 ], | ||
[ 2, 4, 6 ], | ||
[ 3, 4, 5 ], | ||
[ 3, 4, 6 ] | ||
] | ||
``` | ||
### object | ||
@@ -74,0 +90,0 @@ |
@@ -27,2 +27,25 @@ // wink-helpers | ||
// ### Private Functions | ||
// #### Product Reducer (Callback) | ||
// Callback function used by `reduce` inside the `product()` function. | ||
// Follows the standard guidelines of `reduce()` callback function. | ||
var productReducer = function ( prev, curr ) { | ||
var c, | ||
cmax = curr.length; | ||
var p, | ||
pmax = prev.length; | ||
var result = []; | ||
for ( p = 0; p < pmax; p += 1 ) { | ||
for ( c = 0; c < cmax; c += 1 ) { | ||
result.push( prev[ p ].concat( curr[ c ] ) ); | ||
} | ||
} | ||
return ( result ); | ||
}; // productReducer() | ||
// ### Public Function | ||
// ### Array Helpers | ||
@@ -120,3 +143,15 @@ | ||
// #### product | ||
// Finds the Cartesian Product of arrays present inside the array `a`. Therefore | ||
// the array `a` must be an array of 1-dimensional arrays. For example, | ||
// `product( [ [ 9, 8 ], [ 1, 2 ] ] )` | ||
// will produce `[ [ 9, 1 ], [ 9, 2 ], [ 8, 1 ], [ 8, 2 ] ]`. | ||
helpers.array.product = function ( a ) { | ||
return ( | ||
a.reduce( productReducer, [ [] ] ) | ||
); | ||
}; | ||
// ### Object Helpers | ||
@@ -123,0 +158,0 @@ |
@@ -176,2 +176,43 @@ // wink-helpers | ||
describe( 'cartesian product', function () { | ||
var expectedOutputIs = [ | ||
[ [ 9, 1 ], [ 9, 2 ], [ 8, 1 ], [ 8, 2 ] ], | ||
[ ], | ||
[ | ||
[ 1, 4, 5 ], | ||
[ 1, 4, 6 ], | ||
[ 2, 4, 5 ], | ||
[ 2, 4, 6 ], | ||
[ 3, 4, 5 ], | ||
[ 3, 4, 6 ] | ||
], | ||
[ | ||
[ 1, 4, 6 ], | ||
[ 1, 4, 7 ], | ||
[ 1, 5, 6 ], | ||
[ 1, 5, 7 ], | ||
[ 2, 4, 6 ], | ||
[ 2, 4, 7 ], | ||
[ 2, 5, 6 ], | ||
[ 2, 5, 7 ], | ||
[ 3, 4, 6 ], | ||
[ 3, 4, 7 ], | ||
[ 3, 5, 6 ], | ||
[ 3, 5, 7 ] | ||
] | ||
], | ||
whenInputIs = [ | ||
[ [ 9, 8 ], [ 1, 2 ] ], | ||
[ [ 9, 8 ], [ 1, 2 ], [ ] ], | ||
[ [ 1, 2, 3 ], [ 4 ], [ 5, 6 ] ], | ||
[ [ 1, 2, 3 ], [ 4, 5 ], [ 6, 7 ] ] | ||
]; | ||
whenInputIs.forEach( function ( input, i ) { | ||
it( 'should return ' + JSON.stringify( expectedOutputIs[ i ] ) + '\n\tif the input is ' + JSON.stringify( input ), function () { | ||
expect( helpers.array.product( input ) ).to.deep.equal( expectedOutputIs[ i ] ); | ||
} ); | ||
} ); | ||
} ); | ||
describe( 'isObject', function () { | ||
@@ -178,0 +219,0 @@ var tests = [ |
69694
730
117