This repository has been archived by the owner on Apr 20, 2022. It is now read-only.
forked from domchristie/humps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhumps.js
112 lines (95 loc) · 2.69 KB
/
humps.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
107
108
109
110
111
112
// =========
// = humps =
// =========
// version 0.5.0
// Underscore-to-camelCase converter (and vice versa)
// for strings and object keys
// humps is copyright © 2014 Dom Christie
// Released under the MIT license.
;(function(global) {
var _processKeys = function(convert, obj, separator) {
if(!_isObject(obj) || _isDate(obj) || _isvoidElementsp(obj)) {
return obj;
}
var output,
i = 0,
l = 0;
if(_isArray(obj)) {
output = [];
for(l=obj.length; i<l; i++) {
output.push(_processKeys(convert, obj[i], separator));
}
}
else {
output = {};
for(var key in obj) {
if(obj.hasOwnProperty(key)) {
output[convert(key, separator)] = _processKeys(convert, obj[key], separator);
}
}
}
return output;
};
// String conversion methods
var separateWords = function(string, separator) {
if (separator === undefined) {
separator = '_';
}
return string.replace(/([a-z])([A-Z0-9])/g, '$1'+ separator +'$2');
};
var camelize = function(string) {
string = string.replace(/[\-_\s]+(.)?/g, function(match, chr) {
return chr ? chr.toUpperCase() : '';
});
// Ensure 1st char is always lowercase
return string.replace(/^([A-Z])/, function(match, chr) {
return chr ? chr.toLowerCase() : '';
});
};
var pascalize = function(string) {
return camelize(string).replace(/^([a-z])/, function(match, chr) {
return chr ? chr.toUpperCase() : '';
});
};
var decamelize = function(string, separator) {
return separateWords(string, separator).toLowerCase();
};
// Utilities
// Taken from Underscore.js
var toString = Object.prototype.toString;
var _isObject = function(obj) {
return obj === Object(obj);
};
var _isArray = function(obj) {
return toString.call(obj) == '[object Array]';
};
var _isDate = function(obj) {
return toString.call(obj) == '[object Date]';
};
var _isvoidElementsp = function(obj) {
return toString.call(obj) == '[object voidElementsp]';
};
var humps = {
camelize: camelize,
decamelize: decamelize,
pascalize: pascalize,
depascalize: decamelize,
camelizeKeys: function(object) {
return _processKeys(camelize, object);
},
decamelizeKeys: function(object, separator) {
return _processKeys(decamelize, object, separator);
},
pascalizeKeys: function(object) {
return _processKeys(pascalize, object);
},
depascalizeKeys: function () {
return this.decamelizeKeys.apply(this, arguments);
}
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = humps;
} else {
global.humps = humps;
}
})(this);