Skip to content

Commit

Permalink
performance improvements and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tlivings committed Apr 28, 2018
1 parent 93b7326 commit 11d13d9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -33,4 +33,5 @@ Thumbs.db
.AppleDouble
.LSOverride
.Spotlight-V100
.Trashes
.Trashes
package-lock.json
44 changes: 25 additions & 19 deletions index.js
Expand Up @@ -2,10 +2,6 @@

var thing = require('core-util-is');

function filterUndefined(key) {
return !!key;
}

exports = module.exports = {

/**
Expand All @@ -14,7 +10,7 @@ exports = module.exports = {
* @param mapFn map keys, return undefined to not include.
*/
camelize: function camelize(obj, mapFn) {
var newobj;
var newobj, keys, k;

if (!mapFn) {
mapFn = function (k) {
Expand All @@ -36,12 +32,16 @@ exports = module.exports = {
}
else if (thing.isObject(obj) && !Buffer.isBuffer(obj)) {
newobj = {};
keys = Object.keys(obj);

Object.keys(obj).map(mapFn).filter(filterUndefined).forEach(function (key) {
var newkey = exports.camelCase(key);

newobj[newkey] = exports.camelize(obj[key], mapFn);
});
for (var i = 0; i < keys.length; i++) {
k = mapFn(keys[i]);
if (!k) {
continue;
}
k = exports.camelCase(k);
newobj[k] = exports.camelize(obj[keys[i]], mapFn);
}
}
else {
newobj = obj;
Expand All @@ -56,7 +56,7 @@ exports = module.exports = {
* @param mapFn map keys, return undefined to not include.
*/
underscorify: function (obj, mapFn) {
var newobj;
var newobj, keys, k;

if (!mapFn) {
mapFn = function (k) {
Expand All @@ -78,12 +78,16 @@ exports = module.exports = {
}
else if (thing.isObject(obj) && !Buffer.isBuffer(obj)) {
newobj = {};
keys = Object.keys(obj);

Object.keys(obj).map(mapFn).filter(filterUndefined).forEach(function (key) {
var newkey = exports.underscore(key);

newobj[newkey] = exports.underscorify(obj[key], mapFn);
});
for (var i = 0; i < keys.length; i++) {
k = mapFn(keys[i]);
if (!k) {
continue;
}
k = exports.underscore(k);
newobj[k] = exports.underscorify(obj[keys[i]], mapFn);
}
}
else {
newobj = obj;
Expand All @@ -98,7 +102,7 @@ exports = module.exports = {
* @returns {string|*}
*/
camelCase: function camelCase(str) {
return str.replace(/([a-z])_(\w)/g, function (g) {
return str.replace(/([aA-zZ])_(\w)/g, function (g) {
return g[0] + g[2].toUpperCase();
});
},
Expand All @@ -109,8 +113,10 @@ exports = module.exports = {
* @returns {string|*}
*/
underscore: function underscore(str) {
return str.replace(/([a-z])([A-Z]+)/g, function (g) {
return g[0] + '_' + g[1].toLowerCase();
return str.replace(/([a-z])([A-Z0-9]+)/g, function (g) {

This comment has been minimized.

Copy link
@real-ashwin

real-ashwin May 4, 2018

@tlivings If this change to include numbers [0-9] is intentional, then I think this should be a major upgrade.
It will change keys like key1 to key_1 where as previously it was doing key1.

return g.split('').map(function (k) {
return k.toLowerCase();
}).join('_');
});
}
};
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "camelscore",
"description": "camelCase and under_score utilities for objects.",
"version": "0.1.3",
"version": "0.1.4",
"author": "Trevor Livingston <tlivings@gmail.com>",
"repository": {
"type": "git",
Expand Down
13 changes: 11 additions & 2 deletions test/test-camelscore.js
Expand Up @@ -8,14 +8,18 @@ test('test', function (t) {
t.test('camelCase', function (assert) {
var str = camelscore.camelCase('foo_bar');

assert.plan(4);
assert.plan(5);

assert.strictEqual(str, 'fooBar', 'camel cased 1 underscore.');

str = camelscore.camelCase('foo_bar_baz');

assert.strictEqual(str, 'fooBarBaz', 'camel cased 2 underscores.');

str = camelscore.camelCase('is_2FA_login');

assert.strictEqual(str, 'is2FALogin', 'camel cased 2 underscores with a caps.');

str = camelscore.camelCase('_foo_bar_baz');

assert.strictEqual(str, '_fooBarBaz', 'camel cased and left leading _.');
Expand All @@ -28,14 +32,19 @@ test('test', function (t) {
t.test('underscore', function (assert) {
var str = camelscore.underscore('fooBar');

assert.plan(4);
assert.plan(5);

assert.strictEqual(str, 'foo_bar', 'underscore cased with 1 hump.');

str = camelscore.underscore('fooBarBaz');

assert.strictEqual(str, 'foo_bar_baz', 'underscore cased with 2 humps.');

str = camelscore.underscore('is2FALogin');

//This sucks but is reality
assert.strictEqual(str, 'is_2_f_a_login', 'underscore cased with 2 humps and caps.');

str = camelscore.underscore('_fooBaredBaz');

assert.strictEqual(str, '_foo_bared_baz', 'underscore cased and left leading _.');
Expand Down

0 comments on commit 11d13d9

Please sign in to comment.