Skip to content

Commit

Permalink
Change to return undefined, not null
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 6, 2023
1 parent 4e8e670 commit 60c4a49
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
11 changes: 5 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@
* Heading node to check.
* @param {Style | null | undefined} [relative]
* Relative style (preferred style).
* @returns {Style | null}
* Style, if it can be inferred, `null` otherwise.
* @returns {Style | undefined}
* Style, if it can be inferred, `undefined` otherwise.
*/
// To do: next major: use `undefined`.
export function headingStyle(node, relative) {
const last = node.children[node.children.length - 1]
const depth = node.depth
const pos = node.position && node.position.end
const final = last && last.position && last.position.end

if (!pos) {
return null
return undefined
}

// This can only occur for `'atx'` and `'atx-closed'` headings.
Expand Down Expand Up @@ -59,12 +58,12 @@ export function headingStyle(node, relative) {
*
* @param {number} depth
* @param {Style | null | undefined} relative
* @returns {Style | null}
* @returns {Style | undefined}
*/
function consolidate(depth, relative) {
return depth < 3
? 'atx'
: relative === 'atx' || relative === 'setext'
? relative
: null
: undefined
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ headingStyle(fromMarkdown('# ATX').children[0]) // => 'atx'
headingStyle(fromMarkdown('# ATX #\n').children[0]) // => 'atx-closed'
headingStyle(fromMarkdown('ATX\n===').children[0]) // => 'setext'

headingStyle(fromMarkdown('### ATX').children[0]) // => null
headingStyle(fromMarkdown('### ATX').children[0]) // => undefined
headingStyle(fromMarkdown('### ATX').children[0], 'setext') // => 'setext'
```

Expand All @@ -103,7 +103,7 @@ considered setext.

###### Returns

Style ([`Style`][api-style]) if it can be inferred, `null` otherwise.
Style ([`Style`][api-style]) if it can be inferred, `undefined` otherwise.

### `Style`

Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('headingStyle', async function (t) {
type: 'heading',
children: [{type: 'text', value: 'foo'}]
}),
null
undefined
)
})

Expand All @@ -45,7 +45,7 @@ test('headingStyle', async function (t) {
})

await t.test('should work on ambiguous nodes', async function () {
assert.equal(headingStyle(parseFirstNode('### ATX')), null)
assert.equal(headingStyle(parseFirstNode('### ATX')), undefined)
})

await t.test(
Expand Down

0 comments on commit 60c4a49

Please sign in to comment.