forked from trustwallet/assets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.ts
More file actions
48 lines (41 loc) · 1.29 KB
/
json.ts
File metadata and controls
48 lines (41 loc) · 1.29 KB
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
import {
readFileSync,
writeFileSync
} from "./filesystem";
import { sortElements } from "./types";
export function isValidJSON(path: string): boolean {
try {
const rawdata = readFileSync(path);
JSON.parse(rawdata);
return true;
} catch {
return false;
}
}
export function formatJson(content: unknown): string {
return JSON.stringify(content, null, 4);
}
export function formatSortJson(content: unknown[]): string {
return JSON.stringify(sortElements(content), null, 4);
}
// Return if updated
export function formatJsonFile(filename: string): boolean {
const origText: string = readFileSync(filename);
const newText: string = formatJson(JSON.parse(origText));
if (newText == origText) {
return false;
}
writeFileSync(filename, newText);
console.log(`Formatted json file ${filename}`);
return true;
}
export function formatSortJsonFile(filename: string): void {
writeFileSync(filename, formatSortJson(JSON.parse(readFileSync(filename))));
console.log(`Formatted json file ${filename}`);
}
export function readJsonFile(path: string): unknown {
return JSON.parse(readFileSync(path));
}
export function writeJsonFile(path: string, data: unknown): void {
writeFileSync(path, JSON.stringify(data, null, 4));
}