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

Add utility for tag combination & negation #80

Merged
merged 4 commits into from
May 6, 2024
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
20 changes: 19 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -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][] = [
Expand Down Expand Up @@ -93,3 +93,21 @@ 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']],
[['b', '!b', 'b'], ['b']],
])('combineTags(%o) -> %o', (tags, expected) => {
expect(combineTags(...tags)).toEqual(expected);
});
});
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,20 @@ 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 result = tags.reduce((acc, tag) => {
if (tag.startsWith('!')) {
acc.delete(tag.slice(1));
} else {
acc.add(tag);
}
return acc;
}, new Set<string>());
return Array.from(result);
};

export { includeConditionalArg } from './includeConditionalArg';
export * from './story';
15 changes: 5 additions & 10 deletions src/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ export type BaseAnnotations<TRenderer extends Renderer = Renderer, TArgs = Args>
* Define a custom render function for the story(ies). If not passed, a default render function by the renderer will be used.
*/
render?: ArgsStoryFn<TRenderer, TArgs>;

/**
* Named tags for a story, used to filter stories in different contexts.
*/
tags?: Tag[];
};

export type ProjectAnnotations<
Expand Down Expand Up @@ -457,11 +462,6 @@ export interface ComponentAnnotations<TRenderer extends Renderer = Renderer, TAr
* Function that is executed after the story is rendered.
*/
play?: PlayFunction<TRenderer, TArgs>;

/**
* Named tags for a story, used to filter stories in different contexts.
*/
tags?: Tag[];
}

export type StoryAnnotations<
Expand All @@ -484,11 +484,6 @@ export type StoryAnnotations<
*/
play?: PlayFunction<TRenderer, TArgs>;

/**
* Named tags for a story, used to filter stories in different contexts.
*/
tags?: Tag[];

/** @deprecated */
story?: Omit<StoryAnnotations<TRenderer, TArgs>, 'story'>;
// eslint-disable-next-line @typescript-eslint/ban-types
Expand Down
Loading