-
Notifications
You must be signed in to change notification settings - Fork 20
/
patrun.ts
504 lines (401 loc) · 11.1 KB
/
patrun.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/* Copyright (c) 2013-2022 Richard Rodger, MIT License */
// TODO: matchers should accept string[] of key names - only operate on these keys
// TODO: expose walk as method for general purpose
import { Gex } from 'gex'
import {
Matcher,
MatchValue,
GexMatcher,
IntervalMatcher,
} from './lib/matchers'
function Patrun(custom?: any) {
custom = custom || {}
var self: any = {}
var top: any = {}
let matchers: Matcher[] = []
if (custom.gex) {
matchers.push(new GexMatcher())
}
if (custom.interval) {
matchers.push(new IntervalMatcher())
}
// Provide internal search order structure
self.top = function () {
return top
}
self.add = function (pat: any, data: any) {
pat = { ...pat }
var customizer =
'function' === typeof custom ? custom.call(self, pat, data) : null
var keys = Object.keys(pat)
.filter((key) => null != pat[key])
.sort()
keys.forEach(function (key) {
pat[key] = String(pat[key])
})
var keymap: any = top
var valmap: any
// Partial matches return next wider match - see partial-match test.
// Traverse the key path (keys are ordered), insert preserves order.
for (var i = 0; i < keys.length; i++) {
// console.log('L', i, keys.length)
var key = keys[i]
var fix = pat[key]
let mv: MatchValue | undefined = matchers.reduce(
(m, t) => m || t.make(key, fix),
undefined,
)
// if (mv) mv.val$ = fix
valmap = keymap.v
// console.log('S0',key,fix,keymap,valmap)
// An existing key
if (valmap && key == keymap.k) {
// console.log('S1-a')
// add_mv(keymap, key, mv)
if (mv) {
var g = (keymap.g = keymap.g || {})
var ga = (g[key] = g[key] || [])
mv = (ga.find((gmv: MatchValue) => gmv.same(mv)) ||
(ga.push(mv), mv)) as MatchValue
keymap = mv.keymap || (mv.keymap = {})
} else {
keymap = valmap[fix] || (valmap[fix] = {})
}
}
// End of key path reached, so this is a new key, ordered last
else if (!keymap.k) {
keymap.k = key
keymap.v = {}
if (mv) {
var g = (keymap.g = keymap.g || {})
var ga = (g[key] = g[key] || [])
mv = (ga.find((gmv: MatchValue) => gmv.same(mv)) ||
(ga.push(mv), mv)) as MatchValue
keymap = mv.keymap || (mv.keymap = {})
} else {
keymap = keymap.v[fix] = {}
}
}
// Insert key orders before next existing key in path, so insert
else if (key < keymap.k) {
// console.log('S1-c', key, keymap.k)
var s = keymap.s
var g = keymap.g
keymap.s = {
k: keymap.k,
// sk: keymap.sk,
v: keymap.v,
}
if (s) {
keymap.s.s = s
}
if (g) {
keymap.s.g = g
}
if (keymap.g) {
keymap.g = {}
}
keymap.k = key
keymap.v = {}
if (mv) {
var g = (keymap.g = keymap.g || {})
var ga = (g[key] = g[key] || [])
mv = (ga.find((gmv: MatchValue) => gmv.same(mv)) ||
(ga.push(mv), mv)) as MatchValue
keymap = mv.keymap || (mv.keymap = {})
} else {
keymap = keymap.v[fix] = {}
}
}
// Follow star path
else {
keymap = keymap.s || (keymap.s = {})
// NOTE: current key is still not inserted
i--
}
}
if (void 0 !== data && keymap) {
keymap.d = data
if (customizer) {
keymap.f =
'function' === typeof customizer ? customizer : customizer.find
keymap.r =
'function' === typeof customizer.remove ? customizer.remove : void 0
}
}
return self
}
self.findexact = function (pat: any) {
return self.find(pat, true)
}
self.find = function (pat: any, exact: any, collect: any) {
if (null == pat) return null
var keymap: any = top
var data: any = void 0 === top.d ? null : top.d
var finalfind = top.f
var key = null
var stars = []
var foundkeys: any = {}
var patlen = Object.keys(pat).length
var collection = []
if (void 0 !== top.d) {
collection.push(top.d)
}
do {
key = keymap.k
if (keymap.v) {
var val = pat[key]
// Matching operation is either string equality (by prop lookup)
// or gex match.
var nextkeymap = keymap.v[val]
if (!nextkeymap && keymap.g && keymap.g[key]) {
var ga = keymap.g[key]
for (var gi = 0; gi < ga.length; gi++) {
if (ga[gi].match(val)) {
nextkeymap = ga[gi].keymap
break
}
}
}
if (nextkeymap) {
foundkeys[key] = true
if (keymap.s) {
stars.push(keymap.s)
}
data = void 0 === nextkeymap.d ? (exact ? null : data) : nextkeymap.d
if (collect && void 0 !== nextkeymap.d) {
collection.push(nextkeymap.d)
}
finalfind = nextkeymap.f
keymap = nextkeymap
}
// no match found for this value, follow star trail
else {
keymap = keymap.s
}
} else {
keymap = null
}
if (
null == keymap &&
0 < stars.length &&
(null == data || (collect && !exact))
) {
keymap = stars.pop()
}
} while (keymap)
if (exact) {
if (Object.keys(foundkeys).length !== patlen) {
data = null
}
} else {
// If there's root data, return as a catch all
if (null == data && void 0 !== top.d) {
data = top.d
}
}
if (finalfind) {
data = finalfind.call(self, pat, data)
}
return collect ? collection : data
}
self.remove = function (pat: any) {
var keymap = top
var data = null
var key
var path = []
do {
key = keymap.k
// console.log('keymap v g', keymap.v, keymap.g)
if (keymap.v || keymap.g) {
if (keymap.v) {
var nextkeymap = keymap.v[pat[key]]
if (nextkeymap) {
path.push({ km: keymap, v: pat[key] })
}
}
if (null == nextkeymap && keymap.g) {
let mvs: MatchValue[] = keymap.g[key] || []
for (let mvi = 0; mvi < mvs.length; mvi++) {
// TODO: should parse!
if (mvs[mvi].fix === pat[key]) {
path.push({ km: keymap, v: pat[key], mv: mvs[mvi] })
nextkeymap = mvs[mvi].keymap
break
}
}
}
if (nextkeymap) {
data = nextkeymap.d
keymap = nextkeymap
} else {
keymap = keymap.s
}
} else {
keymap = null
}
} while (keymap)
if (void 0 !== data) {
var part = path[path.length - 1]
if (part && part.km && part.km.v) {
var point = part.km.v[part.v] || (part.mv && part.mv.keymap)
if (point && (!point.r || point.r(pat, point.d))) {
delete point.d
}
}
}
}
// values can be verbatim, glob, or array of globs
self.list = function (pat: any, exact: boolean) {
pat = pat || {}
function descend(keymap: any, match: any, missing: any, acc: any) {
if (keymap.v) {
var key = keymap.k
var gexval = Gex(
pat ? (null == pat[key] ? (exact ? null : '*') : pat[key]) : '*',
)
var itermatch = { ...match }
var itermissing = { ...missing }
var nextkeymap
for (var val in keymap.v) {
if (
val === pat[key] ||
(!exact && null == pat[key]) ||
gexval.on(val)
) {
var valitermatch = { ...itermatch }
valitermatch[key] = val
var valitermissing = { ...itermissing }
delete valitermissing[key]
nextkeymap = keymap.v[val]
if (
0 === Object.keys(valitermissing).length &&
nextkeymap &&
nextkeymap.d
) {
acc.push({
match: valitermatch,
data: nextkeymap.d,
find: nextkeymap.f,
})
}
if (nextkeymap && null != nextkeymap.v) {
descend(
nextkeymap,
{ ...valitermatch },
{ ...valitermissing },
acc,
)
}
}
}
nextkeymap = keymap.s
if (nextkeymap) {
descend(nextkeymap, { ...itermatch }, { ...itermissing }, acc)
}
}
}
var acc = []
if (top.d) {
acc.push({
match: {},
data: top.d,
find: top.f,
})
}
descend(top, {}, { ...pat }, acc)
return acc
}
self.toString = function (first: any, second: any) {
var tree = true === first ? true : !!second
var dstr =
'function' === typeof first
? first
: function (d: any) {
return 'function' === typeof d ? '<' + d.name + '>' : '<' + d + '>'
}
function indent(o: any, d: any) {
for (var i = 0; i < d; i++) {
o.push(' ')
}
}
var str: any[] = []
function walk(n: any, o: any, d: any, vs: any) {
var vsc
if (void 0 !== n.d) {
o.push(' ' + dstr(n.d))
str.push(vs.join(', ') + ' -> ' + dstr(n.d))
}
if (n.k) {
o.push('\n')
indent(o, d)
o.push(n.k + ':')
}
if (n.v || n.s || n.g) {
d++
}
if (n.v) {
// d++
var pa = Object.keys(n.v).sort()
for (var pi = 0; pi < pa.length; pi++) {
var p = pa[pi]
o.push('\n')
indent(o, d)
o.push(p + ' ->')
vsc = vs.slice()
vsc.push(n.k + '=' + p)
walk(n.v[p], o, d + 1, vsc)
}
}
if (n.g) {
var pa = Object.keys(n.g).sort()
for (var pi = 0; pi < pa.length; pi++) {
var mvs = n.g[pa[pi]]
for (var mvi = 0; mvi < mvs.length; mvi++) {
var mv = mvs[mvi]
o.push('\n')
indent(o, d)
o.push(mv.fix + ' ~>')
vsc = vs.slice()
vsc.push(n.k + '~' + mv.fix)
walk(mv.keymap, o, d + 1, vsc)
}
}
}
if (n.s) {
o.push('\n')
indent(o, d)
o.push('|')
vsc = vs.slice()
walk(n.s, o, d + 1, vsc)
}
}
var o: any = []
walk(top, o, 0, [])
return tree ? o.join('') : str.join('\n')
}
self.inspect = self.toString
self.toJSON = function (indent: any) {
return JSON.stringify(
top,
function (_key: any, val: any) {
if ('function' === typeof val) return '[Function]'
return val
},
indent,
)
}
return self
}
type PatrunRouter = ReturnType<typeof Patrun>
function make(custom?: any) {
return new (Patrun as any)(custom)
}
if ('undefined' !== typeof module) {
module.exports = make
module.exports.Patrun = Patrun
module.exports.Gex = Gex
}
export default make
export { Patrun, Gex }
export type { PatrunRouter }