just-cartesian-product
Advanced tools
Comparing version 1.0.2 to 3.1.0
32
index.js
@@ -16,15 +16,13 @@ module.exports = cartesianProduct; | ||
function baseProduct(arr1, arr2) { | ||
var output = []; | ||
function baseProduct(prevProduct, arr2) { | ||
//pre allocate all our memory | ||
var newProduct = new Array(prevProduct.length * arr2.length); | ||
for (var i = 0; i < arr1.length; i++) { | ||
var item = isArray(arr1[i]) ? arr1[i] : [arr1[i]]; | ||
for (var i = 0; i < prevProduct.length; i++) { | ||
for (var j = 0; j < arr2.length; j++) { | ||
var items = item.concat([arr2[j]]); | ||
output.push(items); | ||
//always provide array to array.concat for consistent behavior | ||
newProduct[i * arr2.length + j] = prevProduct[i].concat([arr2[j]]); | ||
} | ||
} | ||
return output; | ||
return newProduct; | ||
} | ||
@@ -34,3 +32,3 @@ | ||
if (!isArray(arr)) { | ||
throw new Error('just-array-product expects an array'); | ||
throw new Error('just-cartesian-product expects an array'); | ||
} | ||
@@ -42,9 +40,17 @@ | ||
var output = arr[0]; | ||
if(!isArray(arr[0])) { | ||
throw new Error('set at index 0 must be an array'); | ||
} | ||
//initialize our product array | ||
var product = arr[0].map(function(v) { return [v]; }); | ||
for (var i = 1; i < arr.length; i++) { | ||
output = baseProduct(output, arr[i]); | ||
if(!isArray(product)) { | ||
throw new Error('set at index ' + i + ' must be an array'); | ||
} | ||
product = baseProduct(product, arr[i]); | ||
} | ||
return output; | ||
return product; | ||
} |
{ | ||
"name": "just-cartesian-product", | ||
"version": "1.0.2", | ||
"version": "3.1.0", | ||
"description": "Cartesian product of arrays", | ||
"type": "commonjs", | ||
"main": "index.js", | ||
"module": "index.esm.js", | ||
"exports": { | ||
".": { | ||
"require": "./index.js", | ||
"default": "./index.esm.js" | ||
} | ||
}, | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "rollup -c" | ||
}, | ||
@@ -9,0 +19,0 @@ "repository": "https://github.com/angus-c/just", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6157
8
121