-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslice.ts
44 lines (37 loc) · 1011 Bytes
/
slice.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
/**
* Concentrate JSON to use fewer tokens
*
* arguments:
* -f <file>
* -f <file> is the JSON file to concentrate
* -f <file> is required
*
* --levelsDown
* --levelsDown is the number of levels to go down
*/
import { readFile } from "node:fs/promises";
import yargs from "yargs";
import { objectSubtrees } from "./utils";
(async () => {
const options = yargs
.usage("Usage: -f <file> --levelsDown <number>")
.option("f", {
alias: "file",
describe: "JSON file to slice",
type: "string",
demandOption: true,
})
.option("levelsDown", {
describe: "Number of levels to go down",
type: "number",
demandOption: true,
}).argv;
const { file, levelsDown } = options;
const input = await readFile(file, { encoding: "utf8" });
const data = JSON.parse(input);
const slices = objectSubtrees(data, levelsDown);
slices.map((slice, index) => {
console.log(`SLICE ${index}:`);
console.log(JSON.stringify(slice));
});
})();