-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson-utility.js
93 lines (82 loc) · 3 KB
/
json-utility.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
'use strict';
const jsonfile = require('jsonfile');
module.exports = {
write: write,
read: read,
getProperty: getProperty,
writeToProperty: writeToProperty,
deleteProperty: deleteProperty,
writePropertyToJsonOjbect: writePropertyToJsonOjbect,
deletePropertyFromJsonObject: deletePropertyFromJsonObject,
getPropertyFromJsonObject: getPropertyFromJsonObject
};
function read(filePath) {
try {
let content = jsonfile.readFileSync(filePath);
return content;
} catch (e) {
console.error('Invalid json: ' + filePath);
process.exit(1);
}
}
function write(filePath, jsonObject) {
try {
jsonfile.writeFileSync(filePath, jsonObject, {spaces: 2});
} catch (e) {
console.error('Invalid file, cannot write to: ' + filePath);
process.exit(1);
}
}
function writeToProperty(filePath, propertyPathArray, writeObject) {
let jsonObject = read(filePath);
writePropertyToJsonOjbect(jsonObject, propertyPathArray, writeObject);
write(filePath, jsonObject);
}
function deleteProperty(filePath, propertyPathArray) {
let jsonObject = read(filePath);
deletePropertyFromJsonObject(jsonObject, propertyPathArray);
write(filePath, jsonObject);
return true;
}
function getProperty(filePath, propertyPathArray) {
let jsonObject = read(filePath);
return getPropertyFromJsonObject(jsonObject, propertyPathArray);
}
function getPropertyFromJsonObject(jsonObject, propertyPathArray) {
let targetObject = jsonObject;
for (let index = 0; index < propertyPathArray.length - 1; index++) {
if (!targetObject.hasOwnProperty(propertyPathArray[index])) {
return null;
}
targetObject = targetObject[propertyPathArray[index]];
}
return targetObject[propertyPathArray[propertyPathArray.length - 1]];
}
function deletePropertyFromJsonObject(jsonObject, propertyPathArray) {
let targetObject = jsonObject;
for (let index = 0; index < propertyPathArray.length - 1; index++) {
if (!targetObject.hasOwnProperty(propertyPathArray[index])) {
return false;
}
targetObject = targetObject[propertyPathArray[index]];
}
delete targetObject[propertyPathArray[propertyPathArray.length - 1]];
}
function writePropertyToJsonOjbect(jsonObject, propertyPathArray, writeObject) {
let targetObject = jsonObject;
for (let index = 0; index < propertyPathArray.length - 1; index++) {
if (!targetObject.hasOwnProperty(propertyPathArray[index])) {
if (typeof targetObject !== 'object') {
break;
}
targetObject[propertyPathArray[index]] = {};
}
targetObject = targetObject[propertyPathArray[index]];
}
if (typeof targetObject === 'object') {
targetObject[propertyPathArray[propertyPathArray.length - 1]] = writeObject;
return jsonObject;
}
console.error('[Error]: cannot add property to non-object value. Please correct your target path');
process.exit(1);
}