From 61882cb56bf11a4152170a8d6464688395a17e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mati=20K=C3=A4rner?= Date: Thu, 15 Oct 2020 16:49:35 +0300 Subject: [PATCH 1/2] Use 'toId' without story --- src/index.test.ts | 7 +++++-- src/index.ts | 6 ++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index 27d0095..b5b4aa4 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,9 +1,10 @@ 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'], @@ -11,7 +12,9 @@ describe('toId', () => { ['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); }); diff --git a/src/index.ts b/src/index.ts index 136583a..73fb39f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,8 +28,10 @@ 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')}${ + typeof name === 'string' ? `--${sanitizeSafe(name, 'name')}` : '' + }`; /** * Transform a CSF named export into a readable story name From eb0e9b4dfe25ba62fc0d7eb3399222b735f962a0 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Fri, 1 Oct 2021 14:07:21 +0200 Subject: [PATCH 2/2] Allow empty string for story --- src/index.test.ts | 4 ++-- src/index.ts | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index b5b4aa4..1874d5b 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -36,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(); }); }); diff --git a/src/index.ts b/src/index.ts index 73fb39f..e9c1b90 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,9 +29,7 @@ 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')}${ - typeof name === 'string' ? `--${sanitizeSafe(name, 'name')}` : '' - }`; + `${sanitizeSafe(kind, 'kind')}${name ? `--${sanitizeSafe(name, 'name')}` : ''}`; /** * Transform a CSF named export into a readable story name