Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes name argument for 'toId' optional #17

Merged
merged 2 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { toId, storyNameFromExport, isExportStory } from '.';

describe('toId', () => {
[
const testCases: [string, string, string | undefined, string][] = [
// name, kind, story, output
['handles simple cases', 'kind', 'story', 'kind--story'],
['handles kind without story', 'kind', undefined, 'kind'],
['handles basic substitution', 'a b$c?d😀e', '1-2:3', 'a-b-c-d😀e--1-2-3'],
['handles runs of non-url chars', 'a?&*b', 'story', 'a-b--story'],
['removes non-url chars from start and end', '?ab-', 'story', 'ab--story'],
['downcases', 'KIND', 'STORY', 'kind--story'],
['non-latin', 'Кнопки', 'нормальный', 'кнопки--нормальный'],
['korean', 'kind', '바보 (babo)', 'kind--바보-babo'],
['all punctuation', 'kind', 'unicorns,’–—―′¿`"<>()!.!!!{}[]%^&$*#&', 'kind--unicorns'],
].forEach(([name, kind, story, output]) => {
];

testCases.forEach(([name, kind, story, output]) => {
it(name, () => {
expect(toId(kind, story)).toBe(output);
});
Expand All @@ -33,8 +36,8 @@ describe('toId', () => {
);
});

it('does not allow empty story', () => {
expect(() => toId('kind', '')).toThrow(`Invalid name '', must include alphanumeric characters`);
it('allows empty story', () => {
expect(() => toId('kind', '')).not.toThrow();
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const sanitizeSafe = (string: string, part: string) => {
/**
* Generate a storybook ID from a component/kind and story name.
*/
export const toId = (kind: string, name: string) =>
`${sanitizeSafe(kind, 'kind')}--${sanitizeSafe(name, 'name')}`;
export const toId = (kind: string, name?: string) =>
`${sanitizeSafe(kind, 'kind')}${name ? `--${sanitizeSafe(name, 'name')}` : ''}`;

/**
* Transform a CSF named export into a readable story name
Expand Down