Skip to content

Commit 1635e2f

Browse files
committed
hast-util-from-string: remove return value
1 parent d831bc7 commit 1635e2f

File tree

4 files changed

+63
-55
lines changed

4 files changed

+63
-55
lines changed

packages/hast-util-from-string/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@
2323
* import {h} from 'hastscript'
2424
* import {fromString} from 'hast-util-from-string'
2525
*
26-
* console.log(fromString(h('p'), 'Alpha'))
26+
* const p = h('p')
27+
* fromString(p, 'Alpha')
28+
* console.log(p)
2729
* // { type: 'element',
2830
* // tagName: 'p',
2931
* // properties: {},
3032
* // children: [ { type: 'text', value: 'Alpha' } ] }
31-
* console.log(fromString(h('div', [h('b', 'Bold'), ' and ', h('i', 'italic'), '.']), 'Charlie'))
33+
*
34+
* const div = h('div', [h('b', 'Bold'), ' and ', h('i', 'italic'), '.'])
35+
* fromString(div, 'Charlie')
36+
* console.log(div)
3237
* // { type: 'element',
3338
* // tagName: 'div',
3439
* // properties: {},
@@ -49,12 +54,12 @@
4954
*
5055
* ###### Parameters
5156
*
52-
* * `node` (`Node`) — node to change.
57+
* * `node` (`Node`) — node to change
5358
* * `value` (`string`, default: `''`) — value to use
5459
*
5560
* ###### Returns
5661
*
57-
* Given node (`Node`).
62+
* Nothing (`undefined`).
5863
*/
5964

6065
export {fromString} from './lib/index.js'

packages/hast-util-from-string/lib/index.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22
* @typedef {import('hast').Nodes} Nodes
33
*/
44

5-
// To do: next major: remove return result.
65
/**
76
* Set the plain-text value of a hast node.
87
* This is like the DOMs `Node#textContent` setter.
98
* The given node is returned.
109
*
11-
* @template {Nodes} Thing
12-
* Node kind.
13-
* @param {Thing} node
10+
* @param {Nodes} node
1411
* Node to change.
1512
* @param {string | null | undefined} [value='']
1613
* Value to use (default: `''`)
17-
* @returns {Thing}
18-
* Given node.
14+
* @returns {undefined}
15+
* Nothing.
1916
*/
2017
export function fromString(node, value) {
2118
const normalized = value === undefined || value === null ? '' : String(value)
@@ -29,6 +26,4 @@ export function fromString(node, value) {
2926
} else if (node.type !== 'doctype') {
3027
node.value = normalized
3128
}
32-
33-
return node
3429
}

packages/hast-util-from-string/readme.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,17 @@ In browsers with [`esm.sh`][esm-sh]:
7373
import {h} from 'hastscript'
7474
import {fromString} from 'hast-util-from-string'
7575

76-
console.log(fromString(h('p'), 'Alpha'))
76+
const p = h('p')
77+
fromString(p, 'Alpha')
78+
console.log(p)
7779
// { type: 'element',
7880
// tagName: 'p',
7981
// properties: {},
8082
// children: [ { type: 'text', value: 'Alpha' } ] }
81-
console.log(fromString(h('div', [h('b', 'Bold'), ' and ', h('i', 'italic'), '.']), 'Charlie'))
83+
84+
const div = h('div', [h('b', 'Bold'), ' and ', h('i', 'italic'), '.'])
85+
fromString(div, 'Charlie')
86+
console.log(div)
8287
// { type: 'element',
8388
// tagName: 'div',
8489
// properties: {},
@@ -103,12 +108,12 @@ Set the plain-text value of a node.
103108

104109
###### Parameters
105110

106-
* `node` (`Node`) — node to change.
111+
* `node` (`Node`) — node to change
107112
* `value` (`string`, default: `''`) — value to use
108113

109114
###### Returns
110115

111-
Given node (`Node`).
116+
Nothing (`undefined`).
112117

113118
## Syntax
114119

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,59 @@
1+
/**
2+
* @typedef {import('hast').Nodes} Nodes
3+
*/
4+
15
import assert from 'node:assert/strict'
26
import test from 'node:test'
37
import {fromString} from './index.js'
48

59
test('hast-util-from-string', async function (t) {
610
await t.test('should set text nodes', async function () {
7-
assert.deepEqual(fromString({type: 'text', value: 'alpha'}, 'bravo'), {
8-
type: 'text',
9-
value: 'bravo'
10-
})
11+
/** @type {Nodes} */
12+
const node = {type: 'text', value: 'alpha'}
13+
fromString(node, 'bravo')
14+
assert.deepEqual(node, {type: 'text', value: 'bravo'})
1115
})
1216

1317
await t.test('should reset text nodes', async function () {
14-
assert.deepEqual(fromString({type: 'text', value: 'alpha'}), {
15-
type: 'text',
16-
value: ''
17-
})
18+
/** @type {Nodes} */
19+
const node = {type: 'text', value: 'alpha'}
20+
fromString(node)
21+
assert.deepEqual(node, {type: 'text', value: ''})
1822
})
1923

2024
await t.test('should set parent nodes', async function () {
21-
assert.deepEqual(
22-
fromString(
23-
{
24-
type: 'element',
25-
tagName: 'p',
26-
properties: {},
27-
children: [{type: 'text', value: 'alpha'}]
28-
},
29-
'bravo'
30-
),
31-
{
32-
type: 'element',
33-
tagName: 'p',
34-
properties: {},
35-
children: [{type: 'text', value: 'bravo'}]
36-
}
37-
)
25+
/** @type {Nodes} */
26+
const node = {
27+
type: 'element',
28+
tagName: 'p',
29+
properties: {},
30+
children: [{type: 'text', value: 'alpha'}]
31+
}
32+
fromString(node, 'bravo')
33+
34+
assert.deepEqual(node, {
35+
type: 'element',
36+
tagName: 'p',
37+
properties: {},
38+
children: [{type: 'text', value: 'bravo'}]
39+
})
3840
})
3941

4042
await t.test('should reset parent nodes', async function () {
41-
assert.deepEqual(
42-
fromString({
43-
type: 'element',
44-
tagName: 'p',
45-
properties: {},
46-
children: [{type: 'text', value: 'alpha'}]
47-
}),
48-
{
49-
type: 'element',
50-
tagName: 'p',
51-
properties: {},
52-
children: []
53-
}
54-
)
43+
/** @type {Nodes} */
44+
const node = {
45+
type: 'element',
46+
tagName: 'p',
47+
properties: {},
48+
children: [{type: 'text', value: 'alpha'}]
49+
}
50+
fromString(node)
51+
52+
assert.deepEqual(node, {
53+
type: 'element',
54+
tagName: 'p',
55+
properties: {},
56+
children: []
57+
})
5558
})
5659
})

0 commit comments

Comments
 (0)