-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.js
106 lines (101 loc) · 4 KB
/
objects.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
104
105
106
const ALWAYS_CAPS_REGEX = /id|url|uri|http|https|ftp/i;
const Objects = {
/**
* Converts an object's keys from snake_case to camelCase recursively. Accepts an array of objects or single
* object.
* @param {Array|Object} obj - The object to convert keys to camelCase.
* @returns {Array|Object}
*/
toCamelCase: function (obj) {
if (Array.isArray(obj)) {
return obj.map(v => Objects.toCamelCase(v));
} else if (obj !== null && obj.constructor === Object) {
return Object.keys(obj).reduce((result, key) => {
const camelCaseKey = key.replace(/_([^_]+)/g, (_, word) => {
if (word.length) {
if (ALWAYS_CAPS_REGEX.test(word)) {
return word.toUpperCase();
}
return word.substring(0, 1).toUpperCase() + word.substring(1);
}
});
result[camelCaseKey] = Objects.toCamelCase(obj[key]);
return result;
}, {});
}
return obj;
},
/**
* Converts an object's keys from camelCase to snake_case recursively. Accepts an array of objects or single
* object.
* @param {Array|Object} obj - The object to convert keys to snake_case.
* @returns {Array|Object}
*/
toSnakeCase: function (obj) {
if (Array.isArray(obj)) {
return obj.map(v => Objects.toSnakeCase(v));
} else if (obj !== null && obj.constructor === Object) {
return Object.keys(obj).reduce((result, key) => {
const snakeCaseKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
result[snakeCaseKey] = Objects.toSnakeCase(obj[key]);
return result;
}, {});
}
return obj;
},
/**
* Returns true if the object is a plain object (literal object).
* @param {*} obj - The object to check.
* @returns {Boolean}
*/
isLiteral: function (obj) {
return !!(
obj &&
Object.prototype.toString.call(obj) === '[object Object]' && (obj.constructor === Object ||
Object.getPrototypeOf(obj) === null)
);
},
/**
* Flattens a recursive object.
* @param {Object} obj - The object to flatten.
* @param {{ separator:String, excise:Array.<String> }} [options] - The options for the flattening operation.
* @param {String} [prefix=''] - The prefix for the keys.
* @param {Object} [result={}] - The result object.
* @returns {Object} - The flattened object.
*/
flatten: function (obj, options, prefix = '', result) {
if (obj === null || typeof obj !== 'object') {
if (typeof result !== 'undefined') {
return result;
}
return obj;
}
if (!options) {
options = { separator: '.' };
}
result = result || {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const value = obj[key];
if (options?.excise && options.excise.includes(key)) {
continue; //skip excised keys
}
let newKey = prefix ? `${prefix}${options?.separator ?? '.'}${key}` : key;
if (options.separator === '' || options.separator === null) { //no separator, make camelCase-like
if (ALWAYS_CAPS_REGEX.test(key)) {
newKey = prefix ? `${prefix}${key.toUpperCase()}` : key;
} else {
newKey = prefix ? `${prefix}${key.substring(0, 1).toUpperCase() + key.substring(1)}` : key;
}
}
if (Objects.isLiteral(value)) {
Objects.flatten(value, options, newKey, result);
} else {
result[newKey] = value;
}
}
}
return result;
}
};
export default Objects;