From 536032852cc8fe0dd1ac479cea70fc9682149cb5 Mon Sep 17 00:00:00 2001 From: Nam Hoang Le Date: Sun, 14 Nov 2021 13:52:13 +0700 Subject: [PATCH 1/2] Move regexp to outer scope --- index.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index cdb5511..785080f 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,15 @@ 'use strict'; +const UPPER_CASE = /[\p{Lu}]/u; +const LOWER_CASE = /[\p{Ll}]/u; +const LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/gu; +const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u; +const SEPARATORS = /[_.\- ]+/; + +const LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source); +const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu'); +const NUMBERS_AND_IDENTIFIER = new RegExp('\\d+' + IDENTIFIER.source, 'gu'); + const preserveCamelCase = (string, locale) => { let isLastCharLower = false; let isLastCharUpper = false; @@ -8,13 +18,13 @@ const preserveCamelCase = (string, locale) => { for (let i = 0; i < string.length; i++) { const character = string[i]; - if (isLastCharLower && /[\p{Lu}]/u.test(character)) { + if (isLastCharLower && UPPER_CASE.test(character)) { string = string.slice(0, i) + '-' + string.slice(i); isLastCharLower = false; isLastLastCharUpper = isLastCharUpper; isLastCharUpper = true; i++; - } else if (isLastCharUpper && isLastLastCharUpper && /[\p{Ll}]/u.test(character)) { + } else if (isLastCharUpper && isLastLastCharUpper && LOWER_CASE.test(character)) { string = string.slice(0, i - 1) + '-' + string.slice(i - 1); isLastLastCharUpper = isLastCharUpper; isLastCharUpper = false; @@ -30,12 +40,12 @@ const preserveCamelCase = (string, locale) => { }; const preserveConsecutiveUppercase = input => { - return input.replace(/^[\p{Lu}](?![\p{Lu}])/gu, m1 => m1.toLowerCase()); + return input.replace(LEADING_CAPITAL, m1 => m1.toLowerCase()); }; const postProcess = (input, options) => { - return input.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toLocaleUpperCase(options.locale)) - .replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toLocaleUpperCase(options.locale)); + return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => identifier.toLocaleUpperCase(options.locale)) + .replace(NUMBERS_AND_IDENTIFIER, m => m.toLocaleUpperCase(options.locale)); }; const camelCase = (input, options) => { @@ -71,7 +81,7 @@ const camelCase = (input, options) => { input = preserveCamelCase(input, options.locale); } - input = input.replace(/^[_.\- ]+/, ''); + input = input.replace(LEADING_SEPARATORS, ''); if (options.preserveConsecutiveUppercase) { input = preserveConsecutiveUppercase(input); From c1baa3a94a77d37ecc8b8f49a4dbf9c993df31a5 Mon Sep 17 00:00:00 2001 From: Nam Hoang Le Date: Mon, 15 Nov 2021 09:37:43 +0700 Subject: [PATCH 2/2] Rename and reset lastIndex for global regexp --- index.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 785080f..b501478 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ 'use strict'; -const UPPER_CASE = /[\p{Lu}]/u; -const LOWER_CASE = /[\p{Ll}]/u; +const UPPERCASE = /[\p{Lu}]/u; +const LOWERCASE = /[\p{Ll}]/u; const LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/gu; const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u; const SEPARATORS = /[_.\- ]+/; @@ -18,13 +18,13 @@ const preserveCamelCase = (string, locale) => { for (let i = 0; i < string.length; i++) { const character = string[i]; - if (isLastCharLower && UPPER_CASE.test(character)) { + if (isLastCharLower && UPPERCASE.test(character)) { string = string.slice(0, i) + '-' + string.slice(i); isLastCharLower = false; isLastLastCharUpper = isLastCharUpper; isLastCharUpper = true; i++; - } else if (isLastCharUpper && isLastLastCharUpper && LOWER_CASE.test(character)) { + } else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character)) { string = string.slice(0, i - 1) + '-' + string.slice(i - 1); isLastLastCharUpper = isLastCharUpper; isLastCharUpper = false; @@ -40,10 +40,15 @@ const preserveCamelCase = (string, locale) => { }; const preserveConsecutiveUppercase = input => { + LEADING_CAPITAL.lastIndex = 0; + return input.replace(LEADING_CAPITAL, m1 => m1.toLowerCase()); }; const postProcess = (input, options) => { + SEPARATORS_AND_IDENTIFIER.lastIndex = 0; + NUMBERS_AND_IDENTIFIER.lastIndex = 0; + return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => identifier.toLocaleUpperCase(options.locale)) .replace(NUMBERS_AND_IDENTIFIER, m => m.toLocaleUpperCase(options.locale)); };