Skip to content

Commit 44a9f62

Browse files
committed
Refactor to move implementation to lib/
1 parent 56b5be5 commit 44a9f62

File tree

3 files changed

+54
-52
lines changed

3 files changed

+54
-52
lines changed

index.js

+1-52
Original file line numberDiff line numberDiff line change
@@ -1,52 +1 @@
1-
/**
2-
* @typedef {import('mdast').Root|import('mdast').Content} Node
3-
* @typedef {import('mdast').Definition} Definition
4-
*/
5-
6-
import {visit} from 'unist-util-visit'
7-
8-
const own = {}.hasOwnProperty
9-
10-
/**
11-
* Find definitions in `node`.
12-
* Uses CommonMark precedence, which means that earlier definitions are
13-
* preferred over duplicate later definitions.
14-
*
15-
* @param {Node} node
16-
*/
17-
export function definitions(node) {
18-
/** @type {Record<string, Definition>} */
19-
const cache = Object.create(null)
20-
21-
if (!node || !node.type) {
22-
throw new Error('mdast-util-definitions expected node')
23-
}
24-
25-
visit(node, 'definition', (definition) => {
26-
const id = clean(definition.identifier)
27-
if (id && !own.call(cache, id)) {
28-
cache[id] = definition
29-
}
30-
})
31-
32-
return definition
33-
34-
/**
35-
* Get a node from the bound definition cache.
36-
*
37-
* @param {string} identifier
38-
* @returns {Definition|null}
39-
*/
40-
function definition(identifier) {
41-
const id = clean(identifier)
42-
return id && own.call(cache, id) ? cache[id] : null
43-
}
44-
}
45-
46-
/**
47-
* @param {string} [value]
48-
* @returns {string}
49-
*/
50-
function clean(value) {
51-
return String(value || '').toUpperCase()
52-
}
1+
export {definitions} from './lib/index.js'

lib/index.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @typedef {import('mdast').Root|import('mdast').Content} Node
3+
* @typedef {import('mdast').Definition} Definition
4+
*/
5+
6+
import {visit} from 'unist-util-visit'
7+
8+
const own = {}.hasOwnProperty
9+
10+
/**
11+
* Find definitions in `node`.
12+
* Uses CommonMark precedence, which means that earlier definitions are
13+
* preferred over duplicate later definitions.
14+
*
15+
* @param {Node} node
16+
*/
17+
export function definitions(node) {
18+
/** @type {Record<string, Definition>} */
19+
const cache = Object.create(null)
20+
21+
if (!node || !node.type) {
22+
throw new Error('mdast-util-definitions expected node')
23+
}
24+
25+
visit(node, 'definition', (definition) => {
26+
const id = clean(definition.identifier)
27+
if (id && !own.call(cache, id)) {
28+
cache[id] = definition
29+
}
30+
})
31+
32+
return definition
33+
34+
/**
35+
* Get a node from the bound definition cache.
36+
*
37+
* @param {string} identifier
38+
* @returns {Definition|null}
39+
*/
40+
function definition(identifier) {
41+
const id = clean(identifier)
42+
return id && own.call(cache, id) ? cache[id] : null
43+
}
44+
}
45+
46+
/**
47+
* @param {string} [value]
48+
* @returns {string}
49+
*/
50+
function clean(value) {
51+
return String(value || '').toUpperCase()
52+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"main": "index.js",
3232
"types": "index.d.ts",
3333
"files": [
34+
"lib/",
3435
"index.d.ts",
3536
"index.js"
3637
],

0 commit comments

Comments
 (0)