generated from jsonicjs/directive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.ts
61 lines (51 loc) · 1.49 KB
/
path.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
/* Copyright (c) 2022-2024 Richard Rodger, MIT License */
import { Jsonic, Plugin } from '@jsonic/jsonic-next'
type PathOptions = {}
/* Keeps track of the property path to the current location.
* Example: {a:{b:1 ## path=["a","b"]
* Use the Rule.k key-value store so that the path is propagated to children and followers.
* Depth must be greater than 0 - ensures path only starts once top level implicit is set up.
*/
const Path: Plugin = (jsonic: Jsonic, _options: PathOptions) => {
jsonic.rule('val', (rs) => {
rs.bo((r, ctx) => {
// At top level, create path array, or inherit from meta context.
if (0 === r.d) {
r.k.path = ctx.meta.path?.base?.slice(0) || []
}
})
})
jsonic.rule('map', (rs) => {
rs.bo((r) => {
// Not in an array, so no need to track element index.
delete r.k.index
})
})
jsonic.rule('pair', (rs) => {
rs.ao((r) => {
if (0 < r.d && r.u.pair) {
r.child.k.path = [...r.k.path, r.u.key]
r.child.k.key = r.u.key
}
})
})
jsonic.rule('list', (rs) => {
rs.bo((r) => {
// In array, the path property is the element index.
r.k.index = -1
})
})
jsonic.rule('elem', (rs) => {
rs.ao((r) => {
if (0 < r.d) {
r.k.index = 1 + r.k.index
r.child.k.path = [...r.k.path, r.k.index]
r.child.k.key = r.k.index
r.child.k.index = r.k.index
}
})
})
}
Path.defaults = {} as PathOptions
export { Path }
export type { PathOptions }