Skip to content

Commit af60251

Browse files
committed
Change to yield undefined
1 parent aba35d9 commit af60251

File tree

3 files changed

+93
-83
lines changed

3 files changed

+93
-83
lines changed

lib/index.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
const search = /\r?\n|\r/g
1212

13-
// To do next major: do not return given element.
1413
/**
1514
* Set the plain-text value of a node.
1615
*
@@ -26,14 +25,12 @@ const search = /\r?\n|\r/g
2625
* In this utility, we accept all parent nodes and handle them as elements, and
2726
* for all literals we set the `value` of the given node the given value.
2827
*
29-
* @template {Nodes} T
30-
* Node type.
31-
* @param {T} tree
32-
* Tree to change.
28+
* @param {Nodes} tree
29+
* Node to change.
3330
* @param {string | null | undefined} [text]
3431
* Value to set (default: `''`).
35-
* @returns {T}
36-
* Given, modified, tree.
32+
* @returns {undefined}
33+
* Nothing.
3734
*/
3835
export function fromText(tree, text) {
3936
const value = text === null || text === undefined ? '' : String(text)
@@ -69,6 +66,4 @@ export function fromText(tree, text) {
6966
} else if (tree.type !== 'doctype') {
7067
tree.value = value
7168
}
72-
73-
return tree
7469
}

readme.md

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,44 @@ In browsers with [`esm.sh`][esmsh]:
7272
import {h} from 'hastscript'
7373
import {fromText} from 'hast-util-from-text'
7474

