Function
findBracketObject
(
text: string,
offset: number,
open: string,
close: string,
isInner: boolean,
)
Source from the content-addressed store, hash-verified
| 147 | } |
| 148 | |
| 149 | function findBracketObject( |
| 150 | text: string, |
| 151 | offset: number, |
| 152 | open: string, |
| 153 | close: string, |
| 154 | isInner: boolean, |
| 155 | ): TextObjectRange { |
| 156 | let depth = 0 |
| 157 | let start = -1 |
| 158 | |
| 159 | for (let i = offset; i >= 0; i--) { |
| 160 | if (text[i] === close && i !== offset) depth++ |
| 161 | else if (text[i] === open) { |
| 162 | if (depth === 0) { |
| 163 | start = i |
| 164 | break |
| 165 | } |
| 166 | depth-- |
| 167 | } |
| 168 | } |
| 169 | if (start === -1) return null |
| 170 | |
| 171 | depth = 0 |
| 172 | let end = -1 |
| 173 | for (let i = start + 1; i < text.length; i++) { |
| 174 | if (text[i] === open) depth++ |
| 175 | else if (text[i] === close) { |
| 176 | if (depth === 0) { |
| 177 | end = i |
| 178 | break |
| 179 | } |
| 180 | depth-- |
| 181 | } |
| 182 | } |
| 183 | if (end === -1) return null |
| 184 | |
| 185 | return isInner ? { start: start + 1, end } : { start, end: end + 1 } |
| 186 | } |
| 187 | |
Tested by
no test coverage detected