Permalink
Browse files

[fix] change toCamel/toUnderscore functions back to their original ve…

…rsions.
  • Loading branch information...
1 parent 202ec6d commit d27405f5c46d9448b94dacf51e10d099703b5968 @chjj chjj committed Aug 1, 2012
Showing with 23 additions and 19 deletions.
  1. +23 −19 lib/index.js
View
@@ -377,22 +377,23 @@ utile.clone = function (object, filter) {
// specified `obj`.
//
utile.camelToUnderscore = function (obj) {
+ if (typeof obj !== 'object' || obj === null) {
+ return obj;
+ }
+
if (Array.isArray(obj)) {
- return obj.forEach(utile.camelToUnderscore);
+ obj.forEach(utile.camelToUnderscore);
+ return obj;
}
- Object.keys(obj || {}).forEach(function (key) {
- var k = utile.inflect.underscore(key);
- if (k != key) {
+ Object.keys(obj).forEach(function (key) {
+ if (/[a-z][A-Z]/.test(key)) {
+ var k = key.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
obj[k] = obj[key];
delete obj[key];
key = k;
}
-
- if (obj[key] && !Array.isArray(obj[key])
- && typeof obj[key] === 'object') {
- utile.camelToUnderscore(obj[key]);
- }
+ utile.camelToUnderscore(obj[key]);
});
return obj;
@@ -405,23 +406,26 @@ utile.camelToUnderscore = function (obj) {
// specified `obj`.
//
utile.underscoreToCamel = function (obj) {
+ if (typeof obj !== 'object' || obj === null) {
+ return obj;
+ }
+
if (Array.isArray(obj)) {
- return obj.forEach(utile.underscoreToCamel);
+ obj.forEach(utile.underscoreToCamel);
+ return obj;
}
- Object.keys(obj || {}).forEach(function (key) {
- var k = utile.inflect.camelize(key, false);
- if (k != key) {
+ Object.keys(obj).forEach(function (key) {
+ if (/[a-z]_[a-z]/.test(key)) {
+ var k = key.replace(/([a-z])_([a-z])/g, function (_, $1, $2) {
+ return $1 + $2.toUpperCase();
+ });
obj[k] = obj[key];
delete obj[key];
key = k;
}
-
- if (obj[key] && !Array.isArray(obj[key])
- && typeof obj[key] === 'object') {
- utile.underscoreToCamel(obj[key]);
- }
+ utile.underscoreToCamel(obj[key]);
});
return obj;
-}
+};

0 comments on commit d27405f

Please sign in to comment.