75-
fromText(h('p'), 'Alpha')
76-
// { type: 'element',
77-
// tagName: 'p',
78-
// properties: {},
79-
// children: [ { type: 'text', value: 'Alpha' } ] }
80-
81-
fromText(h('p', [h('b', 'Bravo'), '.']), 'Charlie')
82-
// { type: 'element',
83-
// tagName: 'p',
84-
// properties: {},
85-
// children: [ { type: 'text', value: 'Charlie' } ] }
86-
87-
fromText(h('p'), 'Delta\nEcho')
88-
// { type: 'element',
89-
// tagName: 'p',
90-
// properties: {},
91-
// children:
92-
// [ { type: 'text', value: 'Delta' },
93-
// { type: 'element', tagName: 'br', properties: {}, children: [] },
94-
// { type: 'text', value: 'Echo' } ] }
75+
const p1 = h('p')
76+
const p2 = h('p', [h('b', 'Bravo'), '.'])
77+
const p3 = h('p')
78+
79+
fromText(p1, 'Alpha')
80+
fromText(p2, 'Charlie')
81+
fromText(p3, 'Delta\nEcho')
82+
83+
console.log(p1)
84+
console.log(p2)
85+
console.log(p3)
86+
```
87+
88+
Yields:
89+
90+
```js
91+
{
92+
type: 'element',
93+
tagName: 'p',
94+
properties: {},
95+
children: [ { type: 'text', value: 'Alpha' } ]
96+
}
97+
{
98+
type: 'element',
99+
tagName: 'p',
100+
properties: {},
101+
children: [ { type: 'text', value: 'Charlie' } ]
102+
}
103+
{
104+
type: 'element',
105+
tagName: 'p',
106+
properties: {},
107+
children: [
108+
{ type: 'text', value: 'Delta' },
109+
{ type: 'element', tagName: 'br', properties: {}, children: [] },
110+
{ type: 'text', value: 'Echo' }
111+
]
112+
}
95113
```
96114

97115
## API
@@ -106,18 +124,18 @@ Set the plain-text value of a node.
106124
###### Parameters
107125

108126
* `tree` ([`Node`][node])
109-
tree to change
110-
* `value` (`string`, optional)
127+
node to change
128+
* `value` (`string`, default: `''`)
111129
— value to set
112130

113131
###### Returns
114132

115-
Given, modified, tree ([`Node`][node]).
133+
Nothing (`undefined`).
116134

117135
###### Algorithm
118136

119137
* if `tree` is a `comment` or `text`, sets its `value`
120-
* if `tree` is a `root` or `element`, replaces its children with a `br`
138+
* if `tree` is a `element` or `root`, replaces its children with a `br`
121139
element for every line ending and a `text` for everything else
122140

123141
###### Notes

test.js

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,124 +12,121 @@ test('fromText', async function (t) {
1212
})
1313

1414
await t.test('should set text nodes', async function () {
15-
assert.deepEqual(
16-
// @ts-expect-error runtime.
17-
fromText(u('text'), 'foo'),
18-
u('text', 'foo')
19-
)
15+
const node = u('text')
16+
// @ts-expect-error: check how a missing `value` is handled.
17+
fromText(node, 'foo')
18+
assert.deepEqual(node, u('text', 'foo'))
2019
})
2120

2221
await t.test('should reset text nodes (1)', async function () {
23-
assert.deepEqual(
24-
// @ts-expect-error runtime.
25-
fromText(u('text')),
26-
u('text', '')
27-
)
22+
const node = u('text')
23+
// @ts-expect-error: check how a missing `value` is handled.
24+
fromText(node)
25+
assert.deepEqual(node, u('text', ''))
2826
})
2927

3028
await t.test('should reset text nodes (2)', async function () {
31-
assert.deepEqual(fromText(u('text', 'foo')), u('text', ''))
29+
const node = u('text', 'foo')
30+
fromText(node, '')
31+
assert.deepEqual(node, u('text', ''))
3232
})
3333

3434
await t.test('should set parent nodes', async function () {
35-
assert.deepEqual(fromText(h('p'), 'foo'), h('p', 'foo'))
35+
const node = h('p')
36+
fromText(node, 'foo')
37+
assert.deepEqual(node, h('p', 'foo'))
3638
})
3739

3840
await t.test(
3941
'should set parent nodes with <br>s if ␊ is used',
4042
async function () {
41-
assert.deepEqual(
42-
fromText(h('p', 'foo'), 'foo\nbar\nbaz'),
43-
h('p', ['foo', h('br'), 'bar', h('br'), 'baz'])
44-
)
43+
const node = h('p', 'foo')
44+
fromText(node, 'foo\nbar\nbaz')
45+
assert.deepEqual(node, h('p', ['foo', h('br'), 'bar', h('br'), 'baz']))
4546
}
4647
)
4748

4849
await t.test(
4950
'should set parent nodes with <br>s if ␍ is used',
5051
async function () {
51-
assert.deepEqual(
52-
fromText(h('p', 'foo'), 'foo\rbar\rbaz'),
53-
h('p', ['foo', h('br'), 'bar', h('br'), 'baz'])
54-
)
52+
const node = h('p', 'foo')
53+
fromText(node, 'foo\rbar\rbaz')
54+
assert.deepEqual(node, h('p', ['foo', h('br'), 'bar', h('br'), 'baz']))
5555
}
5656
)
5757

5858
await t.test(
5959
'should set parent nodes with <br>s if ␍␊ is used',
6060
async function () {
61-
assert.deepEqual(
62-
fromText(h('p', 'foo'), 'foo\r\nbar\r\nbaz'),
63-
h('p', ['foo', h('br'), 'bar', h('br'), 'baz'])
64-
)
61+
const node = h('p', 'foo')
62+
fromText(node, 'foo\r\nbar\r\nbaz')
63+
assert.deepEqual(node, h('p', ['foo', h('br'), 'bar', h('br'), 'baz']))
6564
}
6665
)
6766

6867
await t.test(
6968
'should set parent nodes with <br>s if a final ␊ is used',
7069
async function () {
71-
assert.deepEqual(
72-
fromText(h('p', 'foo'), 'foo\n'),
73-
h('p', ['foo', h('br')])
74-
)
70+
const node = h('p', 'foo')
71+
fromText(node, 'foo\n')
72+
assert.deepEqual(node, h('p', ['foo', h('br')]))
7573
}
7674
)
7775

7876
await t.test(
7977
'should set parent nodes with <br>s if a final ␍␊ is used',
8078
async function () {
81-
assert.deepEqual(
82-
fromText(h('p', 'foo'), 'foo\r'),
83-
h('p', ['foo', h('br')])
84-
)
79+
const node = h('p', 'foo')
80+
fromText(node, 'foo\r')
81+
assert.deepEqual(node, h('p', ['foo', h('br')]))
8582
}
8683
)
8784

8885
await t.test(
8986
'should set parent nodes with <br>s if a final ␍␊ is used',
9087
async function () {
91-
assert.deepEqual(
92-
fromText(h('p', 'foo'), 'foo\r\n'),
93-
h('p', ['foo', h('br')])
94-
)
88+
const node = h('p', 'foo')
89+
fromText(node, 'foo\r\n')
90+
assert.deepEqual(node, h('p', ['foo', h('br')]))
9591
}
9692
)
9793

9894
await t.test(
9995
'should set parent nodes with <br>s if an initial ␊ is used',
10096
async function () {
101-
assert.deepEqual(
102-
fromText(h('p', 'foo'), '\nfoo'),
103-
h('p', [h('br'), 'foo'])
104-
)
97+
const node = h('p', 'foo')
98+
fromText(node, '\nfoo')
99+
assert.deepEqual(node, h('p', [h('br'), 'foo']))
105100
}
106101
)
107102

108103
await t.test(
109104
'should set parent nodes with <br>s if an initial ␍␊ is used',
110105
async function () {
111-
assert.deepEqual(
112-
fromText(h('p', 'foo'), '\rfoo'),
113-
h('p', [h('br'), 'foo'])
114-
)
106+
const node = h('p', 'foo')
107+
fromText(node, '\rfoo')
108+
assert.deepEqual(node, h('p', [h('br'), 'foo']))
115109
}
116110
)
117111

118112
await t.test(
119113
'should set parent nodes with <br>s if an initial ␍␊ is used',
120114
async function () {
121-
assert.deepEqual(
122-
fromText(h('p', 'foo'), '\r\nfoo'),
123-
h('p', [h('br'), 'foo'])
124-
)
115+
const node = h('p', 'foo')
116+
fromText(node, '\r\nfoo')
117+
assert.deepEqual(node, h('p', [h('br'), 'foo']))
125118
}
126119
)
127120

128121
await t.test('should reset parent nodes (1)', async function () {
129-
assert.deepEqual(fromText(h('p')), h('p'))
122+
const node = h('p')
123+
fromText(node)
124+
assert.deepEqual(node, h('p'))
130125
})
131126

132127
await t.test('should reset parent nodes (2)', async function () {
133-
assert.deepEqual(fromText(h('p', 'foo')), h('p'))
128+
const node = h('p', 'foo')
129+
fromText(node)
130+
assert.deepEqual(node, h('p'))
134131
})
135132
})

0 commit comments

Comments
 (0)