diff --git a/.gitignore b/.gitignore index 944e2d9..1333c2d 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,5 @@ Thumbs.db .AppleDouble .LSOverride .Spotlight-V100 -.Trashes \ No newline at end of file +.Trashes +package-lock.json diff --git a/index.js b/index.js index d266af6..08992ab 100644 --- a/index.js +++ b/index.js @@ -2,10 +2,6 @@ var thing = require('core-util-is'); -function filterUndefined(key) { - return !!key; -} - exports = module.exports = { /** @@ -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) { @@ -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; @@ -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) { @@ -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; @@ -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(); }); }, @@ -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) { + return g.split('').map(function (k) { + return k.toLowerCase(); + }).join('_'); }); } }; diff --git a/package.json b/package.json index 625ba49..33370a5 100644 --- a/package.json +++ b/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 ", "repository": { "type": "git", diff --git a/test/test-camelscore.js b/test/test-camelscore.js index 5fbc7ea..337ce82 100644 --- a/test/test-camelscore.js +++ b/test/test-camelscore.js @@ -8,7 +8,7 @@ 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.'); @@ -16,6 +16,10 @@ test('test', function (t) { 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 _.'); @@ -28,7 +32,7 @@ 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.'); @@ -36,6 +40,11 @@ test('test', function (t) { 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 _.');