@getsolara/solara.js
Version:
A lightweight and modular Discord bot framework built on discord.js v14, with truly optional feature packages.
33 lines (27 loc) • 1.1 kB
JavaScript
module.exports = {
name: "$toKebabCase",
description: "Converts a string from various cases (e.g., PascalCase, camelCase, snake_case, space-separated) to kebab-case. Example: 'Hello World_and-HTML' becomes 'hello-world-and-html'.",
takesBrackets: true,
execute: async (context, args) => {
const inputString = args[0];
if (inputString === undefined || inputString === null) {
return "[Error: $toKebabCase requires a string argument.]";
}
const s = String(inputString);
if (s.trim() === "") {
return "";
}
const words = s.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g);
if (!words) {
let processed = s
.replace(/[\s_]+/g, '-')
.replace(/[^a-zA-Z0-9-]+/g, '')
.replace(/-+/g, '-')
.replace(/^-+|-+$/g, '');
return processed.toLowerCase();
}
return words
.map(word => word.toLowerCase())
.join('-');
}
};