-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
json.ts
133 lines (115 loc) Β· 3.87 KB
/
json.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
import jsonpointer from "jsonpointer";
import { TextLoader } from "./text.js";
export class JSONLoader extends TextLoader {
public pointers: string[];
constructor(filePathOrBlob: string | Blob, pointers: string | string[] = []) {
super(filePathOrBlob);
this.pointers = Array.isArray(pointers) ? pointers : [pointers];
}
protected async parse(raw: string): Promise<string[]> {
const json = JSON.parse(raw.trim());
// If there is no pointers specified we extract all strings we found
const extractAllStrings = !(this.pointers.length > 0);
const compiledPointers = this.pointers.map((pointer) =>
jsonpointer.compile(pointer)
);
return this.extractArrayStringsFromObject(
json,
compiledPointers,
extractAllStrings
);
}
/**
* If JSON pointers are specified, return all strings below any of them
* and exclude all other nodes expect if they match a JSON pointer (to allow to extract strings from different levels)
*
* If no JSON pointer is specified then return all string in the object
*/
private extractArrayStringsFromObject(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
json: any,
pointers: jsonpointer[],
extractAllStrings = false,
keyHasBeenFound = false
): string[] {
if (!json) {
return [];
}
if (typeof json === "string" && extractAllStrings) {
return [json];
}
if (Array.isArray(json) && extractAllStrings) {
let extractedString: string[] = [];
for (const element of json) {
extractedString = extractedString.concat(
this.extractArrayStringsFromObject(element, pointers, true)
);
}
return extractedString;
}
if (typeof json === "object") {
if (extractAllStrings) {
return this.extractArrayStringsFromObject(
Object.values(json),
pointers,
true
);
}
const targetedEntries = this.getTargetedEntries(json, pointers);
const thisLevelEntries = Object.values(json) as object[];
const notTargetedEntries = thisLevelEntries.filter(
(entry: object) => !targetedEntries.includes(entry)
);
let extractedStrings: string[] = [];
// If we found a targeted entry, we extract all strings from it
if (targetedEntries.length > 0) {
for (const oneEntry of targetedEntries) {
extractedStrings = extractedStrings.concat(
this.extractArrayStringsFromObject(oneEntry, pointers, true, true)
);
}
for (const oneEntry of notTargetedEntries) {
extractedStrings = extractedStrings.concat(
this.extractArrayStringsFromObject(oneEntry, pointers, false, true)
);
}
} else if (extractAllStrings || !keyHasBeenFound) {
for (const oneEntry of notTargetedEntries) {
extractedStrings = extractedStrings.concat(
this.extractArrayStringsFromObject(
oneEntry,
pointers,
extractAllStrings
)
);
}
}
return extractedStrings;
}
return [];
}
private getTargetedEntries(json: object, pointers: jsonpointer[]): object[] {
const targetEntries = [];
for (const pointer of pointers) {
const targetedEntry = pointer.get(json);
if (targetedEntry) {
targetEntries.push(targetedEntry);
}
}
return targetEntries;
}
}
export class JSONLinesLoader extends TextLoader {
constructor(filePathOrBlob: string | Blob, public pointer: string) {
super(filePathOrBlob);
}
protected async parse(raw: string): Promise<string[]> {
const lines = raw.split("\n");
const jsons = lines
.map((line) => line.trim())
.filter(Boolean)
.map((line) => JSON.parse(line));
const pointer = jsonpointer.compile(this.pointer);
return jsons.map((json) => pointer.get(json));
}
}