-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
41 lines (36 loc) · 1004 Bytes
/
example.js
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
const {json2ast, ast2json, transform} = require ('./index.js');
const json = {
hello1: 'hello world',
hello2: ['hello', 2, true],
hellonested: {
nested1: 'nested',
nested2: ['world', 7, false],
},
};
const ast = json2ast (json);
const json2 = ast2json (ast);
console.log ('JSON => AST => JSON');
console.log ('ORIGINAL: %O', json);
console.log ('AST: %O', ast);
console.log ('COPY: %O', json2);
console.log ('-------------------');
function pluginKeyReverse (api) {
return {
[api.AstType.key]: node => {
node.value.value = node.value.value.split ('').reverse ().join ('');
},
};
}
function pluginNotBoolean (api) {
return {
[api.AstType.boolean]: node => {
node.value = !node.value;
},
};
}
const result = transform (json, [pluginKeyReverse, pluginNotBoolean]);
console.log ('TRANSFORM:');
console.log ('ORIGINAL: %O', json);
console.log ('NEW JSON: %O', result.json);
console.log ('NEW AST: %O', result.ast);
console.log ('-------------------');