forked from project-sunbird/sunbird-localization
-
Notifications
You must be signed in to change notification settings - Fork 1
/
formatjson.js
37 lines (35 loc) · 1.33 KB
/
formatjson.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
var _ = require('lodash');
const formatjson = (object) => {
for (let key in object) {
if(key.includes('\r\n') || key.includes('\r') || key.includes('\n') || key.includes('\t')){
let objectValue = object[key];
delete object[key];
key = key.replace('\r\n', '');
key = key.replace('\r', '');
key = key.replace('\n', '');
key = key.replace('\t', '');
if(_.has(object, key)) {
object[key] = _.mergeWith(object[key], objectValue, customizer);
} else {
object[key] = objectValue;
}
}
if (object.hasOwnProperty(key) && (typeof object[key] === "object")) {
formatjson(object[key]);
} else {
if(object[key].includes('\r\n') || object[key].includes('\r') || object[key].includes('\n') || object[key].includes('\t')){
object[key] = object[key].replace('\r\n', '');
object[key] = object[key].replace('\r', '');
object[key] = object[key].replace('\n', '');
object[key] = object[key].replace('\t', '');
}
}
}
return object;
};
function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
exports.formatjson = formatjson;