Skip to content

Commit

Permalink
Fix combineTags to respect tag order
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Apr 5, 2024
1 parent f469755 commit ba37fd5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(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<string>());
return Array.from(result);
};

export { includeConditionalArg } from './includeConditionalArg';
Expand Down

0 comments on commit ba37fd5

Please sign in to comment.