@formvk/path 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,280 +0,0 @@
1
- import { expect, test } from 'vitest'
2
-
3
- import { Path } from '../'
4
- import { Matcher } from '../matcher'
5
-
6
- function match(obj: Record<string, any>) {
7
- for (const name in obj) {
8
- test('test match ' + name, () => {
9
- const path = new Path(name)
10
- if (Array.isArray(obj[name]) && Array.isArray(obj[name][0])) {
11
- obj[name].forEach(_path => {
12
- expect(path.match(_path)).toBeTruthy()
13
- })
14
- } else {
15
- expect(path.match(obj[name])).toBeTruthy()
16
- }
17
- })
18
- }
19
- }
20
-
21
- function unMatch(obj: Record<string, any>) {
22
- for (const name in obj) {
23
- test('test unMatch ' + name, () => {
24
- const path = new Path(name)
25
- if (Array.isArray(obj[name]) && Array.isArray(obj[name][0])) {
26
- obj[name].forEach(_path => {
27
- expect(path.match(_path)).toBeFalsy()
28
- })
29
- } else {
30
- expect(path.match(obj[name])).toBeFalsy()
31
- }
32
- })
33
- }
34
- }
35
-
36
- test('basic match', () => {
37
- expect(Path.parse('xxx').match('')).toBeFalsy()
38
- expect(Path.parse('xxx').match('aaa')).toBeFalsy()
39
- expect(Path.parse('xxx.eee').match('xxx')).toBeFalsy()
40
- expect(Path.parse('*(xxx.eee~)').match('xxx')).toBeFalsy()
41
- expect(Path.parse('xxx.eee~').match('xxx.eee')).toBeTruthy()
42
- expect(Path.parse('*(!xxx.eee,yyy)').match('xxx')).toBeFalsy()
43
- expect(Path.parse('*(!xxx.eee,yyy)').match('xxx.ooo.ppp')).toBeTruthy()
44
- expect(Path.parse('*(!xxx.eee,yyy)').match('xxx.eee')).toBeFalsy()
45
- expect(Path.parse('*(!xxx.eee~,yyy)').match('xxx.eee')).toBeFalsy()
46
- expect(Path.parse('~.aa').match('xxx.aa')).toBeTruthy()
47
- })
48
-
49
- test('not expect match not', () => {
50
- expect(new Matcher({}).match(['']).matched).toBeFalsy()
51
- })
52
-
53
- test('test matchGroup', () => {
54
- const pattern = new Path('*(aa,bb,cc)')
55
- expect(pattern.matchAliasGroup('aa', 'bb')).toEqual(true)
56
- const excludePattern = new Path('aa.bb.*(11,22,33).*(!aa,bb,cc)')
57
- expect(excludePattern.matchAliasGroup('aa.bb.11.mm', 'aa.cc.dd.bb.11.mm')).toEqual(true)
58
- expect(excludePattern.matchAliasGroup('aa.cc', 'aa.kk.cc')).toEqual(false)
59
- expect(new Path('aa.*(!bb)').matchAliasGroup('kk.mm.aa.bb', 'aa.bb')).toEqual(false)
60
- expect(new Path('aa.*(!bb)').matchAliasGroup('kk.mm.aa.bb.cc', 'kk.mm.aa')).toEqual(false)
61
- expect(new Path('aa.*(!bb,oo)').matchAliasGroup('kk.mm', 'aa')).toEqual(false)
62
- expect(new Path('aa.*(!bb.*)').matchAliasGroup('kk.mm', 'aa')).toEqual(false)
63
- expect(new Path('aa.*(!bb)').matchAliasGroup('kk.mm.aa.cc', 'aa.cc')).toEqual(true)
64
- const patttern2 = Path.parse('*(array)')
65
- expect(patttern2.matchAliasGroup(['array', 0], ['array', 0])).toEqual(false)
66
- })
67
-
68
- test('exclude match', () => {
69
- //路径长度相等
70
- expect(Path.parse('*(!aaa)').match('ggg')).toBeTruthy()
71
- expect(Path.parse('*(!aaa)').match('aaa')).toBeFalsy()
72
- expect(Path.parse('*(!aaa.bbb)').match('ggg.ddd')).toBeTruthy()
73
- expect(Path.parse('*(!aaa.ccc)').match('aaa.ccc')).toBeFalsy()
74
- //长路径匹配短路径
75
- expect(Path.parse('*(!aaa.bbb)').match('ggg')).toBeTruthy()
76
- expect(Path.parse('*(!aaa.bbb)').match('aaa')).toBeFalsy()
77
- //短路径匹配长路径
78
- expect(Path.parse('*(!aaa)').match('aaa.bbb')).toBeTruthy()
79
- expect(Path.parse('*(!aaa)').match('aaa.ccc')).toBeTruthy()
80
- expect(Path.parse('*(!aaa)').match('bbb.ccc')).toBeTruthy()
81
-
82
- expect(Path.parse('*(!aaa,bbb)').match('bbb')).toBeFalsy()
83
- expect(Path.parse('*(!aaa.bbb)').match('aaa.ccc')).toBeTruthy()
84
- expect(Path.parse('*(!basic.name,versionTag)').match('basic.id')).toBeTruthy()
85
- expect(Path.parse('*(!basic.name,versionTag)').match('basic')).toBeFalsy()
86
- expect(Path.parse('*(!basic.name,versionTag)').match('isExecutable')).toBeTruthy()
87
- expect(Path.parse('*(!basic.name,versionTag)').match('versionTag')).toBeFalsy()
88
- expect(Path.parse('*(!basic.name,basic.name.*,versionTag)').match('basic.name')).toBeFalsy()
89
- expect(Path.parse('*(!basic.name,basic.name.*,versionTag)').match('basic.name.kkk')).toBeFalsy()
90
- expect(Path.parse('aa.*(!bb)').match('kk.mm.aa.bb.cc')).toBeFalsy()
91
- expect(Path.parse('aa.*(!bb)').match('aa')).toBeFalsy()
92
- expect(Path.parse('aa.*(!bb.*)').match('aa')).toBeFalsy()
93
- expect(Path.parse('aa.*(!bb,cc)').match('aa')).toBeFalsy()
94
- expect(Path.parse('aa.*(!bb,cc)').match('aa.dd')).toBeTruthy()
95
- expect(Path.parse('aa.*(!bb,cc)').match('aa.kk')).toBeTruthy()
96
- })
97
-
98
- test('match regexp', () => {
99
- expect(Path.parse(/^\d+$/).match('212')).toBeTruthy()
100
- expect(Path.parse(/^\d+$/).match('212dd')).toBeFalsy()
101
- })
102
-
103
- test('test zero', () => {
104
- expect(Path.parse('t.0.value~').match(['t', 0, 'value_list'])).toEqual(true)
105
- })
106
-
107
- test('test optional wild match', () => {
108
- expect(Path.parse('aa.**').match(['aa'])).toEqual(true)
109
- expect(Path.parse('aa.**').match(['aa', 'bb', 'cc'])).toEqual(true)
110
- expect(Path.parse('aa.*').match(['aa'])).toEqual(false)
111
- expect(Path.parse('aa.\\*\\*\\.aa').match(['aa', '**.aa'])).toEqual(true)
112
- expect(Path.parse('aa.[[**.aa]]').match(['aa', '**.aa'])).toEqual(true)
113
- expect(() => Path.parse('aa.**.aa').match(['aa'])).toThrowError()
114
- expect(() => Path.parse('aa.**(bb)').match(['aa'])).toThrowError()
115
- expect(Path.parse('*(aa.**)').match(['aa'])).toEqual(true)
116
- expect(Path.parse('*(aa.**,bb.**)').match(['aa'])).toEqual(true)
117
- expect(Path.parse('*(aa.**,bb.**)').match(['aa', 'bb', 'cc'])).toEqual(true)
118
- expect(Path.parse('*(aa.**,bb.**)').match(['bb'])).toEqual(true)
119
- expect(Path.parse('*(aa.**,bb.**)').match(['bb', 'cc', 'dd'])).toEqual(true)
120
- expect(Path.parse('*(aa.**,bb.**)').match(['cc'])).toEqual(false)
121
- expect(Path.parse('*(aa.**,bb.**).bb').match(['aa', 'oo'])).toEqual(true)
122
- expect(Path.parse('*(aa.**,bb.**).bb').match(['bb', 'oo'])).toEqual(true)
123
- expect(Path.parse('*(aa.**,bb.**).bb').match(['aa', 'oo', 'bb'])).toEqual(true)
124
- expect(Path.parse('*(aa.**,bb.**).bb').match(['bb', 'oo', 'bb'])).toEqual(true)
125
- expect(Path.parse('*(aa.**,bb.**).bb').match(['aa', 'oo', 'kk', 'dd', 'bb'])).toEqual(true)
126
- expect(Path.parse('*(aa.**,bb.**).bb').match(['cc', 'oo', 'kk', 'dd', 'bb'])).toEqual(false)
127
- expect(Path.parse('*(aa.**,bb.**).bb').match(['bb', 'oo', 'kk', 'dd', 'bb'])).toEqual(true)
128
- expect(Path.parse('*(aa.**,bb.**).bb').match(['kk', 'oo', 'kk', 'dd', 'bb'])).toEqual(false)
129
- })
130
-
131
- test('test expand', () => {
132
- expect(Path.parse('t.0.value~').match(['t', 0, 'value_list', 'hello'])).toEqual(false)
133
- })
134
-
135
- test('test multi expand', () => {
136
- expect(Path.parse('*(aa~,bb~).*').match(['aa12323', 'asdasd'])).toEqual(true)
137
- })
138
-
139
- test('test group', () => {
140
- const node = Path.parse('*(phases.*.type,phases.*.steps.*.type)')
141
- expect(node.match('phases.0.steps.1.type')).toBeTruthy()
142
- })
143
-
144
- test('test segments', () => {
145
- const node = Path.parse('a.0.b')
146
- expect(node.match(['a', 0, 'b'])).toEqual(true)
147
- })
148
-
149
- test('nested group match', () => {
150
- expect(Path.parse('aa.*.*(bb,cc).dd.*(kk,oo).ee').match('aa.0.cc.dd.kk.ee')).toEqual(true)
151
- })
152
-
153
- test('group match with destructor', () => {
154
- expect(Path.parse('*([startDate,endDate],date,weak)').match('date')).toEqual(true)
155
- expect(Path.parse('*({startDate,endDate},date,weak)').match('date')).toEqual(true)
156
- expect(Path.parse('*([startDate,endDate],date,weak)').match('xxx')).toEqual(false)
157
- expect(Path.parse('*({startDate,endDate},date,weak)').match('xxx')).toEqual(false)
158
- expect(Path.parse('*([startDate,endDate],date,weak)').match('[startDate,endDate]')).toEqual(true)
159
- expect(Path.parse('*({startDate,endDate},date,weak)').match('{startDate,endDate}')).toEqual(true)
160
- })
161
-
162
- test('all range match', () => {
163
- expect(Path.parse('array.*[:].*[:].*[:].bb').match('array.0.0.0.aa')).toBeFalsy()
164
- })
165
-
166
- match({
167
- '*': [[], ['aa'], ['aa', 'bb', 'cc'], ['aa', 'dd', 'gg']],
168
- '*.a.b': [
169
- ['c', 'a', 'b'],
170
- ['k', 'a', 'b'],
171
- ['m', 'a', 'b'],
172
- ],
173
- 'a.*.k': [
174
- ['a', 'b', 'k'],
175
- ['a', 'd', 'k'],
176
- ['a', 'c', 'k'],
177
- ],
178
- 'a.*(b,d,m).k': [
179
- ['a', 'b', 'k'],
180
- ['a', 'd', 'k'],
181
- ['a', 'm', 'k'],
182
- ],
183
- 'a.*(!b,d,m).*(!a,b)': [
184
- ['a', 'o', 'k'],
185
- ['a', 'q', 'k'],
186
- ['a', 'c', 'k'],
187
- ],
188
- 'a.*(b.c.d,d,m).k': [
189
- ['a', 'b', 'c', 'd', 'k'],
190
- ['a', 'd', 'k'],
191
- ['a', 'm', 'k'],
192
- ],
193
- 'a.*(b.*(c,k).d,d,m).k': [
194
- ['a', 'b', 'c', 'd', 'k'],
195
- ['a', 'b', 'k', 'd', 'k'],
196
- ['a', 'd', 'k'],
197
- ['a', 'm', 'k'],
198
- ],
199
- 'a.b.*': [
200
- ['a', 'b', 'c', 'd'],
201
- ['a', 'b', 'c'],
202
- ['a', 'b', 2, 'aaa', 3, 'bbb'],
203
- ],
204
- '*(step1,step2).*': [
205
- ['step1', 'aa', 'bb'],
206
- ['step1', 'aa', 'bb', 'ccc', 'ddd'],
207
- ],
208
- 'dyanmic.*(!dynamic-1)': [
209
- ['dyanmic', 'dynamic-2'],
210
- ['dyanmic', 'dynamic-3'],
211
- ],
212
- 't.0.value~': [['t', '0', 'value']],
213
- 'a.*[10:50].*(!a,b)': [
214
- ['a', 49, 's'],
215
- ['a', 10, 's'],
216
- ['a', 50, 's'],
217
- ],
218
- 'a.*[10:].*(!a,b)': [
219
- ['a', 49, 's'],
220
- ['a', 10, 's'],
221
- ['a', 50, 's'],
222
- ],
223
- 'a.*[].*(!a,b)': [
224
- ['a', 49, 's'],
225
- ['a', 10, 's'],
226
- ['a', 50, 's'],
227
- ],
228
- 'a.*[:50].*(!a,b)': [
229
- ['a', 49, 's'],
230
- ['a', 10, 's'],
231
- ['a', 50, 's'],
232
- ],
233
- 'a.*([[a.b.c]],[[c.b.d~]])': [
234
- ['a', '[[a.b.c]]'],
235
- ['a', 'c.b.d~'],
236
- ],
237
- 'a.*(!k,d,m).k': [
238
- ['a', 'u', 'k'],
239
- ['a', 'o', 'k'],
240
- ['a', 'p', 'k'],
241
- ],
242
- 'a\\.\\*\\[1\\]': [['a.*[1]']],
243
- '[[\\[aa,bb\\]]]': [['[aa,bb]']],
244
- '[[\\[aa,bb\\] ]]': [['[aa,bb] ']],
245
- '[[ \\[aa,bb~\\] ]]': [[' [aa,bb~] ']],
246
- 'aa.bb.*': [['aa', 'bb', 'ccc']],
247
- 'a.*': [
248
- ['a', 'b'],
249
- ['a', 'b', 'c'],
250
- ],
251
- 'aa.*.*(bb,cc).dd': [['aa', '0', 'cc', 'dd']],
252
- 'aaa.products.0.*': [['aaa', 'products', '0', 'aaa']],
253
- 'aa~.ccc': [
254
- ['aa', 'ccc'],
255
- ['aa12', 'ccc'],
256
- ],
257
- '*(aa~,bb~).*': [
258
- ['aa12323', 'asdasd'],
259
- ['bb12222', 'asd'],
260
- ],
261
- '*(aa,bb,bb.aa)': [['bb', 'aa']],
262
- '*(!aa,bb,bb.aa)': [['xx'], ['yyy']],
263
- '*(!aaa)': [['bbb']],
264
- '*(!aaa,bbb)': [['ccc'], ['ggg']],
265
- '*([startDate,endDate],date,weak)': [['date']],
266
- })
267
-
268
- unMatch({
269
- 'a.*': [['a'], ['b']],
270
- '*(array)': [['array', '0']],
271
- 'aa.bb.*': [['aa', 'bb']],
272
- 'a.*.b': [['a', 'k', 'b', 'd']],
273
- '*(!aaa)': [['aaa']],
274
- 'dyanmic.*(!dynamic-1)': [['dyanmic', 'dynamic-1']],
275
- 'dyanmic.*(!dynamic-1.*)': [['dyanmic', 'dynamic-1', 'ccc']],
276
- a: [['c', 'b']],
277
- 'aa~.ccc': [['a', 'ccc'], ['aa'], ['aaasdd']],
278
- bb: [['bb', 'cc']],
279
- 'aa.*(cc,bb).*.aa': [['aa', 'cc', '0', 'bb']],
280
- })