Skip to content

Commit

Permalink
Merge pull request #80 from ComponentDriven/shilman/add-combine-tags
Browse files Browse the repository at this point in the history
Add utility for tag combination & negation
  • Loading branch information
shilman authored May 6, 2024
2 parents 6b0f6fa + ba37fd5 commit 7b98e13
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
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 @@ -377,6 +377,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 @@ -485,11 +490,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 @@ -512,11 +512,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

0 comments on commit 7b98e13

Please sign in to comment.