-
-
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
Add easy-to-use options to storySort #9188
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import storySort from './storySort'; | ||
|
||
describe('preview.storySort', () => { | ||
const fixture = { | ||
a: ['', { kind: 'a' }], | ||
á: ['', { kind: 'á' }], | ||
A: ['', { kind: 'A' }], | ||
b: ['', { kind: 'b' }], | ||
a_a: ['', { kind: 'a/a' }], | ||
a_b: ['', { kind: 'a/b' }], | ||
b_a_a: ['', { kind: 'b/a/a' }], | ||
b_b: ['', { kind: 'b/b' }], | ||
locale1: ['', { kind: 'Б' }], | ||
locale2: ['', { kind: 'Г' }], | ||
}; | ||
|
||
it('uses configure order by default', () => { | ||
const sortFn = storySort(); | ||
|
||
expect(sortFn(fixture.a, fixture.b)).toBe(0); | ||
expect(sortFn(fixture.b, fixture.a)).toBe(0); | ||
expect(sortFn(fixture.a, fixture.a)).toBe(0); | ||
}); | ||
|
||
it('can sort shallow kinds alphabetically', () => { | ||
const sortFn = storySort({ method: 'alphabetical' }); | ||
|
||
expect(sortFn(fixture.a, fixture.b)).toBeLessThan(0); | ||
expect(sortFn(fixture.b, fixture.a)).toBeGreaterThan(0); | ||
expect(sortFn(fixture.a, fixture.á)).toBeLessThan(0); | ||
expect(sortFn(fixture.á, fixture.a)).toBeGreaterThan(0); | ||
}); | ||
|
||
it('can sort deep kinds alphabetically', () => { | ||
const sortFn = storySort({ method: 'alphabetical' }); | ||
|
||
expect(sortFn(fixture.a_a, fixture.a_b)).toBeLessThan(0); | ||
expect(sortFn(fixture.a_b, fixture.a_a)).toBeGreaterThan(0); | ||
expect(sortFn(fixture.a_a, fixture.b)).toBeLessThan(0); | ||
expect(sortFn(fixture.b, fixture.a_a)).toBeGreaterThan(0); | ||
expect(sortFn(fixture.a_a, fixture.a)).toBeGreaterThan(0); | ||
expect(sortFn(fixture.a, fixture.a_a)).toBeLessThan(0); | ||
expect(sortFn(fixture.b_a_a, fixture.b_b)).toBeLessThan(0); | ||
expect(sortFn(fixture.b_b, fixture.b_a_a)).toBeGreaterThan(0); | ||
}); | ||
|
||
it('ignores case when sorting alphabetically', () => { | ||
const sortFn = storySort({ method: 'alphabetical' }); | ||
|
||
expect(sortFn(fixture.a, fixture.A)).toBe(0); | ||
expect(sortFn(fixture.A, fixture.a)).toBe(0); | ||
}); | ||
|
||
it('sorts alphabetically using the given locales', () => { | ||
const sortFn = storySort({ method: 'alphabetical', locales: 'ru-RU' }); | ||
|
||
expect(sortFn(fixture.locale1, fixture.locale2)).toBeLessThan(0); | ||
expect(sortFn(fixture.locale2, fixture.locale1)).toBeGreaterThan(0); | ||
}); | ||
|
||
it('sorts according to the order array', () => { | ||
const sortFn = storySort({ order: ['b', 'c'] }); | ||
|
||
expect(sortFn(fixture.a, fixture.b)).toBeGreaterThan(0); | ||
expect(sortFn(fixture.b, fixture.a)).toBeLessThan(0); | ||
expect(sortFn(fixture.b_a_a, fixture.b_b)).toBe(0); | ||
expect(sortFn(fixture.b_b, fixture.b_a_a)).toBe(0); | ||
}); | ||
|
||
it('sorts according to the nested order array', () => { | ||
const sortFn = storySort({ order: ['a', ['b', 'c'], 'c'] }); | ||
|
||
expect(sortFn(fixture.a_a, fixture.a_b)).toBeGreaterThan(0); | ||
expect(sortFn(fixture.a_b, fixture.a_a)).toBeLessThan(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { StorySortObjectParameter, Comparator } from '@storybook/addons'; | ||
|
||
const storySort = (options: StorySortObjectParameter = {}): Comparator<any> => ( | ||
a: any, | ||
b: any | ||
): number => { | ||
// If the two stories have the same story kind, then use the default | ||
// ordering, which is the order they are defined in the story file. | ||
if (a[1].kind === b[1].kind) { | ||
return 0; | ||
} | ||
|
||
// Get the StorySortParameter options. | ||
const method = options.method || 'configure'; | ||
let order = options.order || []; | ||
|
||
// Examine each part of the story kind in turn. | ||
const storyKindA = a[1].kind.split('/'); | ||
const storyKindB = b[1].kind.split('/'); | ||
let depth = 0; | ||
while (storyKindA[depth] || storyKindB[depth]) { | ||
// Stories with a shorter depth should go first. | ||
if (!storyKindA[depth]) { | ||
return -1; | ||
} | ||
if (!storyKindB[depth]) { | ||
return 1; | ||
} | ||
|
||
// Compare the next part of the story kind. | ||
const nameA = storyKindA[depth]; | ||
const nameB = storyKindB[depth]; | ||
if (nameA !== nameB) { | ||
// Look for the names in the given `order` array. | ||
let indexA = order.indexOf(nameA); | ||
let indexB = order.indexOf(nameB); | ||
|
||
// If at least one of the names is found, sort by the `order` array. | ||
if (indexA !== -1 || indexB !== -1) { | ||
// If one of the names is not found in `order`, list it last. | ||
if (indexA === -1) { | ||
indexA = order.length; | ||
} | ||
if (indexB === -1) { | ||
indexB = order.length; | ||
} | ||
|
||
return indexA - indexB; | ||
} | ||
|
||
// Use the default configure() order. | ||
if (method === 'configure') { | ||
return 0; | ||
} | ||
|
||
// Otherwise, use alphabetical order. | ||
return nameA.localeCompare(nameB, options.locales ? options.locales : undefined, { | ||
numeric: true, | ||
sensitivity: 'accent', | ||
}); | ||
} | ||
|
||
// If a nested array is provided for a name, use it for ordering. | ||
const index = order.indexOf(nameA); | ||
order = index !== -1 && Array.isArray(order[index + 1]) ? order[index + 1] : []; | ||
|
||
// We'll need to look at the next part of the name. | ||
depth += 1; | ||
} | ||
|
||
// Identical story kinds. The shortcut at the start of this function prevents | ||
// this from ever being used. | ||
/* istanbul ignore next */ | ||
return 0; | ||
}; | ||
|
||
export default storySort; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I see the PR merged in
next-6.0.0
, but not seeing the changes reflected there. I wanted to point out thesort
key here is wrong. ThestorySort
method in this PR usesorder
to override the order.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.
Cc @ndelangen
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.
Yeah, this doc seems to be outdated. There's
order
in tests.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.
@hasparus would you be able to make a PR fixing the docs for this?
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.
yup, no biggie