Comparing version 0.6.0 to 0.7.0
{ | ||
"name": "humps", | ||
"main": "humps.js", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"homepage": "https://github.com/domchristie/humps", | ||
@@ -6,0 +6,0 @@ "authors": [ |
34
humps.js
// ========= | ||
// = humps = | ||
// ========= | ||
// version 0.6.0 | ||
// version 0.7.0 | ||
// Underscore-to-camelCase converter (and vice versa) | ||
// for strings and object keys | ||
// humps is copyright © 2014 Dom Christie | ||
// humps is copyright © 2012+ Dom Christie | ||
// Released under the MIT license. | ||
@@ -14,4 +14,4 @@ | ||
var _processKeys = function(convert, obj, separator) { | ||
if(!_isObject(obj) || _isDate(obj) || _isRegExp(obj)) { | ||
var _processKeys = function(convert, obj, separator, ignoreNumbers) { | ||
if(!_isObject(obj) || _isDate(obj) || _isRegExp(obj) || _isBoolean(obj)) { | ||
return obj; | ||
@@ -27,3 +27,3 @@ } | ||
for(l=obj.length; i<l; i++) { | ||
output.push(_processKeys(convert, obj[i], separator)); | ||
output.push(_processKeys(convert, obj[i], separator, ignoreNumbers)); | ||
} | ||
@@ -35,3 +35,3 @@ } | ||
if(obj.hasOwnProperty(key)) { | ||
output[convert(key, separator)] = _processKeys(convert, obj[key], separator); | ||
output[convert(key, separator, ignoreNumbers)] = _processKeys(convert, obj[key], separator, ignoreNumbers); | ||
} | ||
@@ -45,7 +45,14 @@ } | ||
var separateWords = function(string, separator) { | ||
var separateWords = function(string, separator, ignoreNumbers) { | ||
if (typeof separator === 'undefined') { | ||
separator = '_'; | ||
} | ||
return string.replace(/([a-z])([A-Z0-9])/g, '$1'+ separator +'$2'); | ||
var regexp = /([a-z])([A-Z0-9])/g; | ||
if (ignoreNumbers) { | ||
regexp = /([a-z])([A-Z])/g; | ||
} | ||
return string.replace(regexp, '$1'+ separator +'$2'); | ||
}; | ||
@@ -70,4 +77,4 @@ | ||
var decamelize = function(string, separator) { | ||
return separateWords(string, separator).toLowerCase(); | ||
var decamelize = function(string, separator, ignoreNumbers) { | ||
return separateWords(string, separator, ignoreNumbers).toLowerCase(); | ||
}; | ||
@@ -92,2 +99,5 @@ | ||
}; | ||
var _isBoolean = function(obj) { | ||
return toString.call(obj) == '[object Boolean]'; | ||
}; | ||
@@ -108,4 +118,4 @@ // Performant way to determine if obj coerces to a number | ||
}, | ||
decamelizeKeys: function(object, separator) { | ||
return _processKeys(decamelize, object, separator); | ||
decamelizeKeys: function(object, separator, ignoreNumbers) { | ||
return _processKeys(decamelize, object, separator, ignoreNumbers); | ||
}, | ||
@@ -112,0 +122,0 @@ pascalizeKeys: function(object) { |
{ | ||
"name": "humps", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Underscore-to-camelCase converter (and vice versa) for strings and object keys in JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "humps.js", |
@@ -22,2 +22,9 @@ humps [![Build status](https://secure.travis-ci.org/domchristie/humps.png)](http://travis-ci.org/#!/domchristie/humps) | ||
### Decamelizing strings and ignoring numbers | ||
humps.decamelize(string, separator, ignoreNumbers); | ||
humps.decamelize("helloWorld1", null, true); // "hello_world1" | ||
Note: ignoring numbers will be the default in v1. | ||
### Converting object keys | ||
@@ -49,2 +56,2 @@ | ||
------- | ||
humps is copyright © 2014 [Dom Christie](http://domchristie.co.uk) and released under the MIT license. | ||
humps is copyright © 2012+ [Dom Christie](http://domchristie.co.uk) and released under the MIT license. |
@@ -78,2 +78,20 @@ describe('humps', function() { | ||
this.complexIgnoringNumbersObj = { | ||
attr_one: 'foo', | ||
attr_two: { | ||
nested_attr1: 'bar' | ||
}, | ||
attr_three: { | ||
nested_attr2: { | ||
nested_attr3: [{ | ||
nested_in_array1: 'baz' | ||
}, { | ||
nested_in_array2: 'hello' | ||
}, { | ||
nested_in_array3: ['world', 'boo'] | ||
}] | ||
} | ||
} | ||
}; | ||
this.complexCustomObj = { | ||
@@ -145,2 +163,6 @@ 'attr-one': 'foo', | ||
}); | ||
it('decamelizes keys ignoring numbers', function() { | ||
expect(humps.decamelizeKeys(this.complexCamelObj, '_', true)).toEqual(this.complexIgnoringNumbersObj); | ||
}); | ||
}); | ||
@@ -182,2 +204,6 @@ | ||
}); | ||
it('depascalizes keys ignoring numbers', function() { | ||
expect(humps.depascalizeKeys(this.complexPascalObj, '_', true)).toEqual(this.complexIgnoringNumbersObj); | ||
}); | ||
}); | ||
@@ -218,2 +244,6 @@ | ||
}); | ||
it('decamelizes strings ignoring numbers', function() { | ||
expect(humps.decamelize('helloWorld1', '_', true)).toEqual('hello_world1'); | ||
}); | ||
}); | ||
@@ -234,2 +264,2 @@ | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16622
357
55