1 | const STORY_KIND_PATH_SEPARATOR = /\s*\/\s*/;
|
2 | export const storySort = (options = {}) => (a, b) => {
|
3 |
|
4 |
|
5 |
|
6 | if (a[1].kind === b[1].kind && !options.includeNames) {
|
7 | return 0;
|
8 | }
|
9 |
|
10 |
|
11 | const method = options.method || 'configure';
|
12 | let order = options.order || [];
|
13 |
|
14 | const storyKindA = a[1].kind.trim().split(STORY_KIND_PATH_SEPARATOR);
|
15 | const storyKindB = b[1].kind.trim().split(STORY_KIND_PATH_SEPARATOR);
|
16 |
|
17 | if (options.includeNames) {
|
18 | storyKindA.push(a[1].name);
|
19 | storyKindB.push(b[1].name);
|
20 | }
|
21 |
|
22 | let depth = 0;
|
23 |
|
24 | while (storyKindA[depth] || storyKindB[depth]) {
|
25 |
|
26 | if (!storyKindA[depth]) {
|
27 | return -1;
|
28 | }
|
29 |
|
30 | if (!storyKindB[depth]) {
|
31 | return 1;
|
32 | }
|
33 |
|
34 |
|
35 | const nameA = storyKindA[depth];
|
36 | const nameB = storyKindB[depth];
|
37 |
|
38 | if (nameA !== nameB) {
|
39 |
|
40 | let indexA = order.indexOf(nameA);
|
41 | let indexB = order.indexOf(nameB);
|
42 | const indexWildcard = order.indexOf('*');
|
43 |
|
44 | if (indexA !== -1 || indexB !== -1) {
|
45 |
|
46 |
|
47 | if (indexA === -1) {
|
48 | if (indexWildcard !== -1) {
|
49 | indexA = indexWildcard;
|
50 | } else {
|
51 | indexA = order.length;
|
52 | }
|
53 | }
|
54 |
|
55 | if (indexB === -1) {
|
56 | if (indexWildcard !== -1) {
|
57 | indexB = indexWildcard;
|
58 | } else {
|
59 | indexB = order.length;
|
60 | }
|
61 | }
|
62 |
|
63 | return indexA - indexB;
|
64 | }
|
65 |
|
66 |
|
67 | if (method === 'configure') {
|
68 | return 0;
|
69 | }
|
70 |
|
71 |
|
72 | return nameA.localeCompare(nameB, options.locales ? options.locales : undefined, {
|
73 | numeric: true,
|
74 | sensitivity: 'accent'
|
75 | });
|
76 | }
|
77 |
|
78 |
|
79 | const index = order.indexOf(nameA);
|
80 | order = index !== -1 && Array.isArray(order[index + 1]) ? order[index + 1] : [];
|
81 |
|
82 | depth += 1;
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | return 0;
|
90 | }; |
\ | No newline at end of file |