-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Fix infinite loop with special characters in kind names #6607
Changes from 7 commits
e90f70a
9141b8e
66052bc
2750e85
212362a
1c49d65
e6fb6fd
dabc161
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,13 @@ interface StoryData { | |
storyId?: string; | ||
} | ||
|
||
interface SeparatorOptions { | ||
rootSeparator: string | RegExp; | ||
groupSeparator: string | RegExp; | ||
} | ||
|
||
export const knownNonViewModesRegex = /(settings)/; | ||
const splitPath = /\/([^/]+)\/([^/]+)?/; | ||
const splitPathRegex = /\/([^/]+)\/([^/]+)?/; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we change this to something else given it has nothing to do with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tmeasday Renamed the functions instead. LMK what you think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WFM 👍 |
||
|
||
// Remove punctuation https://gist.github.com/davidjrice/9d2af51100e41c6c4b4a | ||
export const sanitize = (string: string) => { | ||
|
@@ -30,15 +35,15 @@ const sanitizeSafe = (string: string, part: string) => { | |
export const toId = (kind: string, name: string) => | ||
`${sanitizeSafe(kind, 'kind')}--${sanitizeSafe(name, 'name')}`; | ||
|
||
export const storyDataFromString: (path?: string) => StoryData = memoize(1000)( | ||
export const parsePath: (path?: string) => StoryData = memoize(1000)( | ||
(path: string | undefined | null) => { | ||
const result: StoryData = { | ||
viewMode: undefined, | ||
storyId: undefined, | ||
}; | ||
|
||
if (path) { | ||
const [, viewMode, storyId] = path.match(splitPath) || [undefined, undefined, undefined]; | ||
const [, viewMode, storyId] = path.match(splitPathRegex) || [undefined, undefined, undefined]; | ||
if (viewMode && !viewMode.match(knownNonViewModesRegex)) { | ||
Object.assign(result, { | ||
viewMode, | ||
|
@@ -73,3 +78,14 @@ export const getMatch = memoize(1000)( | |
return null; | ||
} | ||
); | ||
|
||
export const parseKind = (kind: string, { rootSeparator, groupSeparator }: SeparatorOptions) => { | ||
const [root, remainder] = kind.split(rootSeparator, 2); | ||
const groups = (remainder || kind).split(groupSeparator).filter(i => !!i); | ||
|
||
// when there's no remainder, it means the root wasn't found/split | ||
return { | ||
root: remainder ? root : null, | ||
groups, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shilman you can do:
import { toId, sanitize, parseKind } from '@storybook/router';
because
index.ts
of@storybook/router
is exporting everything from/utils
folder: