-
Notifications
You must be signed in to change notification settings - Fork 3
/
text.ts
33 lines (30 loc) · 865 Bytes
/
text.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { isString } from "@core/unknownutil/is/string";
/** インデント数を数える */
export const getIndentCount = (text: string): number =>
text.match(/^(\s*)/)?.[1]?.length ?? 0;
/** 指定した行の配下にある行の数を返す
*
* @param index 指定したい行の行番号
* @param lines 行のリスト
*/
export const getIndentLineCount = (
index: number,
lines: readonly string[] | readonly { text: string }[],
): number => {
const base = getIndentCount(getText(index, lines));
let count = 0;
while (
index + count + 1 < lines.length &&
(getIndentCount(getText(index + count + 1, lines))) > base
) {
count++;
}
return count;
};
const getText = (
index: number,
lines: readonly string[] | readonly { text: string }[],
) => {
const line = lines[index];
return isString(line) ? line : line.text;
};