-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerator.js
103 lines (95 loc) · 2.95 KB
/
generator.js
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
/**
* Main function called by the html file
*/
const generator = () => {
getFiles();
}
/**
* Get added files and call event "handleFiles"
*/
const getFiles = () => {
let inputElement = document.getElementById("fileInput");
inputElement.addEventListener("change", handleFiles, false);
}
/**
* Fills an array with JSON objects representing the content
* of the files and merges all the objects in the array
*/
async function handleFiles() {
try {
let fileList = this.files;
let fileArray = [];
let result = {};
for await (const file of fileList) {
const fileContents = await readUploadedFileAsText(file)
jsonContent = JSON.parse(fileContents);
fileArray.push(jsonContent);
}
for (let i = 0; i < fileArray.length; i++) {
result = deepMergeJson(result, fileArray[i]);
}
exportToJsonFile(result);
} catch (error) {
console.warn(error.message)
}
}
/**
* Reads the contents of an uploaded file and returns the result
* @param {*} inputFile An uploaded file
* @returns {Promise} String representing the content of inputFile
*/
const readUploadedFileAsText = (inputFile) => {
const temporaryFileReader = new FileReader();
return new Promise((resolve, reject) => {
temporaryFileReader.onerror = () => {
temporaryFileReader.abort();
reject(new DOMException("Problem parsing input file."));
};
temporaryFileReader.onload = () => {
resolve(temporaryFileReader.result);
};
temporaryFileReader.readAsText(inputFile);
});
};
/*
* Deep merge two or more objects together.
* Inspired by (c) 2019 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Object} The objects to merge together
* @returns {Object} Merged values of defaults and options
*/
function deepMergeJson(...file) {
// Setup merged object
let newObj = {};
// Merge the object into the newObj object
let merge = function (obj) {
for (let prop in obj) {
if (obj.hasOwnProperty(prop)) {
// If property is an object, merge properties
if (Object.prototype.toString.call(obj[prop]) === '[object Object]') {
newObj[prop] = deepMergeJson(newObj[prop], obj[prop]);
} else {
newObj[prop] = obj[prop];
}
}
}
};
// Loop through each object and conduct a merge
for (let i = 0; i < file.length; i++) {
merge(file[i]);
}
return newObj;
};
/**
* Displays a window to download the json file
* @param {Object} jsonData The result of the deepMerge of the files
*/
const exportToJsonFile = (jsonData) => {
let dataStr = JSON.stringify(jsonData);
let dataUri = 'data:application/json;charset=utf-8,'+ encodeURIComponent(dataStr);
let exportFileDefaultName = 'result.json';
let linkElement = document.createElement('a');
linkElement.setAttribute('href', dataUri);
linkElement.setAttribute('download', exportFileDefaultName);
linkElement.click();
}
generator();