noxon
Version:
Better JSON parsing and manipulation in TypeScript.
40 lines (39 loc) • 1.18 kB
JavaScript
import { fetchJSON } from "../fileHandling/fetch";
import { stringifyJSON } from "../utils/stringify";
const merge = (value1, value2) => {
try {
if (value1 === void 0 || value2 === void 0) {
console.warn("Warning: Value is undefined");
return;
}
if (value1 === null || value2 === null)
throw new Error("Values can't be null in mergeJSON() function");
return "[" + stringifyJSON(value1) + "," + stringifyJSON(value2) + "]";
} catch (error) {
console.error(error);
return void 0;
}
};
const mergeFiles = async (file1, file2) => {
try {
if (file1 === void 0 || file2 === void 0) {
console.warn("Warning: Files are undefined");
return;
}
if (typeof file1 !== "string" || typeof file2 !== "string")
throw new Error("File names must be type of string");
if (!file1.endsWith(".json") || !file2.endsWith(".json")) {
throw new Error("Passed file names are not JSON files");
}
const obj1 = await fetchJSON(file1);
const obj2 = await fetchJSON(file2);
return { ...obj1, ...obj2 };
} catch (error) {
console.error(error);
return void 0;
}
};
export {
merge,
mergeFiles
};