Skip to content

Commit

Permalink
fix: to support sanitize for case-insensitive trailing duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
ksugawara61 committed Jan 4, 2024
1 parent 328712a commit d2c59b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 10 additions & 0 deletions code/lib/preview-api/src/modules/store/autoTitle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ describe('userOrAutoTitleFromSpecifier', () => {
).toMatchInlineSnapshot(`to/button`);
});

it('match with case-insensitive trailing duplicate', () => {
expect(
userOrAuto(
'./path/to/button/Button.stories.js',
normalizeStoriesEntry({ directory: './path' }, options),
undefined
)
).toMatchInlineSnapshot(`to/Button`);
});

it('match with trailing index', () => {
expect(
userOrAuto(
Expand Down
9 changes: 5 additions & 4 deletions code/lib/preview-api/src/modules/store/autoTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ const sanitize = (parts: string[]) => {
if (parts.length === 1) return [lastStripped];

const nextToLast = parts[parts.length - 2];
if (lastStripped && nextToLast && lastStripped.toLowerCase() === nextToLast.toLowerCase()) {
return [...parts.slice(0, -2), lastStripped];
}

return lastStripped &&
nextToLast &&
(lastStripped === nextToLast ||
/^(story|stories)([.][^.]+)$/i.test(last) ||
/^index$/i.test(lastStripped))
(/^(story|stories)([.][^.]+)$/i.test(last) || /^index$/i.test(lastStripped))
? parts.slice(0, -1)
: [...parts.slice(0, -1), lastStripped];
};
Expand Down

0 comments on commit d2c59b3

Please sign in to comment.