File tree 4 files changed +29
-24
lines changed
4 files changed +29
-24
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @typedef {import('hast').Nodes } Nodes
3
+ */
4
+
5
+ // HTML whitespace expression.
6
+ // See <https://infra.spec.whatwg.org/#ascii-whitespace>.
7
+ const re = / [ \t \n \f \r ] / g
8
+
1
9
/**
2
10
* Check if the given value is *inter-element whitespace*.
3
11
*
4
- * @param {unknown } thing
5
- * Thing to check (typically `Node` or `string`).
12
+ * @param {Nodes | string } thing
13
+ * Thing to check (`Node` or `string`).
6
14
* @returns {boolean }
7
15
* Whether the `value` is inter-element whitespace (`boolean`): consisting of
8
16
* zero or more of space, tab (`\t`), line feed (`\n`), carriage return
11
19
* checked.
12
20
*/
13
21
export function whitespace ( thing ) {
14
- /** @type {string } */
15
- const value =
16
- // @ts -expect-error looks like a node.
17
- thing && typeof thing === 'object' && thing . type === 'text'
18
- ? // @ts -expect-error looks like a text.
19
- thing . value || ''
20
- : thing
22
+ return typeof thing === 'object'
23
+ ? thing . type === 'text'
24
+ ? empty ( thing . value )
25
+ : false
26
+ : empty ( thing )
27
+ }
21
28
22
- // HTML whitespace expression.
23
- // See <https://infra.spec.whatwg.org/#ascii-whitespace>.
24
- return typeof value === 'string' && value . replace ( / [ \t \n \f \r ] / g, '' ) === ''
29
+ /**
30
+ * @param {string } value
31
+ * @returns {boolean }
32
+ */
33
+ function empty ( value ) {
34
+ return value . replace ( re , '' ) === ''
25
35
}
Original file line number Diff line number Diff line change 34
34
" index.d.ts" ,
35
35
" index.js"
36
36
],
37
+ "dependencies" : {
38
+ "@types/hast" : " ^3.0.0"
39
+ },
37
40
"devDependencies" : {
38
41
"@types/node" : " ^20.0.0" ,
39
42
"c8" : " ^8.0.0" ,
Original file line number Diff line number Diff line change @@ -66,6 +66,7 @@ import {whitespace} from 'hast-util-whitespace'
66
66
whitespace ({
67
67
type: ' element' ,
68
68
tagName: ' div' ,
69
+ properties: {},
69
70
children: []
70
71
}) // => false
71
72
@@ -91,8 +92,8 @@ Check if the given value is [*inter-element whitespace*][spec].
91
92
92
93
###### Parameters
93
94
94
- * ` thing ` (` unknown ` , optional)
95
- — thing to check (typically [ ` Node ` ] [ node ] or ` string ` )
95
+ * ` thing ` ([ ` Node ` ] [ node ] or ` string ` , optional)
96
+ — thing to check
96
97
97
98
###### Returns
98
99
Original file line number Diff line number Diff line change @@ -10,11 +10,8 @@ test('whitespace', () => {
10
10
'should expose the public api'
11
11
)
12
12
13
- // @ts -expect-error: runtime.
14
- assert . equal ( whitespace ( ) , false , 'should return `false` without node' )
15
-
16
13
assert . equal (
17
- whitespace ( { type : 'element ' , tagName : 'div ' } ) ,
14
+ whitespace ( { type : 'comment ' , value : 'asd ' } ) ,
18
15
false ,
19
16
'should return `false` without text'
20
17
)
@@ -31,12 +28,6 @@ test('whitespace', () => {
31
28
'should return `true` for inter-element white-space'
32
29
)
33
30
34
- assert . equal (
35
- whitespace ( { type : 'text' } ) ,
36
- true ,
37
- 'should return `true` for `text` without value'
38
- )
39
-
40
31
assert . equal (
41
32
whitespace ( ' \v' ) ,
42
33
false ,
You can’t perform that action at this time.
0 commit comments