Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion core/extras/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,33 @@ 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", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a KebabCaseConverter that uses an external dependency ( https://www.npmjs.com/package/just-kebab-case ) that is now at 4.2.0 vs
"just-kebab-case": "^4.0.1",
in mod's package.json.

The code us here:
https://github.com/angus-c/just/blob/master/packages/string-kebab-case/index.cjs

which seems a bit more complete than what you have here. I've known of Angus Croll the author for a while, it might take sense to keep those kind of low level dependencies.

In term of pattern, the method from this package is effectively a converter in mod's terms. So it would make sense to limit the dependencies for String to use Mod's KebabCaseConverter as the core logic, and if for some reason we decide to inline the code and remove the dependency, the the String's toKebabCase() method wouldn't need to change

value: function toKebabCase() {
if (!this) return "";
if (kebabCaseCache[this]) return kebabCaseCache[this];

kebabCaseCache[this] = this.trim()
.split(/[_\s-]+|(?<=[a-z])(?=[A-Z])/)
.map((word) => word.toLowerCase())
.join("-");

return kebabCaseCache[this];
},
writable: true,
configurable: true,
});
}

if(typeof String.prototype.removeSuffix !== "function") {
Object.defineProperty(String.prototype, "removeSuffix", {
value: function (suffix) {
Expand Down Expand Up @@ -290,7 +317,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)
Expand Down