Browse Source

fix(common): Fix generateAllCombinations edge case

Michael Bromley 6 years ago
parent
commit
016adf8f5b
2 changed files with 8 additions and 0 deletions
  1. 5 0
      packages/common/src/shared-utils.spec.ts
  2. 3 0
      packages/common/src/shared-utils.ts

+ 5 - 0
packages/common/src/shared-utils.spec.ts

@@ -22,4 +22,9 @@ describe('generateAllCombinations()', () => {
             ['blue', 'large'],
         ]);
     });
+
+    it('works with second array empty', () => {
+        const result = generateAllCombinations([['red', 'green', 'blue'], []]);
+        expect(result).toEqual([['red'], ['green'], ['blue']]);
+    });
 });

+ 3 - 0
packages/common/src/shared-utils.ts

@@ -34,6 +34,9 @@ export function generateAllCombinations<T>(
     k: number = 0,
     output: T[][] = [],
 ): T[][] {
+    if (k === 0) {
+        optionGroups = optionGroups.filter(g => 0 < g.length);
+    }
     if (k === optionGroups.length) {
         output.push(combination);
         return [];