All files / utils cartesianProduct.ts

100% Statements 52/52
100% Branches 14/14
100% Functions 8/8
100% Lines 48/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 15225x 25x 25x   1x                                                                     28x 8x     20x 2x     18x 2x     16x                             25x         8x 12x     8x 9x 1x   8x           7x       8x   8x 17x   27x     8x             8x 8x 8x 7x     8x   8x 5x     3x 3x   3x     3x 3x 3x 3x   3x 6x     6x     6x 6x     3x 3x 3x 3x   3x                
const supportNewFunction = (() => {
  try {
    return new Function('return 1')() === 1
  } catch (err) {
    return false
  }
})()
 
/**
 * 计算多个数组的笛卡尔积。
 *
 * @param arr 数组内容
 * @example
 * ```typescript
 * cartesianProduct([
 *   ['a', 'b'],
 *   [1, 2],
 * ])
 * // => [['a', 1], ['a', 2], ['b', 1], ['b', 2]]
 * ```
 */
export function cartesianProduct<T>(arr: [T[]]): [T][]
export function cartesianProduct<T, U>(arr: [T[], U[]]): [T, U][]
export function cartesianProduct<T, U, V>(arr: [T[], U[], V[]]): [T, U, V][]
export function cartesianProduct<T, U, V, W>(
  arr: [T[], U[], V[], W[]],
): [T, U, V, W][]
export function cartesianProduct<T, U, V, W, X>(
  arr: [T[], U[], V[], W[], X[]],
): [T, U, V, W, X][]
export function cartesianProduct<T, U, V, W, X, Y>(
  arr: [T[], U[], V[], W[], X[], Y[]],
): [T, U, V, W, X, Y][]
export function cartesianProduct<T, U, V, W, X, Y, Z>(
  arr: [T[], U[], V[], W[], X[], Y[], Z[]],
): [T, U, V, W, X, Y, Z][]
export function cartesianProduct(arr: any[][]): any[][]
 
export function cartesianProduct(arr: any[][]): any[][] {
  if (!Array.isArray(arr)) {
    throw new Error('cartesianProduct expects an array')
  }
 
  if (!arr.length) {
    return []
  }
 
  if (!Array.isArray(arr[0])) {
    throw new Error('set at index 0 must be an array')
  }
 
  return (
    supportNewFunction && arr.length < 100
      ? // 在支持动态构造函数环境下,并且数组长度小于 100 时使用快速模式
        cartesianProductProviders.fast
      : // 否则使用通用模式,如:小程序、数组长度大于等于 100
        cartesianProductProviders.universal
  ).run(arr)
}
 
const cartesianProductProviders: Record<
  'universal' | 'fast',
  {
    run(arr: any[][]): any[][]
    [K: string]: any
  }
> = {
  // https://github.com/angus-c/just/blob/master/packages/array-cartesian-product/index.js
  universal: {
    run(arr) {
      // initialize our product array
      let product = arr[0].map(function (v) {
        return [v]
      })
 
      for (let i = 1; i < arr.length; i++) {
        if (!Array.isArray(arr[i])) {
          throw new Error(`set at index ${i} must be an array`)
        }
        product = cartesianProductProviders.universal.baseProduct(
          product,
          arr[i],
        )
      }
 
      return product
    },
    baseProduct(prevProduct: any[], arr2: any[]) {
      // pre allocate all our memory
      const newProduct = new Array(prevProduct.length * arr2.length)
 
      for (let i = 0; i < prevProduct.length; i++) {
        for (let j = 0; j < arr2.length; j++) {
          // always provide array to array.concat for consistent behavior
          newProduct[i * arr2.length + j] = prevProduct[i].concat([arr2[j]])
        }
      }
      return newProduct
    },
  },
  // https://github.com/ehmicky/fast-cartesian/blob/main/src/main.js
  fast: {
    cache: Object.create(null),
    run(arr) {
      const loopFunc = cartesianProductProviders.fast.getLoopFunc(arr.length)
      const result: any[][] = []
      loopFunc(arr, result)
      return result
    },
    getLoopFunc(length: number) {
      const cachedLoopFunc = cartesianProductProviders.fast.cache[length]
 
      if (cachedLoopFunc !== undefined) {
        return cachedLoopFunc
      }
 
      const loopFunc = cartesianProductProviders.fast.mGetLoopFunc(length)
      cartesianProductProviders.fast.cache[length] = loopFunc
 
      return loopFunc
    },
    mGetLoopFunc(length: number) {
      const prefixArr: string[] = []
      const startArr: string[] = []
      const middleArr: string[] = []
      const endArr: string[] = []
 
      for (let i = 0; i < length; i++) {
        prefixArr.push(
          `if (!Array.isArray(arrays[${i}])) { throw new Error('set at index ${i} must be an array') }`,
        )
        startArr.push(
          `for (var i${i} = 0; i${i} < arrays[${i}].length; i${i}++) {`,
        )
        middleArr.push(`arrays[${i}][i${i}]`)
        endArr.push(`}`)
      }
 
      const prefix = prefixArr.join('\n')
      const start = startArr.join('\n')
      const middle = middleArr.join(',')
      const end = endArr.join('\n')
 
      return new Function(
        'arrays',
        'result',
        `${prefix}\n${start}\nresult.push([${middle}])\n${end}`,
      )
    },
  },
}