Skip to content

Commit aba35d9

Browse files
committed
Refactor code-style
1 parent 70fe89b commit aba35d9

File tree

2 files changed

+114
-81
lines changed

2 files changed

+114
-81
lines changed

lib/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2+
* @typedef {import('hast').Element} Element
23
* @typedef {import('hast').Nodes} Nodes
34
* @typedef {import('hast').Text} Text
4-
* @typedef {import('hast').Element} Element
55
*/
66

77
/**
@@ -17,7 +17,7 @@ const search = /\r?\n|\r/g
1717
* ###### Algorithm
1818
*
1919
* * if `tree` is a `comment` or `text`, sets its `value`
20-
* * if `tree` is a `root` or `element`, replaces its children with a `br`
20+
* * if `tree` is a `element` or `root`, replaces its children with a `br`
2121
* element for every line ending and a `text` for everything else
2222
*
2323
* ###### Notes
@@ -31,12 +31,12 @@ const search = /\r?\n|\r/g
3131
* @param {T} tree
3232
* Tree to change.
3333
* @param {string | null | undefined} [text]
34-
* Value to set.
34+
* Value to set (default: `''`).
3535
* @returns {T}
3636
* Given, modified, tree.
3737
*/
3838
export function fromText(tree, text) {
39-
const value = text === undefined || text === null ? '' : String(text)
39+
const value = text === null || text === undefined ? '' : String(text)
4040

4141
if ('children' in tree) {
4242
/** @type {Array<BreakElement | Text>} */

test.js

Lines changed: 110 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,135 @@
11
import assert from 'node:assert/strict'
22
import test from 'node:test'
3-
import {u} from 'unist-builder'
43
import {h} from 'hastscript'
4+
import {u} from 'unist-builder'
55
import {fromText} from './index.js'
6-
import * as mod from './index.js'
7-
8-
test('fromText', () => {
9-
assert.deepEqual(
10-
Object.keys(mod).sort(),
11-
['fromText'],
12-
'should expose the public api'
13-
)
146

15-
assert.deepEqual(
16-
// @ts-expect-error runtime.
17-
fromText(u('text'), 'foo'),
18-
u('text', 'foo'),
19-
'should set text nodes'
7+
test('fromText', async function (t) {
8+
await t.test('should expose the public api', async function () {
9+
assert.deepEqual(Object.keys(await import('./index.js')).sort(), [
10+
'fromText'
11+
])
12+
})
13+
14+
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+
)
20+
})
21+
22+
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+
)
28+
})
29+
30+
await t.test('should reset text nodes (2)', async function () {
31+
assert.deepEqual(fromText(u('text', 'foo')), u('text', ''))
32+
})
33+
34+
await t.test('should set parent nodes', async function () {
35+
assert.deepEqual(fromText(h('p'), 'foo'), h('p', 'foo'))
36+
})
37+
38+
await t.test(
39+
'should set parent nodes with <br>s if ␊ is used',
40+
async function () {
41+
assert.deepEqual(
42+
fromText(h('p', 'foo'), 'foo\nbar\nbaz'),
43+
h('p', ['foo', h('br'), 'bar', h('br'), 'baz'])
44+
)
45+
}
2046
)
2147

22-
assert.deepEqual(
23-
// @ts-expect-error runtime.
24-
fromText(u('text')),
25-
u('text', ''),
26-
'should reset text nodes (1)'
48+
await t.test(
49+
'should set parent nodes with <br>s if ␍ is used',
50+
async function () {
51+
assert.deepEqual(
52+
fromText(h('p', 'foo'), 'foo\rbar\rbaz'),
53+
h('p', ['foo', h('br'), 'bar', h('br'), 'baz'])
54+
)
55+
}
2756
)
2857

29-
assert.deepEqual(
30-
fromText(u('text', 'foo')),
31-
u('text', ''),
32-
'should reset text nodes (2)'
58+
await t.test(
59+
'should set parent nodes with <br>s if ␍␊ is used',
60+
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+
)
65+
}
3366
)
3467

35-
assert.deepEqual(
36-
fromText(h('p'), 'foo'),
37-
h('p', 'foo'),
38-
'should set parent nodes'
68+
await t.test(
69+
'should set parent nodes with <br>s if a final ␊ is used',
70+
async function () {
71+
assert.deepEqual(
72+
fromText(h('p', 'foo'), 'foo\n'),
73+
h('p', ['foo', h('br')])
74+
)
75+
}
3976
)
4077

41-
assert.deepEqual(
42-
fromText(h('p', 'foo'), 'foo\nbar\nbaz'),
43-
h('p', ['foo', h('br'), 'bar', h('br'), 'baz']),
44-
'should set parent nodes with <br>s if ␊ is used'
78+
await t.test(
79+
'should set parent nodes with <br>s if a final ␍␊ is used',
80+
async function () {
81+
assert.deepEqual(
82+
fromText(h('p', 'foo'), 'foo\r'),
83+
h('p', ['foo', h('br')])
84+
)
85+
}
4586
)
4687

47-
assert.deepEqual(
48-
fromText(h('p', 'foo'), 'foo\rbar\rbaz'),
49-
h('p', ['foo', h('br'), 'bar', h('br'), 'baz']),
50-
'should set parent nodes with <br>s if ␍ is used'
88+
await t.test(
89+
'should set parent nodes with <br>s if a final ␍␊ is used',
90+
async function () {
91+
assert.deepEqual(
92+
fromText(h('p', 'foo'), 'foo\r\n'),
93+
h('p', ['foo', h('br')])
94+
)
95+
}
5196
)
5297

53-
assert.deepEqual(
54-
fromText(h('p', 'foo'), 'foo\r\nbar\r\nbaz'),
55-
h('p', ['foo', h('br'), 'bar', h('br'), 'baz']),
56-
'should set parent nodes with <br>s if ␍␊ is used'
98+
await t.test(
99+
'should set parent nodes with <br>s if an initial ␊ is used',
100+
async function () {
101+
assert.deepEqual(
102+
fromText(h('p', 'foo'), '\nfoo'),
103+
h('p', [h('br'), 'foo'])
104+
)
105+
}
57106
)
58107

59-
assert.deepEqual(
60-
fromText(h('p', 'foo'), 'foo\n'),
61-
h('p', ['foo', h('br')]),
62-
'should set parent nodes with <br>s if a final ␊ is used'
108+
await t.test(
109+
'should set parent nodes with <br>s if an initial ␍␊ is used',
110+
async function () {
111+
assert.deepEqual(
112+
fromText(h('p', 'foo'), '\rfoo'),
113+
h('p', [h('br'), 'foo'])
114+
)
115+
}
63116
)
64117

65-
assert.deepEqual(
66-
fromText(h('p', 'foo'), 'foo\r'),
67-
h('p', ['foo', h('br')]),
68-
'should set parent nodes with <br>s if a final ␍␊ is used'
118+
await t.test(
119+
'should set parent nodes with <br>s if an initial ␍␊ is used',
120+
async function () {
121+
assert.deepEqual(
122+
fromText(h('p', 'foo'), '\r\nfoo'),
123+
h('p', [h('br'), 'foo'])
124+
)
125+
}
69126
)
70127

71-
assert.deepEqual(
72-
fromText(h('p', 'foo'), 'foo\r\n'),
73-
h('p', ['foo', h('br')]),
74-
'should set parent nodes with <br>s if a final ␍␊ is used'
75-
)
76-
77-
assert.deepEqual(
78-
fromText(h('p', 'foo'), '\nfoo'),
79-
h('p', [h('br'), 'foo']),
80-
'should set parent nodes with <br>s if an initial ␊ is used'
81-
)
128+
await t.test('should reset parent nodes (1)', async function () {
129+
assert.deepEqual(fromText(h('p')), h('p'))
130+
})
82131

83-
assert.deepEqual(
84-
fromText(h('p', 'foo'), '\rfoo'),
85-
h('p', [h('br'), 'foo']),
86-
'should set parent nodes with <br>s if an initial ␍␊ is used'
87-
)
88-
89-
assert.deepEqual(
90-
fromText(h('p', 'foo'), '\r\nfoo'),
91-
h('p', [h('br'), 'foo']),
92-
'should set parent nodes with <br>s if an initial ␍␊ is used'
93-
)
94-
95-
assert.deepEqual(fromText(h('p')), h('p'), 'should reset parent nodes (1)')
96-
97-
assert.deepEqual(
98-
fromText(h('p', 'foo')),
99-
h('p'),
100-
'should reset parent nodes (2)'
101-
)
132+
await t.test('should reset parent nodes (2)', async function () {
133+
assert.deepEqual(fromText(h('p', 'foo')), h('p'))
134+
})
102135
})

0 commit comments

Comments
 (0)