From 95d33ec82fc5f7f48e4dd36092a2371a25c9a133 Mon Sep 17 00:00:00 2001 From: Thibault Zanini Date: Thu, 14 Aug 2025 16:35:22 +0200 Subject: [PATCH 1/2] feat: added toKebabCase method on String prototype --- core/extras/string.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/core/extras/string.js b/core/extras/string.js index ebd42ba65..cd64f39fb 100644 --- a/core/extras/string.js +++ b/core/extras/string.js @@ -149,6 +149,38 @@ if (!String.prototype.toCamelCase) { String.prototype.toLowerCamelCase.cache = Object.create(null); } +if (typeof String.prototype.toKebabCase !== "function") { + const kebabCaseCache = Object.create(null); + + /** + * Converts a string to kebab-case. + * @function external:String#toKebabCase + * @returns {string} The string converted to kebab-case. + * @example + * "Hello World".toKebabCase(); // "hello-world" + */ + Object.defineProperty(String.prototype, "toKebabCase", { + value: function toKebabCase() { + if (!this) return ""; + + if (kebabCaseCache.hasOwnProperty(this)) { + return kebabCaseCache[this]; + } + + const transformed = this.trim() + .split(/[_\s-]+|(?<=[a-z])(?=[A-Z])/) + .map((word) => word.toLowerCase()) + .join("-"); + + kebabCaseCache[this] = transformed; + + return transformed; + }, + writable: true, + configurable: true, + }); +} + if(typeof String.prototype.removeSuffix !== "function") { Object.defineProperty(String.prototype, "removeSuffix", { value: function (suffix) { @@ -290,7 +322,7 @@ if(typeof String.prototype.substringWithinRange !== "function") { value: function substringWithinRange (aRange) { /* substring: indexStart: The index of the first character to include in the returned substring. - + indexEnd: The index of the first character to exclude from the returned substring. */ return this.substring(aRange.includesBegin ? aRange.begin : (aRange.begin + 1), aRange.includesEnd ? (aRange.end + 1) : aRange.end) From 16451dec6ba34e8fc9a8ab6c57a9d1f6abf3676a Mon Sep 17 00:00:00 2001 From: Thibault Zanini Date: Fri, 15 Aug 2025 14:59:29 +0200 Subject: [PATCH 2/2] feat: minor improvements --- core/extras/string.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/core/extras/string.js b/core/extras/string.js index cd64f39fb..6861f45f1 100644 --- a/core/extras/string.js +++ b/core/extras/string.js @@ -162,19 +162,14 @@ if (typeof String.prototype.toKebabCase !== "function") { Object.defineProperty(String.prototype, "toKebabCase", { value: function toKebabCase() { if (!this) return ""; + if (kebabCaseCache[this]) return kebabCaseCache[this]; - if (kebabCaseCache.hasOwnProperty(this)) { - return kebabCaseCache[this]; - } - - const transformed = this.trim() + kebabCaseCache[this] = this.trim() .split(/[_\s-]+|(?<=[a-z])(?=[A-Z])/) .map((word) => word.toLowerCase()) .join("-"); - kebabCaseCache[this] = transformed; - - return transformed; + return kebabCaseCache[this]; }, writable: true, configurable: true,