From ed835ad6928f08e76ebc16fbf20169c168de8a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Recep=20Selim=20A=C4=9F=C4=B1rman?= Date: Sun, 19 Jul 2020 00:19:41 +0300 Subject: [PATCH 1/2] Added 'locale' option I have added optional 'locale' option to `options` --- index.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index dd2c490..739feef 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ 'use strict'; -const preserveCamelCase = string => { +const preserveCamelCase = (string, locale) => { let isLastCharLower = false; let isLastCharUpper = false; let isLastLastCharUpper = false; @@ -20,9 +20,9 @@ const preserveCamelCase = string => { isLastCharUpper = false; isLastCharLower = true; } else { - isLastCharLower = character.toLocaleLowerCase() === character && character.toLocaleUpperCase() !== character; + isLastCharLower = character.toLocaleLowerCase(locale) === character && character.toLocaleUpperCase(locale) !== character; isLastLastCharUpper = isLastCharUpper; - isLastCharUpper = character.toLocaleUpperCase() === character && character.toLocaleLowerCase() !== character; + isLastCharUpper = character.toLocaleUpperCase(locale) === character && character.toLocaleLowerCase(locale) !== character; } } @@ -35,11 +35,11 @@ const camelCase = (input, options) => { } options = { - ...{pascalCase: false}, + ...{pascalCase: false, locale: null}, ...options }; - - const postProcess = x => options.pascalCase ? x.charAt(0).toLocaleUpperCase() + x.slice(1) : x; + const locale = (typeof options.locale === 'string') ? options.locale : undefined; + const postProcess = x => options.pascalCase ? x.charAt(0).toLocaleUpperCase(locale) + x.slice(1) : x; if (Array.isArray(input)) { input = input.map(x => x.trim()) @@ -54,10 +54,10 @@ const camelCase = (input, options) => { } if (input.length === 1) { - return options.pascalCase ? input.toLocaleUpperCase() : input.toLocaleLowerCase(); + return options.pascalCase ? input.toLocaleUpperCase(locale) : input.toLocaleLowerCase(locale); } - const hasUpperCase = input !== input.toLocaleLowerCase(); + const hasUpperCase = input !== input.toLocaleLowerCase(locale); if (hasUpperCase) { input = preserveCamelCase(input); @@ -65,9 +65,9 @@ const camelCase = (input, options) => { input = input .replace(/^[_.\- ]+/, '') - .toLocaleLowerCase() - .replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toLocaleUpperCase()) - .replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toLocaleUpperCase()); + .toLocaleLowerCase(locale) + .replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toLocaleUpperCase(locale)) + .replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toLocaleUpperCase(locale)); return postProcess(input); }; From 94aa7f96e360ea6316bb6e3272136dcbea06f2ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Recep=20Selim=20A=C4=9F=C4=B1rman?= Date: Sun, 19 Jul 2020 00:20:14 +0300 Subject: [PATCH 2/2] Added a test to test 'locale' option Added a test to test 'locale' option --- test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test.js b/test.js index 4dea4be..d03a553 100644 --- a/test.js +++ b/test.js @@ -128,6 +128,8 @@ test('camelCase with pascalCase option', t => { t.is(camelCase('РОЗОВЫЙ_ПУШИСТЫЙ-ЕДИНОРОГИ', {pascalCase: true}), 'РозовыйПушистыйЕдинороги'); t.is(camelCase('桑德在这里。', {pascalCase: true}), '桑德在这里。'); t.is(camelCase('桑德_在这里。', {pascalCase: true}), '桑德在这里。'); + t.is(camelCase('run-in-band', {locale: 'tr-TR'}), 'runİnBand'); + t.is(camelCase('run-in-band', {locale: 'en-US'}), 'runInBand'); }); test('invalid input', t => {