Skip to content

Commit

Permalink
fix: alphabetical sort and add flow notation to sort file (#861)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
anthony-redFox authored and tmcw committed Aug 8, 2017
1 parent 4911ba6 commit aa3496a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
26 changes: 26 additions & 0 deletions __tests__/lib/__snapshots__/sort.js.snap
Expand Up @@ -107,3 +107,29 @@ Array [
},
]
`;

exports[`sort toc with files absolute path 2`] = `
Array [
Object {
"context": Object {
"sortKey": "a",
},
"memberof": "classB",
"name": "apples",
},
Object {
"context": Object {
"sortKey": "c",
},
"memberof": "classB",
"name": "bananas",
},
Object {
"context": Object {
"sortKey": "b",
},
"memberof": "classB",
"name": "carrot",
},
]
`;
31 changes: 30 additions & 1 deletion __tests__/lib/sort.js
@@ -1,4 +1,5 @@
var sort = require('../../src/sort'), path = require('path');
var sort = require('../../src/sort'),
path = require('path');

test('sort stream alphanumeric', function() {
var apples = { context: { sortKey: 'a' }, name: 'apples' };
Expand Down Expand Up @@ -204,3 +205,31 @@ test('sort toc with files absolute path', function() {
})
).toMatchSnapshot();
});

test('sort toc with files absolute path', function() {
var apples = {
context: { sortKey: 'a' },
name: 'apples',
memberof: 'classB'
};
var carrot = {
context: { sortKey: 'b' },
name: 'carrot',
memberof: 'classB'
};
var bananas = {
context: { sortKey: 'c' },
name: 'bananas',
memberof: 'classB'
};

var snowflake = {
name: 'snowflake',
file: path.join(__dirname, '../fixture/snowflake.md')
};
expect(
sort([carrot, apples, bananas], {
sortOrder: 'alpha'
})
).toMatchSnapshot();
});
16 changes: 6 additions & 10 deletions src/sort.js
Expand Up @@ -94,25 +94,21 @@ module.exports = function sortDocs(comments: Array<Comment>, options: Object) {
return fixed.concat(unfixed);
};

function compare(a: string, b: string) {
return a.localeCompare(b, undefined, { caseFirst: 'upper' });
}

function compareCommentsByName(a, b) {
var akey = a.memberof || a.name;
var bkey = b.memberof || b.name;
function compareCommentsByName(a: Comment, b: Comment): number {
const akey: ?string = a.name;
const bkey: ?string = b.name;

if (akey && bkey) {
return compare(akey, bkey);
return akey.localeCompare(bkey, undefined, { caseFirst: 'upper' });
}
return 0;
}

function compareCommentsBySourceLocation(a, b) {
function compareCommentsBySourceLocation(a: Comment, b: Comment): number {
return a.context.sortKey.localeCompare(b.context.sortKey);
}

function sortComments(comments, sortOrder) {
function sortComments(comments: Array<Comment>, sortOrder: string) {
return comments.sort(
sortOrder === 'alpha'
? compareCommentsByName
Expand Down

0 comments on commit aa3496a

Please sign in to comment.