/
spec-utils.ts
69 lines (59 loc) · 2.06 KB
/
spec-utils.ts
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
import fuzzySort from 'fuzzysort'
import type { FoundSpec } from '@packages/types'
import { ComputedRef, Ref, ref, watch } from 'vue'
import _ from 'lodash'
import { FuzzyFoundSpec, getPlatform } from './tree/useCollapsibleTree'
export function fuzzySortSpecs <T extends FuzzyFoundSpec> (specs: T[], searchValue: string) {
const normalizedSearchValue = getPlatform() === 'win32' ? searchValue.replaceAll('/', '\\') : searchValue
const fuzzySortResult = fuzzySort
.go(normalizedSearchValue, specs, { keys: ['relative', 'baseName'], allowTypo: false, threshold: -3000 })
.map((result) => {
const [relative, baseName] = result
return {
...result.obj,
fuzzyIndexes: {
relative: relative?.indexes ?? [],
baseName: baseName?.indexes ?? [],
},
}
})
return fuzzySortResult
}
export function makeFuzzyFoundSpec (spec: FoundSpec): FuzzyFoundSpec {
return {
...spec,
fuzzyIndexes: {
relative: [],
baseName: [],
},
}
}
export function useCachedSpecs<S extends { absolute: string }> (
specs: ComputedRef<Readonly<S[]>>,
): Ref<Readonly<S[]>> {
const cachedSpecs: Ref<Readonly<S[]>> = ref([])
watch(specs, (currentSpecs, prevSpecs = []) => {
if (!_.isEqual(currentSpecs, prevSpecs)) {
cachedSpecs.value = currentSpecs
}
}, { immediate: true })
return cachedSpecs
}
// Used to split indexes from a baseName match to a fileName + extension (with cy extension) match
// For example, given a filename of Button.cy.tsx:
// - search of 'Butcytsx' yields indexes [0,1,2,7,8,10,11,12]
// - deriveIndexes yields
// {
// fileNameIndexes: [0,1,2], // indexes to highlight in "Button"
// extensionIndexes: [1,2,4,5,6] // indexes to highlight in ".cy.tsx"
// }
export function deriveIndexes (fileName: string, indexes: number[]) {
return indexes.reduce((acc, idx) => {
if (idx < fileName.length) {
acc.fileNameIndexes.push(idx)
} else {
acc.extensionIndexes.push(idx - fileName.length)
}
return acc
}, { fileNameIndexes: <number[]>[], extensionIndexes: <number[]>[] })
}