Skip to content

Commit aa3496a

Browse files
anthony-redFoxtmcw
authored andcommitted
fix: alphabetical sort and add flow notation to sort file (#861)
* Removed sort by memberof, when the memberOf is the same of items then sort function doesn't works correctly. Added Unit Tests Fixed #838 * Flow.js : added flow notation for sort.js
1 parent 4911ba6 commit aa3496a

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

__tests__/lib/__snapshots__/sort.js.snap

+26
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,29 @@ Array [
107107
},
108108
]
109109
`;
110+
111+
exports[`sort toc with files absolute path 2`] = `
112+
Array [
113+
Object {
114+
"context": Object {
115+
"sortKey": "a",
116+
},
117+
"memberof": "classB",
118+
"name": "apples",
119+
},
120+
Object {
121+
"context": Object {
122+
"sortKey": "c",
123+
},
124+
"memberof": "classB",
125+
"name": "bananas",
126+
},
127+
Object {
128+
"context": Object {
129+
"sortKey": "b",
130+
},
131+
"memberof": "classB",
132+
"name": "carrot",
133+
},
134+
]
135+
`;

__tests__/lib/sort.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var sort = require('../../src/sort'), path = require('path');
1+
var sort = require('../../src/sort'),
2+
path = require('path');
23

34
test('sort stream alphanumeric', function() {
45
var apples = { context: { sortKey: 'a' }, name: 'apples' };
@@ -204,3 +205,31 @@ test('sort toc with files absolute path', function() {
204205
})
205206
).toMatchSnapshot();
206207
});
208+
209+
test('sort toc with files absolute path', function() {
210+
var apples = {
211+
context: { sortKey: 'a' },
212+
name: 'apples',
213+
memberof: 'classB'
214+
};
215+
var carrot = {
216+
context: { sortKey: 'b' },
217+
name: 'carrot',
218+
memberof: 'classB'
219+
};
220+
var bananas = {
221+
context: { sortKey: 'c' },
222+
name: 'bananas',
223+
memberof: 'classB'
224+
};
225+
226+
var snowflake = {
227+
name: 'snowflake',
228+
file: path.join(__dirname, '../fixture/snowflake.md')
229+
};
230+
expect(
231+
sort([carrot, apples, bananas], {
232+
sortOrder: 'alpha'
233+
})
234+
).toMatchSnapshot();
235+
});

src/sort.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,21 @@ module.exports = function sortDocs(comments: Array<Comment>, options: Object) {
9494
return fixed.concat(unfixed);
9595
};
9696

97-
function compare(a: string, b: string) {
98-
return a.localeCompare(b, undefined, { caseFirst: 'upper' });
99-
}
100-
101-
function compareCommentsByName(a, b) {
102-
var akey = a.memberof || a.name;
103-
var bkey = b.memberof || b.name;
97+
function compareCommentsByName(a: Comment, b: Comment): number {
98+
const akey: ?string = a.name;
99+
const bkey: ?string = b.name;
104100

105101
if (akey && bkey) {
106-
return compare(akey, bkey);
102+
return akey.localeCompare(bkey, undefined, { caseFirst: 'upper' });
107103
}
108104
return 0;
109105
}
110106

111-
function compareCommentsBySourceLocation(a, b) {
107+
function compareCommentsBySourceLocation(a: Comment, b: Comment): number {
112108
return a.context.sortKey.localeCompare(b.context.sortKey);
113109
}
114110

115-
function sortComments(comments, sortOrder) {
111+
function sortComments(comments: Array<Comment>, sortOrder: string) {
116112
return comments.sort(
117113
sortOrder === 'alpha'
118114
? compareCommentsByName

0 commit comments

Comments
 (0)