Skip to content

Commit 644926d

Browse files
committed
Refactor code-style
* Accept `null` as input in types * Add more docs to JSDoc
1 parent 47aba33 commit 644926d

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

index.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
// To do next major: use `structuredClone` (or so?) to deep clone `properties`
2+
// and the like: the return value has to be a clone (not shallow copy) of the
3+
// passed tree.
4+
15
/**
26
* @typedef {import('hast').Root} Root
37
* @typedef {import('hast').Content} Content
48
* @typedef {import('hast').Text} Text
5-
* @typedef {Root|Content} Node
9+
*/
10+
11+
/**
12+
* @typedef {Root | Content} Node
613
*
714
* @typedef Options
815
* Configuration.
9-
* @property {number} [size=140]
16+
* @property {number | null | undefined} [size=140]
1017
* Number of characters to truncate to.
11-
* @property {string} [ellipsis]
18+
* @property {string | null | undefined} [ellipsis]
1219
* Value to use at truncation point.
13-
* @property {number} [maxCharacterStrip=30]
20+
* @property {number | null | undefined} [maxCharacterStrip=30]
1421
* How far to walk back.
1522
* The algorithm attempts to break right after a word rather than the exact
1623
* `size`.
@@ -25,27 +32,37 @@
2532
* If `maxCharacterStrip` characters are walked back and no nice break point
2633
* is found, the bad break point is used.
2734
* Set `maxCharacterStrip: 0` to not find a nice break.
28-
* @property {Array<Content>} [ignore=[]]
35+
* @property {Array<Content> | null | undefined} [ignore=[]]
2936
* Nodes to exclude from the resulting tree.
3037
* These are not counted towards `size`.
3138
*/
3239

3340
import {unicodeWhitespace, unicodePunctuation} from 'micromark-util-character'
3441

42+
/** @type {ReadonlyArray<void>} */
43+
const empty = []
44+
3545
/**
3646
* Truncate the tree to a certain number of characters.
3747
*
3848
* @template {Node} Tree
3949
* @param {Tree} tree
40-
* @param {Options} [options]
50+
* @param {Options | null | undefined} [options]
4151
* @returns {Tree}
52+
* A shallow copy of `tree`, truncated.
4253
*/
43-
export function truncate(tree, options = {}) {
54+
export function truncate(tree, options) {
4455
// To do: support units.
45-
const {size = 140, ellipsis, maxCharacterStrip = 30, ignore = []} = options
56+
const config = options || {}
57+
const size = typeof config.size === 'number' ? config.size : 140
58+
const maxCharacterStrip =
59+
typeof config.maxCharacterStrip === 'number' ? config.maxCharacterStrip : 30
60+
const ignore = config.ignore || empty
61+
const ellipsis = config.ellipsis
4662
let searchSize = 0
47-
/** @type {Text|undefined} */
63+
/** @type {Text | undefined} */
4864
let overflowingText
65+
4966
const result = preorder(tree)
5067

5168
if (overflowingText) {
@@ -89,8 +106,12 @@ export function truncate(tree, options = {}) {
89106
return result
90107

91108
/**
109+
* Transform in `preorder`.
110+
*
92111
* @param {Node} node
93-
* @returns {Node|undefined}
112+
* Node to truncate.
113+
* @returns {Node}
114+
* Shallow copy of `node`.
94115
*/
95116
function preorder(node) {
96117
if (node.type === 'text') {
@@ -119,6 +140,7 @@ export function truncate(tree, options = {}) {
119140
if (result) children.push(result)
120141
}
121142

143+
// One of the descendant texts included the breakpoint.
122144
if (overflowingText) {
123145
break
124146
}
@@ -134,7 +156,9 @@ export function truncate(tree, options = {}) {
134156

135157
/**
136158
* @param {number} code
159+
* Character code.
137160
* @returns {boolean}
161+
* Whether `code` is not whitespace and not punctuation.
138162
*/
139163
function unicodeAlphanumeric(code) {
140164
return !unicodeWhitespace(code) && !unicodePunctuation(code)

0 commit comments

Comments
 (0)