File tree Expand file tree Collapse file tree 4 files changed +29
-24
lines changed
Expand file tree Collapse file tree 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+
19/**
210 * Check if the given value is *inter-element whitespace*.
311 *
4- * @param {unknown } thing
5- * Thing to check (typically `Node` or `string`).
12+ * @param {Nodes | string } thing
13+ * Thing to check (`Node` or `string`).
614 * @returns {boolean }
715 * Whether the `value` is inter-element whitespace (`boolean`): consisting of
816 * zero or more of space, tab (`\t`), line feed (`\n`), carriage return
1119 * checked.
1220 */
1321export 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+ }
2128
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 , '' ) === ''
2535}
Original file line number Diff line number Diff line change 3434 " index.d.ts" ,
3535 " index.js"
3636 ],
37+ "dependencies" : {
38+ "@types/hast" : " ^3.0.0"
39+ },
3740 "devDependencies" : {
3841 "@types/node" : " ^20.0.0" ,
3942 "c8" : " ^8.0.0" ,
Original file line number Diff line number Diff line change @@ -66,6 +66,7 @@ import {whitespace} from 'hast-util-whitespace'
6666whitespace ({
6767 type: ' element' ,
6868 tagName: ' div' ,
69+ properties: {},
6970 children: []
7071}) // => false
7172
@@ -91,8 +92,8 @@ Check if the given value is [*inter-element whitespace*][spec].
9192
9293###### Parameters
9394
94- * ` thing ` (` unknown ` , optional)
95- — thing to check (typically [ ` Node ` ] [ node ] or ` string ` )
95+ * ` thing ` ([ ` Node ` ] [ node ] or ` string ` , optional)
96+ — thing to check
9697
9798###### Returns
9899
Original file line number Diff line number Diff line change @@ -10,11 +10,8 @@ test('whitespace', () => {
1010 'should expose the public api'
1111 )
1212
13- // @ts -expect-error: runtime.
14- assert . equal ( whitespace ( ) , false , 'should return `false` without node' )
15-
1613 assert . equal (
17- whitespace ( { type : 'element ' , tagName : 'div ' } ) ,
14+ whitespace ( { type : 'comment ' , value : 'asd ' } ) ,
1815 false ,
1916 'should return `false` without text'
2017 )
@@ -31,12 +28,6 @@ test('whitespace', () => {
3128 'should return `true` for inter-element white-space'
3229 )
3330
34- assert . equal (
35- whitespace ( { type : 'text' } ) ,
36- true ,
37- 'should return `true` for `text` without value'
38- )
39-
4031 assert . equal (
4132 whitespace ( ' \v' ) ,
4233 false ,
You can’t perform that action at this time.
0 commit comments