Skip to content

Commit 70b15e6

Browse files
committed
Refactor code-style
* Add more docs to JSDoc * Refactor a bunch of code
1 parent ed1ad8e commit 70b15e6

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

lib/index.js

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
* @typedef {import('unist').Parent} UnistParent
33
* @typedef {import('nlcst').Root} Root
44
* @typedef {import('nlcst').Content} Content
5-
* @typedef {Root|Content} Node
5+
*/
6+
7+
/**
8+
* @typedef {Root | Content} Node
69
* @typedef {Extract<Node, UnistParent>} Parent
710
*/
811

@@ -50,19 +53,25 @@ const pairs = {
5053
const open = Object.keys(pairs)
5154

5255
/**
53-
* Check if the node in `parent` at `position` is enclosed by matching
56+
* Check if the child in `parent` at `index` is enclosed by matching
5457
* delimiters.
5558
*
56-
* @param {Parent} parent
57-
* @param {number} index
59+
* @template {Parent} ParentType
60+
* Parent node.
61+
* @param {ParentType} parent
62+
* Parent node.
63+
* @param {number | ParentType['children'][number]} index
64+
* Child node of parent or index of child in parent.
5865
* @returns {boolean}
66+
* Whether the child is a literal.
5967
*/
6068
export function isLiteral(parent, index) {
6169
if (!(parent && parent.children)) {
6270
throw new Error('Parent must be a node')
6371
}
6472

6573
if (index !== null && typeof index === 'object' && 'type' in index) {
74+
// @ts-expect-error: assume child of `parent`.
6675
index = parent.children.indexOf(index)
6776

6877
if (index === -1) {
@@ -86,27 +95,41 @@ export function isLiteral(parent, index) {
8695
/**
8796
* Check if the node in `parent` at `position` is enclosed by matching
8897
* delimiters.
98+
*
8999
* @param {Parent} parent
100+
* Parent node.
90101
* @param {number} position
91-
* @returns {Node|void}
102+
* Position to look around.
103+
* @returns {boolean}
104+
* Whether a child is wrapped.
92105
*/
93106
function isWrapped(parent, position) {
94107
const previous = siblingDelimiter(parent, position, -1, open)
95108

96109
if (previous) {
97-
return siblingDelimiter(parent, position, 1, pairs[toString(previous)])
110+
return (
111+
siblingDelimiter(parent, position, 1, pairs[toString(previous)]) !==
112+
undefined
113+
)
98114
}
115+
116+
return false
99117
}
100118

101119
/**
102120
* Find the previous or next delimiter before or after `position` in `parent`.
103121
* Returns the delimiter node when found.
104122
*
105123
* @param {Parent} parent
124+
* Parent node.
106125
* @param {number} position
126+
* Start position in `parent`.
107127
* @param {number} step
128+
* Step (`-1` to move back, `1` to move forward).
108129
* @param {Array<string>} delimiters
109-
* @returns {Node|void}
130+
* Delimiters to look for.
131+
* @returns {Node | void}
132+
* Delimiter, if found.
110133
*/
111134
function siblingDelimiter(parent, position, step, delimiters) {
112135
let index = position + step
@@ -115,7 +138,7 @@ function siblingDelimiter(parent, position, step, delimiters) {
115138
const sibling = parent.children[index]
116139

117140
if (sibling.type === 'WordNode' || sibling.type === 'SourceNode') {
118-
return
141+
break
119142
}
120143

121144
if (sibling.type !== 'WhiteSpaceNode') {
@@ -127,17 +150,24 @@ function siblingDelimiter(parent, position, step, delimiters) {
127150
}
128151

129152
/**
130-
* Check if parent contains word-nodes between `start` and `end` (both
153+
* Check if parent contains word nodes between `start` and `end` (both
131154
* excluding).
155+
*
132156
* @param {Parent} parent
157+
* Parent node.
133158
* @param {number} start
159+
* Start index in `parent` (excluding).
134160
* @param {number} end
135-
* @returns {boolean|void}
161+
* End index in `parent` (excluding).
162+
* @returns {boolean}
163+
* Whether a child contains a word.
136164
*/
137165
function containsWord(parent, start, end) {
138166
while (++start < end) {
139167
if (parent.children[start].type === 'WordNode') {
140168
return true
141169
}
142170
}
171+
172+
return false
143173
}

0 commit comments

Comments
 (0)