forked from documentationjs/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.js
168 lines (151 loc) · 3.75 KB
/
sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import sort from '../../src/sort.js';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const apples = { context: { sortKey: 'a' }, name: 'apples' };
const carrot = { context: { sortKey: 'b' }, name: 'carrot' };
const bananas = { context: { sortKey: 'c' }, name: 'bananas' };
test('sort stream alphanumeric', function () {
expect(sort([apples, carrot, bananas])).toEqual([apples, carrot, bananas]);
expect(sort([carrot, apples, bananas])).toEqual([apples, carrot, bananas]);
});
test('sort stream with configuration', function () {
expect(
sort([apples, carrot, bananas], {
toc: ['carrot', 'bananas']
})
).toEqual([carrot, bananas, apples]);
});
test('sort stream with configuration and a section', function () {
const section = {
name: 'This is the banana type',
description: 'here lies bananas'
};
const sectionMarkdown = {
name: 'This is the banana type',
description: {
type: 'root',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'here lies bananas'
}
]
}
]
},
kind: 'note',
path: [
{
name: 'This is the banana type',
scope: 'static'
}
]
};
expect(
sort([apples, carrot, bananas], {
toc: ['carrot', section, 'bananas']
})
).toEqual([carrot, sectionMarkdown, bananas, apples]);
});
test('sort an already-sorted stream containing a section/description', function () {
// this happens in the 'serve' task
const section = {
name: 'This is the banana type',
description: 'here lies bananas'
};
const sectionMarkdown = {
name: 'This is the banana type',
description: {
type: 'root',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'here lies bananas'
}
]
}
]
},
kind: 'note',
path: [
{
name: 'This is the banana type',
scope: 'static'
}
]
};
const config = {
toc: ['carrot', section, 'bananas']
};
const sortOnce = sort([apples, carrot, bananas], config);
const sortTwice = sort(sortOnce, config);
expect(sortTwice).toEqual([carrot, sectionMarkdown, bananas, apples]);
});
test('sort toc with files', function () {
const snowflake = {
name: 'snowflake',
file: path.join(__dirname, '../fixture/snowflake.md')
};
expect(
sort([apples, carrot, bananas], {
toc: [snowflake]
})
).toMatchSnapshot();
});
test('sort toc with files absolute path', function () {
const snowflake = {
name: 'snowflake',
file: path.join(__dirname, '../fixture/snowflake.md')
};
expect(
sort([apples, carrot, bananas], {
toc: [snowflake]
})
).toMatchSnapshot();
});
test('sort toc with files absolute path', function () {
const apples = {
context: { sortKey: 'a' },
name: 'apples',
kind: 'function',
memberof: 'classB'
};
const carrot = {
context: { sortKey: 'b' },
name: 'carrot',
memberof: 'classB'
};
const bananas = {
context: { sortKey: 'c' },
name: 'bananas',
kind: 'function',
memberof: 'classA'
};
const snowflake = {
name: 'snowflake',
file: path.join(__dirname, '../fixture/snowflake.md')
};
expect(
sort([carrot, apples, bananas], {
sortOrder: ['alpha']
})
).toMatchSnapshot();
expect(
sort([carrot, apples, bananas], {
sortOrder: ['kind', 'alpha']
})
).toMatchSnapshot();
expect(
sort([carrot, apples, bananas], {
sortOrder: ['memberof', 'kind', 'alpha']
})
).toMatchSnapshot();
});