« get me outta code hell

util: add much more custom behavior to getKebabCase - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-03-14 06:38:35 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-03-14 06:46:48 -0300
commit316814c80848e5b7ea1e8772d14d3f5c23cdb94f (patch)
treefc8eda957a1839213e9711b605b0c9af16cdacfb
parenta26a913841bba73f44182bb5e1725f91573395a9 (diff)
util: add much more custom behavior to getKebabCase
-rw-r--r--src/util/wiki-data.js37
1 files changed, 35 insertions, 2 deletions
diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js
index 1066874..f8ab3ef 100644
--- a/src/util/wiki-data.js
+++ b/src/util/wiki-data.js
@@ -13,12 +13,45 @@ export {filterMultipleArrays} from './sugar.js';
 
 export function getKebabCase(name) {
   return name
+
+    // Spaces to dashes
     .split(' ')
     .join('-')
-    .replace(/&/g, 'and')
-    .replace(/[^a-zA-Z0-9-]/g, '')
+
+    // Punctuation as words
+    .replace(/&/g, '-and-')
+    .replace(/\+/g, '-plus-')
+    .replace(/%/g, '-percent-')
+
+    // Punctuation which only divides words, not single characters
+    .replace(/(\b[^\s-.]{2,})\./g, '$1-')
+    .replace(/\.([^\s-.]{2,})\b/g, '-$1')
+
+    // Punctuation which doesn't divide a number following a non-number
+    .replace(/(?<=[0-9])\^/g, '-')
+    .replace(/\^(?![0-9])/g, '-')
+
+    // General punctuation which always separates surrounding words
+    .replace(/[/@#$%*()_=,[\]{}|\\;:<>?`~]/g, '-')
+
+    // Accented characters
+    .replace(/[áâäàå]/gi, 'a')
+    .replace(/[çč]/gi, 'c')
+    .replace(/[éêëè]/gi, 'e')
+    .replace(/[íîïì]/gi, 'i')
+    .replace(/[óôöò]/gi, 'o')
+    .replace(/[úûüù]/gi, 'u')
+
+    // Strip other characters
+    .replace(/[^a-z0-9-]/gi, '')
+
+    // Combine consecutive dashes
     .replace(/-{2,}/g, '-')
+
+    // Trim dashes on boundaries
     .replace(/^-+|-+$/g, '')
+
+    // Always lowercase
     .toLowerCase();
 }