ethers.js/lib.esm/_admin/utils/json.js
2022-09-05 16:57:11 -04:00

26 lines
817 B
JavaScript

import fs from "fs";
import { atomicWrite } from "./fs.js";
export function loadJson(path) {
return JSON.parse(fs.readFileSync(path).toString());
}
export function saveJson(filename, data, sort) {
let replacer = undefined;
if (sort) {
replacer = (key, value) => {
if (Array.isArray(value)) {
// pass
}
else if (value && typeof (value) === "object") {
const keys = Object.keys(value);
keys.sort();
return keys.reduce((accum, key) => {
accum[key] = value[key];
return accum;
}, {});
}
return value;
};
}
atomicWrite(filename, JSON.stringify(data, replacer, 2) + "\n");
}
//# sourceMappingURL=json.js.map