|
1 | 1 | /** |
2 | | - * @typedef {{type: 'leaf', value: string}} Leaf |
3 | | - * @typedef {{type: 'node', children: Array<Node | Leaf>}} Node |
4 | | - * @typedef {{type: 'root', children: Array<Node | Leaf>}} Root |
5 | | - * @typedef {Root | Node | Leaf} AnyNode |
| 2 | + * @typedef {import('mdast').Content} Content |
| 3 | + * @typedef {import('mdast').Root} Root |
6 | 4 | */ |
7 | 5 |
|
8 | 6 | import assert from 'node:assert/strict' |
9 | 7 | import test from 'node:test' |
10 | 8 | import {u} from 'unist-builder' |
11 | 9 | import {map} from './index.js' |
12 | | -import * as mod from './index.js' |
13 | 10 |
|
14 | | -test('map', function () { |
15 | | - assert.deepEqual( |
16 | | - Object.keys(mod).sort(), |
17 | | - ['map'], |
18 | | - 'should expose the public api' |
19 | | - ) |
20 | | - |
21 | | - /** @type {Root} */ |
22 | | - const rootA = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')]) |
23 | | - assert.deepEqual( |
24 | | - map(rootA, changeLeaf), |
25 | | - u('root', [u('node', [u('leaf', 'CHANGED')]), u('leaf', 'CHANGED')]), |
26 | | - 'should map the specified node' |
27 | | - ) |
28 | | - |
29 | | - /** @type {Root} */ |
30 | | - const rootB = { |
31 | | - type: 'root', |
32 | | - children: [ |
33 | | - {type: 'node', children: [{type: 'leaf', value: '1'}]}, |
34 | | - {type: 'leaf', value: '2'} |
35 | | - ] |
36 | | - } |
37 | | - assert.deepEqual( |
38 | | - map(rootB, changeLeaf), |
39 | | - u('root', [u('node', [u('leaf', 'CHANGED')]), u('leaf', 'CHANGED')]), |
40 | | - 'should map the specified node' |
41 | | - ) |
42 | | - |
43 | | - /** @type {Root} */ |
44 | | - const rootC = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')]) |
45 | | - assert.deepEqual( |
46 | | - // @ts-expect-error: invalid: |
47 | | - map(rootC, nullLeaf), |
48 | | - // @ts-expect-error: not valid but tested anyway. |
49 | | - u('root', [u('node', [{}]), {}]), |
50 | | - 'should work when retuning an empty object' |
51 | | - ) |
52 | | - |
53 | | - assert.deepEqual( |
54 | | - // @ts-expect-error runtime. |
55 | | - map({}, addValue), |
56 | | - {value: 'test'}, |
57 | | - 'should work when passing an empty object' |
58 | | - ) |
59 | | - |
60 | | - /** @type {Root} */ |
61 | | - const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')]) |
62 | | - |
63 | | - assert.deepEqual( |
64 | | - map(tree, asIs), |
65 | | - tree, |
66 | | - 'should support an explicitly typed `MapFunction`' |
67 | | - ) |
| 11 | +test('map', async function (t) { |
| 12 | + await t.test('should expose the public api', async function () { |
| 13 | + assert.deepEqual(Object.keys(await import('./index.js')).sort(), ['map']) |
| 14 | + }) |
| 15 | + |
| 16 | + await t.test('should map the specified node', async function () { |
| 17 | + /** @type {Root} */ |
| 18 | + const tree = u('root', [u('paragraph', [u('text', '1')]), u('text', '2')]) |
| 19 | + |
| 20 | + assert.deepEqual( |
| 21 | + map(tree, changeLeaf), |
| 22 | + u('root', [u('paragraph', [u('text', 'CHANGED')]), u('text', 'CHANGED')]) |
| 23 | + ) |
| 24 | + }) |
| 25 | + |
| 26 | + await t.test('should map the specified node', async function () { |
| 27 | + /** @type {Root} */ |
| 28 | + const tree = u('root', [u('paragraph', [u('text', '1')]), u('text', '2')]) |
| 29 | + |
| 30 | + assert.deepEqual( |
| 31 | + map(tree, changeLeaf), |
| 32 | + u('root', [u('paragraph', [u('text', 'CHANGED')]), u('text', 'CHANGED')]) |
| 33 | + ) |
| 34 | + }) |
| 35 | + |
| 36 | + await t.test('should work when passing an empty object', async function () { |
| 37 | + assert.deepEqual( |
| 38 | + // @ts-expect-error: check how not-a-node is handled. |
| 39 | + map({}, function () { |
| 40 | + return {value: 'test'} |
| 41 | + }), |
| 42 | + {value: 'test'} |
| 43 | + ) |
| 44 | + }) |
68 | 45 | }) |
69 | 46 |
|
70 | | -/** |
71 | | - * @param {AnyNode} node |
72 | | - * @returns {AnyNode} |
73 | | - */ |
74 | | -function changeLeaf(node) { |
75 | | - return node.type === 'leaf' |
76 | | - ? Object.assign({}, node, {value: 'CHANGED'}) |
77 | | - : node |
78 | | -} |
79 | | - |
80 | | -/** |
81 | | - * @param {AnyNode} node |
82 | | - * @returns {Root | Node | null} |
83 | | - */ |
84 | | -function nullLeaf(node) { |
85 | | - return node.type === 'leaf' ? null : node |
86 | | -} |
87 | | - |
88 | | -function addValue() { |
89 | | - return {value: 'test'} |
90 | | -} |
91 | | - |
92 | 47 | /** |
93 | 48 | * @type {import('./index.js').MapFunction<Root>} |
94 | 49 | */ |
95 | | -function asIs(node) { |
96 | | - return node |
| 50 | +function changeLeaf(node) { |
| 51 | + return node.type === 'text' ? {...node, value: 'CHANGED'} : node |
97 | 52 | } |
0 commit comments