]> git.ipfire.org Git - thirdparty/vuejs/create-vue.git/commitdiff
test: fix getCombination function
authorHaoqun Jiang <haoqunjiang@gmail.com>
Tue, 7 Sep 2021 08:36:09 +0000 (16:36 +0800)
committerHaoqun Jiang <haoqunjiang@gmail.com>
Tue, 7 Sep 2021 08:36:09 +0000 (16:36 +0800)
snapshot.js

index 1698d96d2b1aeda4b57a88a8cf208170890c83dd..7ea2cbf81176690dcbce0426a9cacb5b7e8a2f2b 100644 (file)
@@ -13,15 +13,30 @@ function createProjectWithFeatureFlags(flags) {
 
 const featureFlags = ['typescript', 'jsx', 'router', 'vuex', 'with-tests']
 
-// FIXME: not correct
 function getCombinations(arr) {
   const combinations = []
 
-  for (let i = 0; i < arr.length; i++) {
-    for (let j = i; j < arr.length; j++) {
-      const combination = arr.slice(i, j + 1)
-      combinations.push(combination)
+  // The following code & comments are generated by GitHub CoPilot.
+
+  // for an array of 5 elements, there are 2^5 - 1= 31 combinations
+  // (excluding the empty combination)
+  // equivalent to the following:
+  // [0, 0, 0, 0, 1] ... [1, 1, 1, 1, 1]
+  // We can represent the combinations as a binary number
+  // where each digit represents a flag
+  // and the number is the index of the flag
+  // e.g.
+  // [0, 0, 0, 0, 1] = 0b0001
+  // [1, 1, 1, 1, 1] = 0b1111
+
+  for (let i = 1; i < 1 << arr.length; i++) {
+    const combination = []
+    for (let j = 0; j < arr.length; j++) {
+      if (i & (1 << j)) {
+        combination.push(arr[j])
+      }
     }
+    combinations.push(combination)
   }
 
   return combinations