-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathutils.ts
122 lines (116 loc) · 3.16 KB
/
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
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
import type { TreeMateOptions } from 'treemate'
import type {
SelectBaseOption,
SelectGroupOption,
SelectIgnoredOption,
SelectMixedOption,
SelectOption
} from './interface'
export function getIsGroup(option: SelectMixedOption): boolean {
return option.type === 'group'
}
export function getIgnored(option: SelectMixedOption): boolean {
return option.type === 'ignored'
}
export function patternMatched(pattern: string, value: string): boolean {
try {
return !!(
1 + value.toString().toLowerCase().indexOf(pattern.trim().toLowerCase())
)
}
catch {
return false
}
}
export function createTmOptions(
valueField: string,
childrenField: string
): TreeMateOptions<SelectBaseOption, SelectGroupOption, SelectIgnoredOption> {
const options: TreeMateOptions<
SelectBaseOption,
SelectGroupOption,
SelectIgnoredOption
> = {
getIsGroup,
getIgnored,
getKey(option: SelectMixedOption): string | number {
if (getIsGroup(option)) {
return (
((option as SelectGroupOption).name as any)
|| (option as SelectGroupOption).key
|| 'key-required'
)
}
// Required for non-custom label & value field
return (option as SelectBaseOption | SelectIgnoredOption)[
valueField
] as NonNullable<SelectOption['value']>
},
getChildren(option) {
return option[childrenField] as SelectBaseOption[]
}
}
return options
}
export function filterOptions(
originalOpts: SelectMixedOption[],
filter: (pattern: string, option: SelectBaseOption) => boolean,
pattern: string,
childrenField: string
): SelectMixedOption[] {
if (!filter)
return originalOpts
function traverse(options: SelectMixedOption[]): SelectMixedOption[] {
if (!Array.isArray(options))
return []
const filteredOptions: SelectMixedOption[] = []
for (const option of options) {
if (getIsGroup(option)) {
const children = traverse(option[childrenField] as SelectBaseOption[])
if (children.length) {
filteredOptions.push(
Object.assign({}, option, {
[childrenField]: children
})
)
}
}
else if (getIgnored(option)) {
continue
}
else if (filter(pattern, option as SelectBaseOption)) {
filteredOptions.push(option)
}
}
return filteredOptions
}
return traverse(originalOpts)
}
export function createValOptMap(
options: SelectMixedOption[],
valueField: string,
childrenField: string
): Map<string | number, SelectBaseOption> {
const valOptMap = new Map<string | number, SelectBaseOption>()
options.forEach((option) => {
if (getIsGroup(option)) {
;(option[childrenField] as SelectBaseOption[]).forEach(
(selectGroupOption: SelectBaseOption) => {
valOptMap.set(
selectGroupOption[valueField] as NonNullable<
SelectBaseOption['value']
>,
selectGroupOption
)
}
)
}
else {
valOptMap.set(
option[valueField] as NonNullable<SelectBaseOption['value']>,
option as SelectBaseOption
)
}
})
return valOptMap
}