-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.js
84 lines (77 loc) · 2.79 KB
/
tools.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
const editObjectPath = (action, object, path, value, createIgnore = true) => {
if (typeof object !== 'object') {
throw new Error('Not a valid object passed to setObjectPath');
}
path = typeof path === 'string' ? path.split('.') : path;
if (!Array.isArray(path)) {
throw new Error('Invalid path passed to setObjectPath');
}
let handle = object;
let depth = 0;
for (let sub of path.slice(0, -1)) {
if (handle[sub] == undefined) {
if (createIgnore) {
if (action === 'set') {
handle[sub] = {};
} else {
return;
}
} else {
throw new Error('{OBJECT}.' + path.slice(0, depth + 1).join('.') + ' is undefined.');
}
}
if (typeof handle[sub] !== 'object') {
throw new Error('{OBJECT}.' + path.slice(0, depth + 1).join('.') + ' is not an object');
}
handle = handle[sub];
depth++;
}
if (action === 'set') {
handle[path.slice(-1)[0]] = value;
} else if (action === 'delete') {
if (handle[path.slice(-1)[0]] === undefined) {
if (!createIgnore) {
throw new Error('{OBJECT}.' + path.join('.') + ' is not defined');
}
} else {
delete handle[path.slice(-1)[0]]
}
}
}
const setObjectPath = (object, path, value, create = true) => {
editObjectPath('set', object, path, value, create);
}
const deleteObjectPath = (object, path, ignore = true) => {
editObjectPath('delete', object, path, undefined, ignore);
}
const getObjectPathEditor = (object) => {
return {
set (paths, value, create) {
if (typeof paths === 'object') {
for (let [path, value] of Object.entries(paths)) {
setObjectPath(object, path, value, create);
}
} else if (typeof paths === 'string') {
setObjectPath(object, paths, value, create);
}
return this;
},
delete (paths, ignore) {
if (Array.isArray(paths)) {
paths.forEach(path => {
deleteObjectPath(object, path, ignore)
})
} else if (typeof paths === 'string') {
deleteObjectPath(object, paths, ignore);
}
return this;
},
getObject () {
return object;
},
}
}
const camelToSnake = (str) => str.replace(/[A-Z]/g, pat => '_' + pat.toLowerCase());
const snakeToCamel = (str) => str.replace(/(_[a-z])/g, pat => pat.substr(1).toUpperCase());
const ucFirst = (str) => str.substr(0, 1).toUpperCase() + str.substr(1);
module.exports = { setObjectPath, camelToSnake, snakeToCamel, ucFirst, getObjectPathEditor }