-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.utility.ts
43 lines (39 loc) · 1 KB
/
object.utility.ts
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
/**
* static members for @type {Object}
*
* @export
*/
export class ObjectUtility {
/**
* mutates object properties to lowercase
*/
static lowerCasePropertyChar(
o: { [index: string]: any },
charIndex: number = 0
): { [index: string]: any } {
if (!o) {
return o;
}
const lowerCase = (s: string) => {
return `${s.charAt(charIndex).toLowerCase()}${s.slice(
charIndex + 1
)}`;
};
Object.keys(o).forEach(k => {
o = ObjectUtility.renameProperty(k, lowerCase(k), o);
});
return o;
}
/**
* renames a property of an object
*
* [https://medium.com/front-end-weekly/immutably-rename-object-keys-in-javascript-5f6353c7b6dd]
*/
static renameProperty(
oldProperty: string,
newProperty: string,
{ [oldProperty]: old, ...others }
): { [index: string]: any } {
return { [newProperty]: old, ...others };
}
}