Skip to content

Commit

Permalink
Change to return undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 8, 2023
1 parent 6b3e2d6 commit 7a4a5e1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 66 deletions.
15 changes: 0 additions & 15 deletions index.test-d.ts

This file was deleted.

43 changes: 11 additions & 32 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,29 @@
/**
* @typedef {import('mdast').Nodes} Nodes
* @typedef {import('mdast').Paragraph} Paragraph
*/

import {visit} from 'unist-util-visit'

/**
* Remove empty paragraphs in `tree`.
*
* @template {Nodes} Tree
* Node type.
* @param {Tree} tree
* @param {Nodes} tree
* Tree to change.
* @returns {Tree extends Paragraph ? Tree | null : Tree}
* Changed `tree`, or `null` if it was an empty paragraph.
* @returns {undefined}
* Nothing.
*/
export function squeezeParagraphs(tree) {
if (emptyParagraph(tree)) {
// @ts-expect-error: it’s an empty paragraph.
return null
}

visit(tree, function (node, index, parent) {
if (index !== undefined && parent && emptyParagraph(node)) {
if (
index !== undefined &&
parent &&
node.type === 'paragraph' &&
node.children.every(function (child) {
return child.type === 'text' && /^\s*$/.test(child.value)
})
) {
parent.children.splice(index, 1)
return index
}
})

// @ts-expect-error: it’s not an empty paragraph.
return tree
}

/**
* Check if a node is an empty paragraph.
*
* @param {Nodes} node
* Node to check.
* @returns {boolean}
* Whether `node` was an empty paragraph.
*/
function emptyParagraph(node) {
return (
node.type === 'paragraph' &&
node.children.every(function (child) {
return child.type === 'text' && /^\s*$/.test(child.value)
})
)
}
13 changes: 0 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
"prettier": "^2.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"tape": "^5.0.0",
"tsd": "^0.28.0",
"type-coverage": "^2.0.0",
"typescript": "^5.0.0",
"unist-builder": "^4.0.0",
Expand Down Expand Up @@ -84,17 +82,6 @@
"strict": true
},
"xo": {
"overrides": [
{
"files": [
"**/*.ts"
],
"rules": {
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/consistent-type-assertions": "off"
}
}
],
"prettier": true
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Remove empty paragraphs in `tree`.

###### Returns

Changed `tree` ([`Node`][node]), or `null` if it was an empty paragraph.
Nothing (`undefined`).

## Types

Expand Down
17 changes: 12 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ test('squeezeParagraphs', async function (t) {
])
])

squeezeParagraphs(tree)

assert.deepEqual(
squeezeParagraphs(tree),
tree,
u('root', [
u('paragraph', [u('text', 'first')]),
u('paragraph', [
Expand All @@ -37,9 +39,14 @@ test('squeezeParagraphs', async function (t) {
)
})

await t.test('should return `null` for empty paragraphs', async function () {
const tree = u('paragraph', [])
await t.test(
'should do nothing with a (empty or not) paragraph',
async function () {
const tree = u('paragraph', [])

assert.deepEqual(squeezeParagraphs(tree), null)
})
squeezeParagraphs(tree)

assert.deepEqual(tree, u('paragraph', []))
}
)
})

0 comments on commit 7a4a5e1

Please sign in to comment.