-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rewrite as ESM and improve the TypeScript declaration file
And a new `keepLeadingDash` option has been added to the kebabCase function. When set to `false` it will remove any leading dash from the kebab-cased string, in case the original string starts with a leading uppercase letter. Fixes #7, Closes #12. BREAKING CHANGE: kebab-case is rewritten in ESM and is not published as CommonJS anymore. If you still need CJS you should stick to the v1 version.
- Loading branch information
1 parent
66b27a7
commit f572ed3
Showing
11 changed files
with
877 additions
and
1,009 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import globals from "globals"; | ||
import pluginJs from "@eslint/js"; | ||
|
||
export default [ | ||
{languageOptions: { globals: {...globals.browser, ...globals.node} }}, | ||
pluginJs.configs.recommended, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,26 @@ | ||
type Fn = (str: string) => string; | ||
|
||
interface KebabCase extends Fn { | ||
reverse: Fn | ||
} | ||
|
||
const kebabCase: KebabCase; | ||
|
||
export default kebabCase; | ||
/** | ||
* Transforms a string into kebab-case. | ||
* | ||
* @example | ||
* kebabCase("helloWorld"); // "hello-world" | ||
* kebabCase("HelloWorld"); // "-hello-world" | ||
* kebabCase("HelloWorld", false); // "hello-world" | ||
* | ||
* @param {string} str The string to transform | ||
* @param {boolean} keepLeadingDash Whether to keep the leading dash in case the string starts with an uppercase letter (default: true) | ||
* @returns The kebab-cased string | ||
*/ | ||
declare function kebabCase(str: string, keepLeadingDash?: boolean): string | undefined; | ||
declare namespace kebabCase { | ||
/** | ||
* Transforms a kebab-cased string back to the original string. | ||
* | ||
* @example | ||
* kebabCase.reverse("hello-world"); // "helloWorld" | ||
* | ||
* @param {string} str | ||
* @returns The original string, with the kebab-case transformation reversed | ||
*/ | ||
function reverse(str: string): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,39 @@ | ||
'use strict'; | ||
var KEBAB_REGEX = /[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g; | ||
var REVERSE_REGEX = /-[a-z\u00E0-\u00F6\u00F8-\u00FE]/g; | ||
|
||
module.exports = exports = function kebabCase(str) { | ||
return str.replace(KEBAB_REGEX, function (match) { | ||
return '-' + match.toLowerCase(); | ||
}); | ||
}; | ||
const KEBAB_REGEX = /\p{Lu}/gu; | ||
const REVERSE_REGEX = /-\p{Ll}/gu; | ||
|
||
/** | ||
* Transforms a string into kebab-case. | ||
* | ||
* @example | ||
* kebabCase("helloWorld"); // "hello-world" | ||
* kebabCase("HelloWorld"); // "-hello-world" | ||
* kebabCase("HelloWorld", false); // "hello-world" | ||
* | ||
* @param {string} str The string to transform | ||
* @param {boolean} keepLeadingDash Whether to keep the leading dash in case the string starts with an uppercase letter (default: true) | ||
* @returns The kebab-cased string | ||
*/ | ||
const kebabCase = (str, keepLeadingDash = true) => { | ||
const result = str.replace(KEBAB_REGEX, (match) => `-${match.toLowerCase()}`); | ||
|
||
if (keepLeadingDash) { | ||
return result; | ||
} | ||
|
||
exports.reverse = function (str) { | ||
return str.replace(REVERSE_REGEX, function (match) { | ||
return match.slice(1).toUpperCase(); | ||
}); | ||
if (result.startsWith("-")) { | ||
return result.slice(1); | ||
} | ||
}; | ||
|
||
/** | ||
* Transforms a kebab-cased string back to the original string. | ||
* | ||
* @example | ||
* kebabCase.reverse("hello-world"); // "helloWorld" | ||
* | ||
* @param {string} str | ||
* @returns The original string, with the kebab-case transformation reversed | ||
*/ | ||
kebabCase.reverse = (str) => str.replace(REVERSE_REGEX, (match) => match.slice(1).toUpperCase()); | ||
|
||
export default kebabCase; |
Oops, something went wrong.