Skip to content

Commit 2e3ff5b

Browse files
committed
Change to return undefined, not null
1 parent c96a9eb commit 2e3ff5b

File tree

27 files changed

+42
-82
lines changed

27 files changed

+42
-82
lines changed

lib/contents.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import extend from 'extend'
3030
* @param {ContentsOptions} settings
3131
*/
3232
export function contents(map, settings) {
33-
const {ordered = false, tight = false, prefix = null} = settings
33+
const {ordered = false, tight = false, prefix} = settings
3434
/** @type {List} */
3535
const table = {type: 'list', ordered, spread: false, children: []}
3636
let minDepth = Number.POSITIVE_INFINITY

lib/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616
*
1717
* @typedef Result
1818
* Results.
19-
* @property {number | null} index
19+
* @property {number | undefined} index
2020
* Index of the node right after the table of contents heading, `-1` if no
21-
* heading was found, `null` if no `heading` was given.
22-
* @property {number | null} endIndex
21+
* heading was found, `undefined` if no `heading` was given.
22+
* @property {number | undefined} endIndex
2323
* Index of the first node after `heading` that is not part of its section,
24-
* `-1` if no heading was found, `null` if no `heading` was given, same as
25-
* `index` if there are no nodes between `heading` and the first heading in
26-
* the table of contents.
27-
* @property {List | null} map
28-
* List representing the generated table of contents, `null` if no table of
29-
* contents could be created, either because no heading was found or because
30-
* no following headings were found.
24+
* `-1` if no heading was found, `undefined` if no `heading` was given, same
25+
* as `index` if there are no nodes between `heading` and the first heading
26+
* in the table of contents.
27+
* @property {List | undefined} map
28+
* List representing the generated table of contents, `undefined` if no table
29+
* of contents could be created, either because no heading was found or
30+
* because no following headings were found.
3131
*/
3232

3333
import {search} from './search.js'
@@ -60,8 +60,8 @@ export function toc(tree, options) {
6060
const result = search(tree, heading, settings)
6161

6262
return {
63-
index: heading ? result.index : null,
64-
endIndex: heading ? result.endIndex : null,
65-
map: result.map.length > 0 ? contents(result.map, settings) : null
63+
index: heading ? result.index : undefined,
64+
endIndex: heading ? result.endIndex : undefined,
65+
map: result.map.length > 0 ? contents(result.map, settings) : undefined
6666
}
6767
}

readme.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ Yields:
9595

9696
```javascript
9797
{
98-
index: null,
99-
endIndex: null,
98+
index: undefined,
99+
endIndex: undefined,
100100
map: {
101101
type: 'list',
102102
ordered: false,
@@ -171,18 +171,18 @@ Results (TypeScript type).
171171

172172
###### Fields
173173

174-
* `index` (`number` or `null`)
174+
* `index` (`number` or `undefined`)
175175
— index of the node right after the table of contents heading, `-1` if no
176-
heading was found, `null` if no `heading` was given
177-
* `endIndex` (`number` or `null`)
176+
heading was found, `undefined` if no `heading` was given
177+
* `endIndex` (`number` or `undefined`)
178178
— index of the first node after `heading` that is not part of its section,
179-
`-1` if no heading was found, `null` if no `heading` was given, same as
179+
`-1` if no heading was found, `undefined` if no `heading` was given, same as
180180
`index` if there are no nodes between `heading` and the first heading in
181181
the table of contents
182-
* `map` ([`List`][list] or `null`)
183-
— list representing the generated table of contents, `null` if no table of
184-
contents could be created, either because no heading was found or because
185-
no following headings were found
182+
* `map` ([`List`][list] or `undefined`)
183+
— list representing the generated table of contents, `undefined` if no
184+
table of contents could be created, either because no heading was found or
185+
because no following headings were found
186186

187187
## Types
188188

test/fixtures/deep-headings-with-config/output.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"index": null,
3-
"endIndex": null,
42
"map": {
53
"type": "list",
64
"ordered": false,

test/fixtures/deep-headings/output.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"index": null,
3-
"endIndex": null,
42
"map": {
53
"type": "list",
64
"ordered": false,

test/fixtures/empty-headings/output.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"index": null,
3-
"endIndex": null,
42
"map": {
53
"type": "list",
64
"ordered": false,
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
22
"index": 2,
3-
"endIndex": 2,
4-
"map": null
3+
"endIndex": 2
54
}

test/fixtures/headings-ordered/output.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"index": null,
3-
"endIndex": null,
42
"map": {
53
"type": "list",
64
"ordered": true,
@@ -110,4 +108,4 @@
110108
}
111109
]
112110
}
113-
}
111+
}

test/fixtures/image-in-link-in-heading/output.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"index": null,
3-
"endIndex": null,
42
"map": {
53
"type": "list",
64
"ordered": false,

test/fixtures/maximum-depth-1/output.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"index": null,
3-
"endIndex": null,
42
"map": {
53
"type": "list",
64
"ordered": false,

0 commit comments

Comments
 (0)