Skip to content

Commit

Permalink
fix(autocomplete): cartesian product mistake (#2929)
Browse files Browse the repository at this point in the history
  • Loading branch information
chizukicn committed Aug 1, 2023
1 parent d44d745 commit 44ec3c8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 0 additions & 2 deletions packages/autocomplete/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export function searchAttrKey(content: string, cursor: number) {
}

export function cartesian<T>(arr: T[][]): T[][] {
if (arr.length < 2)
return arr
return arr.reduce(
(a, b) => {
const ret: T[][] = []
Expand Down
24 changes: 24 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { mergeDeep } from '@unocss/core'
import { getComponent } from '@unocss/preset-mini/utils'
import { expect, it } from 'vitest'
import { addRemToPxComment, getColorString } from '@unocss/vscode/utils'
import { cartesian } from '@unocss/autocomplete'

it('mergeDeep', () => {
expect(mergeDeep<any>({
Expand Down Expand Up @@ -114,3 +115,26 @@ it('addRemToPxComment', () => {
expect(addRemToPxComment(text, 0)).eql(text)
expect(addRemToPxComment(text, -1)).eql(text)
})

it('cartesian', () => {
const a = ['a', 'b', 'c']
const b = ['1', '2', '3']
// multiple
expect(cartesian([a, b])).eql([
['a', '1'],
['a', '2'],
['a', '3'],
['b', '1'],
['b', '2'],
['b', '3'],
['c', '1'],
['c', '2'],
['c', '3'],
])
// single
expect(cartesian([a])).eql([
['a'],
['b'],
['c'],
])
})

0 comments on commit 44ec3c8

Please sign in to comment.