Skip to content

Commit

Permalink
fix: prioritizedEntries class sort order
Browse files Browse the repository at this point in the history
Fixes: #104
PR-URL: #105
  • Loading branch information
lundibundi committed Sep 14, 2020
1 parent 509f110 commit dffc814
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 34 deletions.
69 changes: 35 additions & 34 deletions lib/generator.js
Expand Up @@ -4,6 +4,8 @@ const common = require('@metarhia/common');

const types = require('./types');

const { iter } = common;

const ALL_TYPES = new Set(types.STANDARD_TYPES);

const MAX_LINE_LENGTH = 80;
Expand Down Expand Up @@ -41,14 +43,12 @@ const links = [
];

const getLinks = (usedTypes, customLinks) =>
common
.iter(
common
.iter(links)
.chain(customLinks)
.filter(([type]) => usedTypes.has(type))
.collectTo(Map)
)
iter(
iter(links)
.chain(customLinks)
.filter(([type]) => usedTypes.has(type))
.collectTo(Map)
)
.map(([type, link]) => '[' + type.toLowerCase() + ']: ' + link)
.collectTo(Set);

Expand Down Expand Up @@ -465,34 +465,35 @@ const generateContentsTable = (buf, levelLimit = 0) => {
};

const sortMethods = (methods, prioritizedEntries) => {
const result = [];
const other = [];
for (const [method, sig] of methods) {
for (const entry of prioritizedEntries) {
if (
method === entry ||
(method.startsWith(entry) && method.slice(entry.length).startsWith('.'))
) {
result.push([method, sig]);
}
const sortFn = ([a], [b]) => {
if (a === b) return 0;
let aIdx = prioritizedEntries.indexOf(a);
let bIdx = prioritizedEntries.indexOf(b);
if (aIdx === -1) aIdx = Infinity;
if (bIdx === -1) bIdx = Infinity;
if (aIdx < bIdx) return -1;
if (aIdx > bIdx) return 1;
if (a < b) return -1;
if (a > b) return 1;
return 0;
};
const other = Symbol('other');
const grouped = iter(methods).groupBy(
([m]) =>
prioritizedEntries.find(e => m === e || m.startsWith(e + '.')) || other
);
let result = iter([]);
for (const p of prioritizedEntries) {
const entries = grouped.get(p);
if (entries) {
const [other, sorted] = iter(entries).partition(
([m]) => !!prioritizedEntries.find(e => m === e)
);
result = result.chain(sorted.sort(sortFn)).chain(other);
}
other.push([method, sig]);
}
result
.sort(([a], [b]) => {
if (a === b) return 0;
let aIdx = prioritizedEntries.indexOf(a);
let bIdx = prioritizedEntries.indexOf(b);
if (aIdx === -1) aIdx = Infinity;
if (bIdx === -1) bIdx = Infinity;
if (aIdx < bIdx) return -1;
if (aIdx > bIdx) return 1;
if (a < b) return -1;
if (a > b) return 1;
return 0;
})
.push(...other);
return new Map(result);
result = result.chain(grouped.get(other));
return result.collectTo(Map);
};

// Generate md from interfaces inventory
Expand Down
22 changes: 22 additions & 0 deletions test/example.js
Expand Up @@ -273,6 +273,26 @@ class UndocumentedClass extends AnotherClass {
}
}

// Order1Class should be ordered correctly.
class Order1Class {
constructor(abc) {}

order1Method() {}

order2Method() {}

order3Method() {}
}

// Order2Class should be ordered correctly.
class Order2Class {
constructor(abc) {}

aMethod() {}

order2Method() {}
}

// PrototypeClass description description
// Note that classes on prototypes will not have constructors and will be
// treated as a regular <function>.
Expand Down Expand Up @@ -412,4 +432,6 @@ module.exports = {

nullObject: null,
nullObjectCreate: Object.create(null),
Order1Class,
Order2Class,
};
31 changes: 31 additions & 0 deletions test/example.md
@@ -1,4 +1,13 @@
- [Interface example](#interface-example)
- [Order2Class](#class-order2class)
- [Order2Class.prototype.constructor](#order2classprototypeconstructorabc)
- [Order2Class.prototype.aMethod](#order2classprototypeamethod)
- [Order2Class.prototype.order2Method](#order2classprototypeorder2method)
- [Order1Class](#class-order1class)
- [Order1Class.prototype.order2Method](#order1classprototypeorder2method)
- [Order1Class.prototype.order1Method](#order1classprototypeorder1method)
- [Order1Class.prototype.constructor](#order1classprototypeconstructorabc)
- [Order1Class.prototype.order3Method](#order1classprototypeorder3method)
- [methodName](#methodnamenum-str-arg-flag-arr-data-obj-cb)
- [noTitleFunction](#notitlefunctionstr-num)
- [typeFunction](#typefunctionobj-arg1-arg2-arg3)
Expand Down Expand Up @@ -37,6 +46,28 @@

# Interface: example

## class Order2Class

Order2Class should be ordered correctly.

### Order2Class.prototype.constructor(abc)

### Order2Class.prototype.aMethod()

### Order2Class.prototype.order2Method()

## class Order1Class

Order1Class should be ordered correctly.

### Order1Class.prototype.order2Method()

### Order1Class.prototype.order1Method()

### Order1Class.prototype.constructor(abc)

### Order1Class.prototype.order3Method()

## methodName(num, str, arg, flag\[, arr\[, data\]\], obj, cb)

- `num`: [`<number>`][number] argument description
Expand Down
4 changes: 4 additions & 0 deletions test/testConfig.json
Expand Up @@ -9,6 +9,10 @@
"ExampleClass#getProp1": "#prototypeclassprototypegetprop1"
},
"prioritizedEntries": [
"Order2Class",
"Order1Class",
"Order1Class.prototype.order2Method",
"Order1Class.prototype.order1Method",
"methodName",
"noTitleFunction",
"typeFunction",
Expand Down

0 comments on commit dffc814

Please sign in to comment.