From 632d12b306020856d116983349532b27cbd90859 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Sun, 24 Mar 2024 23:00:40 +0800 Subject: [PATCH 1/5] Add utility for tag combination & negation --- src/index.test.ts | 19 ++++++++++++++++++- src/index.ts | 14 ++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/index.test.ts b/src/index.test.ts index a21a41f..f714c45 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,4 +1,4 @@ -import { toId, storyNameFromExport, isExportStory } from './index.js'; +import { toId, storyNameFromExport, isExportStory, combineTags } from './index.js'; describe('toId', () => { const testCases: [string, string, string | undefined, string][] = [ @@ -93,3 +93,20 @@ describe('isExportStory', () => { expect(isExportStory('a', { includeStories: /a/, excludeStories: /b/ })).toBeTruthy(); }); }); + +describe('combineTags', () => { + it.each([ + [[], []], + [ + ['a', 'b'], + ['a', 'b'], + ], + [ + ['a', 'b', 'b'], + ['a', 'b'], + ], + [['a', 'b', '-b'], ['a']], + ])('combineTags(%o) -> %o', (tags, expected) => { + expect(combineTags(...tags)).toEqual(expected); + }); +}); diff --git a/src/index.ts b/src/index.ts index 5fd9d55..b0e08c9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -83,5 +83,19 @@ export const parseKind = (kind: string, { rootSeparator, groupSeparator }: Separ }; }; +/** + * Combine a set of project / meta / story tags, removing duplicates and handling negations. + */ +export const combineTags = (...tags: string[]): string[] => { + const set = new Set(tags); + return Array.from(set).reduce((acc, tag) => { + if (tag.startsWith('-')) return acc; + if (!set.has(`-${tag}`)) { + acc.push(tag); + } + return acc; + }, [] as string[]); +}; + export { includeConditionalArg } from './includeConditionalArg'; export * from './story'; From aedde7ffe2b31da3485d987056c875550cb2cf4c Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Tue, 26 Mar 2024 10:58:20 +0800 Subject: [PATCH 2/5] Change negation from `-` to `!` --- src/index.test.ts | 2 +- src/index.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index f714c45..1b7d5de 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -105,7 +105,7 @@ describe('combineTags', () => { ['a', 'b', 'b'], ['a', 'b'], ], - [['a', 'b', '-b'], ['a']], + [['a', 'b', '!b'], ['a']], ])('combineTags(%o) -> %o', (tags, expected) => { expect(combineTags(...tags)).toEqual(expected); }); diff --git a/src/index.ts b/src/index.ts index b0e08c9..c2d960e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -89,8 +89,8 @@ export const parseKind = (kind: string, { rootSeparator, groupSeparator }: Separ export const combineTags = (...tags: string[]): string[] => { const set = new Set(tags); return Array.from(set).reduce((acc, tag) => { - if (tag.startsWith('-')) return acc; - if (!set.has(`-${tag}`)) { + if (tag.startsWith('!')) return acc; + if (!set.has(`!${tag}`)) { acc.push(tag); } return acc; From f46975584070a78a43a28e6aa8dd75e7341cfd64 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 29 Mar 2024 17:59:25 +0800 Subject: [PATCH 3/5] Add project level tags --- src/story.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/story.ts b/src/story.ts index b03f46f..3f58f5e 100644 --- a/src/story.ts +++ b/src/story.ts @@ -349,6 +349,11 @@ export type BaseAnnotations * Define a custom render function for the story(ies). If not passed, a default render function by the renderer will be used. */ render?: ArgsStoryFn; + + /** + * Named tags for a story, used to filter stories in different contexts. + */ + tags?: Tag[]; }; export type ProjectAnnotations< @@ -457,11 +462,6 @@ export interface ComponentAnnotations; - - /** - * Named tags for a story, used to filter stories in different contexts. - */ - tags?: Tag[]; } export type StoryAnnotations< @@ -484,11 +484,6 @@ export type StoryAnnotations< */ play?: PlayFunction; - /** - * Named tags for a story, used to filter stories in different contexts. - */ - tags?: Tag[]; - /** @deprecated */ story?: Omit, 'story'>; // eslint-disable-next-line @typescript-eslint/ban-types From b6c0801c3b133404a6ac007d967ceca3ba6b4d33 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Wed, 3 Apr 2024 12:24:59 +0200 Subject: [PATCH 4/5] Fix the package name in the readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f1f77de..3907414 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ A minimal set of utility functions for dealing with [Component Story Format (CSF ### Install ```sh -yarn add @componentdriven/csf +yarn add @storybook/csf ``` ### API From ba37fd5b91d186c22d9a0cdd16dac3cddaa6aa81 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 5 Apr 2024 09:57:30 +0800 Subject: [PATCH 5/5] Fix combineTags to respect tag order --- src/index.test.ts | 1 + src/index.ts | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index 1b7d5de..dd2858a 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -106,6 +106,7 @@ describe('combineTags', () => { ['a', 'b'], ], [['a', 'b', '!b'], ['a']], + [['b', '!b', 'b'], ['b']], ])('combineTags(%o) -> %o', (tags, expected) => { expect(combineTags(...tags)).toEqual(expected); }); diff --git a/src/index.ts b/src/index.ts index c2d960e..a7bd966 100644 --- a/src/index.ts +++ b/src/index.ts @@ -87,14 +87,15 @@ export const parseKind = (kind: string, { rootSeparator, groupSeparator }: Separ * Combine a set of project / meta / story tags, removing duplicates and handling negations. */ export const combineTags = (...tags: string[]): string[] => { - const set = new Set(tags); - return Array.from(set).reduce((acc, tag) => { - if (tag.startsWith('!')) return acc; - if (!set.has(`!${tag}`)) { - acc.push(tag); + const result = tags.reduce((acc, tag) => { + if (tag.startsWith('!')) { + acc.delete(tag.slice(1)); + } else { + acc.add(tag); } return acc; - }, [] as string[]); + }, new Set()); + return Array.from(result); }; export { includeConditionalArg } from './includeConditionalArg';