Skip to content

Commit

Permalink
Extract RegExp to variable for reuse and trim the kind before splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed May 26, 2021
1 parent 0a4b5be commit 8c20356
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion addons/docs/src/blocks/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import { DocsContext, DocsContextProps } from './DocsContext';
interface TitleProps {
children?: JSX.Element | string;
}

const STORY_KIND_PATH_SEPARATOR = /\s*\/\s*/;

export const extractTitle = ({ kind }: DocsContextProps) => {
const groups = kind.split(/\s*\/\s*/);
const groups = kind.trim().split(STORY_KIND_PATH_SEPARATOR);
return (groups && groups[groups.length - 1]) || kind;
};

Expand Down
4 changes: 3 additions & 1 deletion lib/api/src/lib/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ export const denormalizeStoryParameters = ({
}));
};

const STORY_KIND_PATH_SEPARATOR = /\s*\/\s*/;

export const transformStoriesRawToStoriesHash = (
input: StoriesRaw,
{ provider }: { provider: Provider }
Expand All @@ -168,7 +170,7 @@ export const transformStoriesRawToStoriesHash = (
warnChangedDefaultHierarchySeparators();
}

const groups = kind.split(/\s*\/\s*/);
const groups = kind.trim().split(STORY_KIND_PATH_SEPARATOR);
const root = (!setShowRoots || showRoots) && groups.length > 1 ? [groups.shift()] : [];

const rootAndGroups = [...root, ...groups].reduce((list, name, index) => {
Expand Down
11 changes: 9 additions & 2 deletions lib/client-api/src/storySort.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { StorySortObjectParameter, StorySortComparator } from '@storybook/addons';

const STORY_KIND_PATH_SEPARATOR = /\s*\/\s*/;

export const storySort = (options: StorySortObjectParameter = {}): StorySortComparator => (
a: any,
b: any
Expand All @@ -16,8 +18,13 @@ export const storySort = (options: StorySortObjectParameter = {}): StorySortComp
let order = options.order || [];

// Examine each part of the story kind in turn.
const storyKindA = [...a[1].kind.split(/\s*\/\s*/), ...(options.includeNames ? a[1].name : [])];
const storyKindB = [...b[1].kind.split(/\s*\/\s*/), ...(options.includeNames ? b[1].name : [])];
const storyKindA = a[1].kind.trim().split(STORY_KIND_PATH_SEPARATOR);
const storyKindB = b[1].kind.trim().split(STORY_KIND_PATH_SEPARATOR);
if (options.includeNames) {
storyKindA.push(a[1].name);
storyKindB.push(b[1].name);
}

let depth = 0;
while (storyKindA[depth] || storyKindB[depth]) {
// Stories with a shorter depth should go first.
Expand Down

0 comments on commit 8c20356

Please sign in to comment.