Skip to content

Commit

Permalink
greatly simplify snakeCaseWords
Browse files Browse the repository at this point in the history
  • Loading branch information
johrstrom committed Oct 17, 2024
1 parent c17b872 commit 064b630
Showing 1 changed file with 3 additions and 20 deletions.
23 changes: 3 additions & 20 deletions apps/dashboard/app/javascript/dynamic_forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('_');
}

/**
Expand Down

0 comments on commit 064b630

Please sign in to comment.