-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.ts
42 lines (36 loc) · 988 Bytes
/
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
import { FeatureBase, NoFeature, PartialToken, TokenExtension } from '../token';
const _path = Symbol('path');
export interface Path extends FeatureBase<'Path', Path> {
payload: {
readonly [_path]: string;
};
childFeatures: {
[P in PropertyKey]: Path;
};
}
export function addPath(
initialPath: string = ''
): TokenExtension<NoFeature, { add: Path }> {
return tokenBuilder =>
tokenBuilder.addFeature<Path>({
extend: {
[_path]: initialPath,
},
extendChildren: (_, parent, property) => {
let childPath;
if (typeof property === 'number') {
childPath = `${parent[_path]}[${String(property)}]`;
} else {
childPath = parent[_path]
? `${parent[_path]}.${String(property)}`
: String(property);
}
return {
[_path]: childPath,
};
},
});
}
export function getTokenPath(token: PartialToken<Path>): string {
return token[_path];
}