Skip to content

Commit a98ca64

Browse files
committed
Refactor code-style
* Add more docs to JSDoc * Add support for `null` in input of API types * Add type for `GetDefinition`
1 parent 44a9f62 commit a98ca64

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
/**
2+
* @typedef {import('./lib/index.js').GetDefinition} GetDefinition
3+
*/
4+
15
export {definitions} from './lib/index.js'

lib/index.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
11
/**
2-
* @typedef {import('mdast').Root|import('mdast').Content} Node
2+
* @typedef {import('mdast').Root} Root
3+
* @typedef {import('mdast').Content} Content
34
* @typedef {import('mdast').Definition} Definition
45
*/
56

7+
/**
8+
* @typedef {Root | Content} Node
9+
*
10+
* @callback GetDefinition
11+
* Get a definition by identifier.
12+
* @param {string | null | undefined} [identifier]
13+
* Identifier of definition.
14+
* @returns {Definition | null}
15+
* Definition corresponding to `identifier`, if found.
16+
*/
17+
618
import {visit} from 'unist-util-visit'
719

820
const own = {}.hasOwnProperty
921

1022
/**
1123
* Find definitions in `node`.
24+
*
1225
* Uses CommonMark precedence, which means that earlier definitions are
1326
* preferred over duplicate later definitions.
1427
*
15-
* @param {Node} node
28+
* @param {Node} tree
29+
* Tree to check.
30+
* @returns {GetDefinition}
31+
* Getter.
1632
*/
17-
export function definitions(node) {
33+
export function definitions(tree) {
1834
/** @type {Record<string, Definition>} */
1935
const cache = Object.create(null)
2036

21-
if (!node || !node.type) {
37+
if (!tree || !tree.type) {
2238
throw new Error('mdast-util-definitions expected node')
2339
}
2440

25-
visit(node, 'definition', (definition) => {
41+
visit(tree, 'definition', (definition) => {
2642
const id = clean(definition.identifier)
2743
if (id && !own.call(cache, id)) {
2844
cache[id] = definition
@@ -31,20 +47,15 @@ export function definitions(node) {
3147

3248
return definition
3349

34-
/**
35-
* Get a node from the bound definition cache.
36-
*
37-
* @param {string} identifier
38-
* @returns {Definition|null}
39-
*/
50+
/** @type {GetDefinition} */
4051
function definition(identifier) {
4152
const id = clean(identifier)
4253
return id && own.call(cache, id) ? cache[id] : null
4354
}
4455
}
4556

4657
/**
47-
* @param {string} [value]
58+
* @param {string | null | undefined} [value]
4859
* @returns {string}
4960
*/
5061
function clean(value) {

0 commit comments

Comments
 (0)