-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
55 lines (50 loc) · 1.4 KB
/
lib.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
const _ = require('lodash');
const sortObject = require('deep-sort-object');
const yaml = require('node-yaml');
module.exports = {
addAndWrite({ object, filePath, key, translation }) {
return new Promise((resolve, reject) => {
let updated = _.set(object, key, translation);
let sorted = sortObject(updated);
yaml.write(filePath, sorted, (error) => {
if (error) { reject(error); }
resolve();
});
});
},
findDuplicates(file, translation, recursion) {
let res = false;
let paths = [];
for (let key in file) {
if (file.hasOwnProperty(key)) {
if (typeof file[key] == 'object') {
if (res = this.findDuplicates(file[key], translation, true)) {
res = `${key}.${res}`;
if (!recursion) {
paths.push(res);
} else {
return res;
}
}
} else if (translation === file[key]) {
if (!recursion) {
paths.push(key);
} else {
return key;
}
}
}
}
return recursion ? res : paths;
},
isSimpleTranslation(key, translation) {
let majorGroups = [
'global',
'actions',
'table_headers'
];
let smallTranslation = translation.trim().split(' ').length <= 2;
let [rootKey] = key.split('.');
return smallTranslation && !majorGroups.includes(rootKey);
}
};