From 8b58e893389dc8341965faa172d094533e702fef Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 15 Nov 2018 11:25:58 +0900 Subject: [PATCH 1/4] Create function: toCamelCase() --- lib/camel_case_keys.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/camel_case_keys.js b/lib/camel_case_keys.js index 6008b910..daca0ac1 100644 --- a/lib/camel_case_keys.js +++ b/lib/camel_case_keys.js @@ -16,6 +16,20 @@ function setter(key) { }; } +function toCamelCase(key) { + const match = key.match(rPrefixUnderscore); + let newKey; + + if (match) { + const underscore = match[1]; + newKey = underscore + camelCase(key.substring(underscore.length)); + } else { + newKey = camelCase(key); + } + + return newKey; +} + function camelCaseKeys(obj) { if (typeof obj !== 'object') throw new TypeError('obj must be an object!'); @@ -25,15 +39,7 @@ function camelCaseKeys(obj) { for (let i = 0, len = keys.length; i < len; i++) { const key = keys[i]; const value = obj[key]; - const match = key.match(rPrefixUnderscore); - let newKey; - - if (match) { - const underscore = match[1]; - newKey = underscore + camelCase(key.substring(underscore.length)); - } else { - newKey = camelCase(key); - } + const newKey = toCamelCase(key); if (newKey === key) { result[key] = value; From e9b14231dc648bd6c138ecc52987578f6d5043e7 Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 15 Nov 2018 11:28:08 +0900 Subject: [PATCH 2/4] Refactoring toCamelCase() --- lib/camel_case_keys.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/camel_case_keys.js b/lib/camel_case_keys.js index daca0ac1..8b2b6f14 100644 --- a/lib/camel_case_keys.js +++ b/lib/camel_case_keys.js @@ -16,18 +16,14 @@ function setter(key) { }; } -function toCamelCase(key) { - const match = key.match(rPrefixUnderscore); - let newKey; - - if (match) { - const underscore = match[1]; - newKey = underscore + camelCase(key.substring(underscore.length)); - } else { - newKey = camelCase(key); - } +function toCamelCase(str) { + const match = str.match(rPrefixUnderscore); - return newKey; + if (!match) { + return camelCase(str); + } + const underscore = match[1]; + return underscore + camelCase(str.substring(underscore.length)); } function camelCaseKeys(obj) { From 40270ac979e53e51bdd9dde3ad07b9666aa095c2 Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 15 Nov 2018 11:29:14 +0900 Subject: [PATCH 3/4] Remove rPrefixUnderscore --- lib/camel_case_keys.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/camel_case_keys.js b/lib/camel_case_keys.js index 8b2b6f14..557e4fe4 100644 --- a/lib/camel_case_keys.js +++ b/lib/camel_case_keys.js @@ -2,8 +2,6 @@ const camelCase = require('camel-case'); -const rPrefixUnderscore = /^(_+)/; - function getter(key) { return function() { return this[key]; @@ -17,13 +15,14 @@ function setter(key) { } function toCamelCase(str) { - const match = str.match(rPrefixUnderscore); + let prefixLength = -1; + + while (str[++prefixLength] === '_'); - if (!match) { + if (!prefixLength) { return camelCase(str); } - const underscore = match[1]; - return underscore + camelCase(str.substring(underscore.length)); + return str.substring(0, prefixLength) + camelCase(str.substring(prefixLength)); } function camelCaseKeys(obj) { From cbd6a18c3706e5833505ba1bad7840153cbe4e74 Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 15 Nov 2018 11:33:52 +0900 Subject: [PATCH 4/4] Refactoring camelCaseKeys() --- lib/camel_case_keys.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/camel_case_keys.js b/lib/camel_case_keys.js index 557e4fe4..55e04f3e 100644 --- a/lib/camel_case_keys.js +++ b/lib/camel_case_keys.js @@ -29,18 +29,17 @@ function camelCaseKeys(obj) { if (typeof obj !== 'object') throw new TypeError('obj must be an object!'); const keys = Object.keys(obj); + const { length } = keys; const result = {}; - for (let i = 0, len = keys.length; i < len; i++) { - const key = keys[i]; - const value = obj[key]; - const newKey = toCamelCase(key); + for (let i = 0; i < length; i++) { + const oldKey = keys[i]; + const newKey = toCamelCase(oldKey); - if (newKey === key) { - result[key] = value; - } else { - result[newKey] = value; - Object.defineProperty(result, key, { + result[newKey] = obj[oldKey]; + + if (newKey !== oldKey) { + Object.defineProperty(result, oldKey, { get: getter(newKey), set: setter(newKey), configurable: true,