From 064b6305f4963d85eca7a30655e544935d38a34f Mon Sep 17 00:00:00 2001 From: Jeff Ohrstrom Date: Thu, 17 Oct 2024 11:41:18 -0400 Subject: [PATCH] greatly simplify snakeCaseWords --- .../dashboard/app/javascript/dynamic_forms.js | 23 +++---------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/apps/dashboard/app/javascript/dynamic_forms.js b/apps/dashboard/app/javascript/dynamic_forms.js index bc4d4b5628..3a96160460 100644 --- a/apps/dashboard/app/javascript/dynamic_forms.js +++ b/apps/dashboard/app/javascript/dynamic_forms.js @@ -93,27 +93,10 @@ function mountainCaseWords(str) { function snakeCaseWords(str) { if(str === undefined) return undefined; - let snakeCase = ""; - - str.split('').forEach((c, index) => { - if(c === '-' || c === '_') { - snakeCase += '_'; - } else if (index == 0) { - snakeCase += c.toLowerCase(); - } else if(c == c.toUpperCase() && isNaN(c)) { - const nextIsUpper = (index + 1 !== str.length) ? str[index + 1] === str[index + 1].toUpperCase() : true; - const nextIsNum = !isNaN(str[index + 1]); - if ((str[index-1] === '_' || nextIsUpper) && !nextIsNum) { - snakeCase += c.toLowerCase(); - } else { - snakeCase += `_${c.toLowerCase()}`; - } - } else { - snakeCase += c; - } - }); + const rex = /([A-Z]{1}[a-z]*[0-9]*)/g; + const words = str.match(rex); - return snakeCase; + return words.map(word => word.toLowerCase()).join('_'); } /**