-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (81 loc) · 2.54 KB
/
index.js
File metadata and controls
94 lines (81 loc) · 2.54 KB
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
function shouldProcessValue(value) {
return value && typeof value == 'object' &&
!(value instanceof Date) && !(value instanceof Function);
}
function processKeys(obj, fun, opts) {
let obj2;
if(obj instanceof Array) {
obj2 = [];
} else {
if(typeof obj.prototype !== 'undefined') {
// return non-plain object unchanged
return obj;
}
obj2 = {};
}
for(let key in obj) {
let value = obj[key];
if(typeof key === 'string')
key = fun(key, opts && opts.separator);
if(shouldProcessValue(value)) {
obj2[key] = processKeys(value, fun, opts);
} else {
obj2[key] = value;
}
}
return obj2;
}
function processKeysInPlace(obj, fun, opts) {
let keys = Object.keys(obj);
for(let idx = 0;idx < keys.length;++idx) {
let key = keys[idx];
let value = obj[key];
let newKey = fun(key, opts && opts.separator);
if(newKey !== key) {
delete obj[key];
}
if(shouldProcessValue(value)) {
obj[newKey] = processKeys(value, fun, opts);
} else {
obj[newKey] = value;
}
}
return obj;
}
import * as algorithms from './algorithms';
export function camelize(str, separator) {
return algorithms.camelize(str, (separator && separator.charCodeAt(0)) || 0x5f /* _ */);
}
export function decamelize(str, separator) {
return algorithms.decamelize(str, (separator && separator.charCodeAt(0)) || 0x5f /* _ */);
}
export function pascalize(str, separator) {
return algorithms.pascalize(str, (separator && separator.charCodeAt(0)) || 0x5f /* _ */);
}
export function depascalize(str, separator) {
return algorithms.depascalize(str, (separator && separator.charCodeAt(0)) || 0x5f /* _ */);
}
export function camelizeKeys(obj, opts) {
opts = opts || {};
if(!shouldProcessValue(obj)) return obj;
if(opts.inPlace) return processKeysInPlace(obj, camelize, opts);
return processKeys(obj, camelize, opts);
}
export function decamelizeKeys(obj, opts) {
opts = opts || {};
if(!shouldProcessValue(obj)) return obj;
if(opts.inPlace) return processKeysInPlace(obj, decamelize, opts);
return processKeys(obj, decamelize, opts);
}
export function pascalizeKeys(obj, opts) {
opts = opts || {};
if(!shouldProcessValue(obj)) return obj;
if(opts.inPlace) return processKeysInPlace(obj, pascalize, opts);
return processKeys(obj, pascalize, opts);
}
export function depascalizeKeys(obj, opts) {
opts = opts || {};
if(!shouldProcessValue(obj)) return obj;
if(opts.inPlace) return processKeysInPlace(obj, depascalize, opts);
return processKeys(obj, depascalize, opts);
}