From 46cd67b02d17c79d96d7d3bad9475118d6bf3ae3 Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Fri, 3 Dec 2021 17:02:49 +0100 Subject: [PATCH 1/4] correctly sort undefined values --- src/sort.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sort.js b/src/sort.js index a5bde334f..17736017b 100644 --- a/src/sort.js +++ b/src/sort.js @@ -110,6 +110,8 @@ function compareCommentsByField(field, a, b) { if (akey && bkey) { return akey.localeCompare(bkey, undefined, { caseFirst: 'upper' }); } + if (akey) return 1; + if (bkey) return -1; return 0; } From 38bcfcbb9b7a48ebd90d969b38db957b394aa2aa Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Fri, 3 Dec 2021 17:11:35 +0100 Subject: [PATCH 2/4] the new sort order is the correct one --- __tests__/lib/__snapshots__/sort.js.snap | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/__tests__/lib/__snapshots__/sort.js.snap b/__tests__/lib/__snapshots__/sort.js.snap index dee9fc0b0..13fa12e9b 100644 --- a/__tests__/lib/__snapshots__/sort.js.snap +++ b/__tests__/lib/__snapshots__/sort.js.snap @@ -156,26 +156,26 @@ exports[`sort toc with files absolute path 3`] = ` Array [ Object { "context": Object { - "sortKey": "a", + "sortKey": "b", }, - "kind": "function", "memberof": "classB", - "name": "apples", + "name": "carrot", }, Object { "context": Object { - "sortKey": "c", + "sortKey": "a", }, "kind": "function", "memberof": "classB", - "name": "bananas", + "name": "apples", }, Object { "context": Object { - "sortKey": "b", + "sortKey": "c", }, + "kind": "function", "memberof": "classB", - "name": "carrot", + "name": "bananas", }, ] `; From 257c54ef9771c8b9221daa5e77f32e9f18ffd29c Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Fri, 3 Dec 2021 20:38:34 +0100 Subject: [PATCH 3/4] support sorting by memberof --- __tests__/lib/__snapshots__/sort.js.snap | 32 ++++++++++++++++++++++-- __tests__/lib/sort.js | 8 +++++- docs/USAGE.md | 2 +- src/commands/shared_options.js | 2 +- src/sort.js | 3 ++- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/__tests__/lib/__snapshots__/sort.js.snap b/__tests__/lib/__snapshots__/sort.js.snap index 13fa12e9b..f35e6bf19 100644 --- a/__tests__/lib/__snapshots__/sort.js.snap +++ b/__tests__/lib/__snapshots__/sort.js.snap @@ -139,7 +139,7 @@ Array [ "sortKey": "c", }, "kind": "function", - "memberof": "classB", + "memberof": "classA", "name": "bananas", }, Object { @@ -174,8 +174,36 @@ Array [ "sortKey": "c", }, "kind": "function", - "memberof": "classB", + "memberof": "classA", + "name": "bananas", + }, +] +`; + +exports[`sort toc with files absolute path 4`] = ` +Array [ + Object { + "context": Object { + "sortKey": "c", + }, + "kind": "function", + "memberof": "classA", "name": "bananas", }, + Object { + "context": Object { + "sortKey": "b", + }, + "memberof": "classB", + "name": "carrot", + }, + Object { + "context": Object { + "sortKey": "a", + }, + "kind": "function", + "memberof": "classB", + "name": "apples", + }, ] `; diff --git a/__tests__/lib/sort.js b/__tests__/lib/sort.js index 76ce57f3c..941b163b1 100644 --- a/__tests__/lib/sort.js +++ b/__tests__/lib/sort.js @@ -141,7 +141,7 @@ test('sort toc with files absolute path', function () { context: { sortKey: 'c' }, name: 'bananas', kind: 'function', - memberof: 'classB' + memberof: 'classA' }; const snowflake = { @@ -159,4 +159,10 @@ test('sort toc with files absolute path', function () { sortOrder: ['kind', 'alpha'] }) ).toMatchSnapshot(); + + expect( + sort([carrot, apples, bananas], { + sortOrder: ['memberof', 'kind', 'alpha'] + }) + ).toMatchSnapshot(); }); diff --git a/docs/USAGE.md b/docs/USAGE.md index 5fdc6bf03..f801e2568 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -58,7 +58,7 @@ Options: [boolean] [default: false] --sort-order The order to sort the documentation, may be specified multiple times - [choices: "source", "alpha", "kind"] + [choices: "source", "alpha", "kind", "memberof"] [default: "source"] --output, -o output location. omit for stdout, otherwise is a filename for single-file outputs and a directory diff --git a/src/commands/shared_options.js b/src/commands/shared_options.js index 0379debb8..0e8464dfd 100644 --- a/src/commands/shared_options.js +++ b/src/commands/shared_options.js @@ -75,7 +75,7 @@ export const sharedInputOptions = { 'sort-order': { describe: 'The order to sort the documentation', array: true, - choices: ['source', 'alpha', 'kind', 'access'], + choices: ['source', 'alpha', 'kind', 'access', 'memberof'], default: ['source'] }, resolve: { diff --git a/src/sort.js b/src/sort.js index 17736017b..db27b02d5 100644 --- a/src/sort.js +++ b/src/sort.js @@ -123,7 +123,8 @@ const sortFns = { alpha: compareCommentsByField.bind(null, 'name'), source: compareCommentsBySourceLocation, kind: compareCommentsByField.bind(null, 'kind'), - access: compareCommentsByField.bind(null, 'access') + access: compareCommentsByField.bind(null, 'access'), + memberof: compareCommentsByField.bind(null, 'memberof') }; function sortComments(comments, sortOrder) { From cc800febb546bf4348f8edeadad88c76e64e4a0c Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Fri, 3 Dec 2021 20:38:34 +0100 Subject: [PATCH 4/4] support sorting by memberof --- __tests__/lib/__snapshots__/sort.js.snap | 32 ++++++++++++++++++++++-- __tests__/lib/sort.js | 8 +++++- docs/USAGE.md | 2 +- src/commands/shared_options.js | 2 +- src/sort.js | 3 ++- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/__tests__/lib/__snapshots__/sort.js.snap b/__tests__/lib/__snapshots__/sort.js.snap index 13fa12e9b..f35e6bf19 100644 --- a/__tests__/lib/__snapshots__/sort.js.snap +++ b/__tests__/lib/__snapshots__/sort.js.snap @@ -139,7 +139,7 @@ Array [ "sortKey": "c", }, "kind": "function", - "memberof": "classB", + "memberof": "classA", "name": "bananas", }, Object { @@ -174,8 +174,36 @@ Array [ "sortKey": "c", }, "kind": "function", - "memberof": "classB", + "memberof": "classA", + "name": "bananas", + }, +] +`; + +exports[`sort toc with files absolute path 4`] = ` +Array [ + Object { + "context": Object { + "sortKey": "c", + }, + "kind": "function", + "memberof": "classA", "name": "bananas", }, + Object { + "context": Object { + "sortKey": "b", + }, + "memberof": "classB", + "name": "carrot", + }, + Object { + "context": Object { + "sortKey": "a", + }, + "kind": "function", + "memberof": "classB", + "name": "apples", + }, ] `; diff --git a/__tests__/lib/sort.js b/__tests__/lib/sort.js index 76ce57f3c..941b163b1 100644 --- a/__tests__/lib/sort.js +++ b/__tests__/lib/sort.js @@ -141,7 +141,7 @@ test('sort toc with files absolute path', function () { context: { sortKey: 'c' }, name: 'bananas', kind: 'function', - memberof: 'classB' + memberof: 'classA' }; const snowflake = { @@ -159,4 +159,10 @@ test('sort toc with files absolute path', function () { sortOrder: ['kind', 'alpha'] }) ).toMatchSnapshot(); + + expect( + sort([carrot, apples, bananas], { + sortOrder: ['memberof', 'kind', 'alpha'] + }) + ).toMatchSnapshot(); }); diff --git a/docs/USAGE.md b/docs/USAGE.md index 5fdc6bf03..f801e2568 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -58,7 +58,7 @@ Options: [boolean] [default: false] --sort-order The order to sort the documentation, may be specified multiple times - [choices: "source", "alpha", "kind"] + [choices: "source", "alpha", "kind", "memberof"] [default: "source"] --output, -o output location. omit for stdout, otherwise is a filename for single-file outputs and a directory diff --git a/src/commands/shared_options.js b/src/commands/shared_options.js index 0379debb8..0e8464dfd 100644 --- a/src/commands/shared_options.js +++ b/src/commands/shared_options.js @@ -75,7 +75,7 @@ export const sharedInputOptions = { 'sort-order': { describe: 'The order to sort the documentation', array: true, - choices: ['source', 'alpha', 'kind', 'access'], + choices: ['source', 'alpha', 'kind', 'access', 'memberof'], default: ['source'] }, resolve: { diff --git a/src/sort.js b/src/sort.js index 17736017b..db27b02d5 100644 --- a/src/sort.js +++ b/src/sort.js @@ -123,7 +123,8 @@ const sortFns = { alpha: compareCommentsByField.bind(null, 'name'), source: compareCommentsBySourceLocation, kind: compareCommentsByField.bind(null, 'kind'), - access: compareCommentsByField.bind(null, 'access') + access: compareCommentsByField.bind(null, 'access'), + memberof: compareCommentsByField.bind(null, 'memberof') }; function sortComments(comments, sortOrder) {