Skip to content

Commit 34fd4b7

Browse files
committed
Refactor to move implementation to lib/
1 parent de13ee2 commit 34fd4b7

File tree

3 files changed

+62
-60
lines changed

3 files changed

+62
-60
lines changed

index.js

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1 @@
1-
/**
2-
* @typedef {import('xast').Root} Root
3-
* @typedef {import('xast').Element} Element
4-
* @typedef {import('xast').RootChildMap} RootChildMap
5-
*/
6-
7-
/**
8-
* @typedef {RootChildMap[keyof RootChildMap]} Child
9-
* @typedef {Root | Child} Node
10-
* @typedef {Root | Element} Parent
11-
*/
12-
13-
/**
14-
* Get the plain-text value of a node.
15-
*
16-
* @param {Node} node
17-
* Node to serialize.
18-
* @returns {string}
19-
* Serialized node.
20-
*/
21-
export function toString(node) {
22-
// A root or an element
23-
if ('children' in node) return all(node)
24-
return 'value' in node ? node.value : ''
25-
}
26-
27-
/**
28-
* Serialize a child.
29-
*
30-
* @param {Node} node
31-
* Child to serialize.
32-
* @returns {string}
33-
* Serialized node.
34-
*/
35-
function one(node) {
36-
if (node.type === 'text') return node.value
37-
// Ignore things like comments, instruction, cdata.
38-
return 'children' in node ? all(node) : ''
39-
}
40-
41-
/**
42-
* Serialize a parent.
43-
*
44-
* @param {Parent} node
45-
* Parent to serialize.
46-
* @returns {string}
47-
* Serialized node.
48-
*/
49-
function all(node) {
50-
const children = node.children
51-
let index = -1
52-
/** @type {Array<string>} */
53-
const result = []
54-
55-
while (++index < children.length) {
56-
result[index] = one(children[index])
57-
}
58-
59-
return result.join('')
60-
}
1+
export {toString} from './lib/index.js'

lib/index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @typedef {import('xast').Root} Root
3+
* @typedef {import('xast').Element} Element
4+
* @typedef {import('xast').RootChildMap} RootChildMap
5+
*/
6+
7+
/**
8+
* @typedef {RootChildMap[keyof RootChildMap]} Child
9+
* @typedef {Root | Child} Node
10+
* @typedef {Root | Element} Parent
11+
*/
12+
13+
/**
14+
* Get the plain-text value of a node.
15+
*
16+
* @param {Node} node
17+
* Node to serialize.
18+
* @returns {string}
19+
* Serialized node.
20+
*/
21+
export function toString(node) {
22+
// A root or an element
23+
if ('children' in node) return all(node)
24+
return 'value' in node ? node.value : ''
25+
}
26+
27+
/**
28+
* Serialize a child.
29+
*
30+
* @param {Node} node
31+
* Child to serialize.
32+
* @returns {string}
33+
* Serialized node.
34+
*/
35+
function one(node) {
36+
if (node.type === 'text') return node.value
37+
// Ignore things like comments, instruction, cdata.
38+
return 'children' in node ? all(node) : ''
39+
}
40+
41+
/**
42+
* Serialize a parent.
43+
*
44+
* @param {Parent} node
45+
* Parent to serialize.
46+
* @returns {string}
47+
* Serialized node.
48+
*/
49+
function all(node) {
50+
const children = node.children
51+
let index = -1
52+
/** @type {Array<string>} */
53+
const result = []
54+
55+
while (++index < children.length) {
56+
result[index] = one(children[index])
57+
}
58+
59+
return result.join('')
60+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"main": "index.js",
3030
"types": "index.d.ts",
3131
"files": [
32+
"lib/",
3233
"index.d.ts",
3334
"index.js"
3435
],

0 commit comments

Comments
 (0)