Skip to content

Commit

Permalink
Fix kebab casing of story names to convert MapSDK into map-sdk and no…
Browse files Browse the repository at this point in the history
…t map-s-d-k (#193)

* Improve the kebab casing of story names to convert MapSDK into map-sdk and not map-s-d-k

* Inline capitals chars
  • Loading branch information
tajo committed Jul 18, 2022
1 parent 3209be6 commit e573adc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-crabs-raise.md
@@ -0,0 +1,5 @@
---
"@ladle/react": patch
---

Improve the kebab casing of story names to convert MapSDK into map-sdk and not map-s-d-k.
15 changes: 9 additions & 6 deletions packages/ladle/lib/cli/vite-plugin/naming-utils.js
Expand Up @@ -4,7 +4,6 @@ export const storyEncodeDelimiter = "$";
// BUT preserving delimiters --
const wordSeparators =
/[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,.\/:;<=>?@\[\]^_`{|}~]+/;
const capitals = /[A-Z\u00C0-\u00D6\u00D9-\u00DD]/g;

/**
* @param {string} str
Expand All @@ -31,11 +30,15 @@ export const storyIdToMeta = (str) => {
* @param {string} str
*/
export const kebabCase = (str) => {
//replace capitals with space + lower case equivalent for later parsing
str = str.replace(capitals, function (match) {
return " " + (match.toLowerCase() || match);
});
return str.trim().split(wordSeparators).join("-");
return str
.replace(
/[A-Z\u00C0-\u00D6\u00D9-\u00DD]+(?![a-z])|[A-Z\u00C0-\u00D6\u00D9-\u00DD]/g,
($, ofs) => (ofs ? "-" : "") + $.toLowerCase(),
)
.replace(/\s-/g, "-")
.trim()
.split(wordSeparators)
.join("-");
};

/**
Expand Down
1 change: 0 additions & 1 deletion packages/ladle/tests/fn.ts

This file was deleted.

12 changes: 12 additions & 0 deletions packages/ladle/tests/naming-utils.test.ts
@@ -0,0 +1,12 @@
import { kebabCase } from "../lib/cli/vite-plugin/naming-utils.js";

test("kebabCase", () => {
expect(kebabCase("ChampsÉlysées")).toBe("champs-élysées");
expect(kebabCase("our-animals--mammals")).toBe("our-animals--mammals");
expect(kebabCase("the quick brown fox")).toBe("the-quick-brown-fox");
expect(kebabCase("the-quick-brown-fox")).toBe("the-quick-brown-fox");
expect(kebabCase("the_quick_brown_fox")).toBe("the-quick-brown-fox");
expect(kebabCase("theQuickBrownFox")).toBe("the-quick-brown-fox");
expect(kebabCase("thequickbrownfox")).toBe("thequickbrownfox");
expect(kebabCase("theQUICKBrownFox")).toBe("the-quick-brown-fox");
});
5 changes: 0 additions & 5 deletions packages/ladle/tests/some.test.ts

This file was deleted.

1 comment on commit e573adc

@vercel
Copy link

@vercel vercel bot commented on e573adc Jul 18, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

ladle – ./

ladle.vercel.app
ladle-git-master-miksu.vercel.app
ladle-miksu.vercel.app
ladle.dev

Please sign in to comment.