From e92f8386749d30cea4575097192320512000deec Mon Sep 17 00:00:00 2001 From: Ashaun Thomas Date: Sat, 7 Jan 2023 18:18:09 -0800 Subject: [PATCH] adds leading dash flag feature and corresponding tests --- index.js | 12 +++++++++--- test.js | 10 ++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 36fe131..065005b 100644 --- a/index.js +++ b/index.js @@ -2,10 +2,16 @@ var KEBAB_REGEX = /[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g; var REVERSE_REGEX = /-[a-z\u00E0-\u00F6\u00F8-\u00FE]/g; -module.exports = exports = function kebabCase(str) { - return str.replace(KEBAB_REGEX, function (match) { - return '-' + match.toLowerCase(); +module.exports = exports = function kebabCase(str, leadingDashFlag) { + let kebab = str.replace(KEBAB_REGEX, function (match) { + return '-' + match }); + let firstChar = kebab.charAt(1); + return ( + leadingDashFlag === false && + firstChar.toUpperCase() === firstChar + ? kebab.slice(1).toLowerCase() : kebab.toLowerCase() + ); }; exports.reverse = function (str) { diff --git a/test.js b/test.js index 724f724..47dc08b 100644 --- a/test.js +++ b/test.js @@ -26,3 +26,13 @@ test("the reverse", (t) => { const str = "Hallå, Mr. Kebab Überstein! How you doin'?-"; t.equal(kebabCase.reverse(kebabCase(str)), str); }); + +test("string with leading dash", (t) => { + const str = "KebabCase"; + t.equal(kebabCase(str, false), "kebab-case"); +}) + +test("string reverse without leading dash", (t) => { + const str = "-kebab-case"; + t.equal(reverse(str), "KebabCase"); +}) \ No newline at end of file