Skip to content

Commit 38b8adf

Browse files
authored
Improve test coverage (#1994)
* refactor type & logic * re-define `PlainNodeParser` as `TerminateNodeParser` * refactor type * fix logic * refactor
1 parent ce444b6 commit 38b8adf

File tree

10 files changed

+1624
-44
lines changed

10 files changed

+1624
-44
lines changed

lcov.info

Lines changed: 1572 additions & 0 deletions
Large diffs are not rendered by default.

src/block/CodeBlock.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Row } from "./Row.ts";
22

33
export interface CodeBlockPack {
44
type: "codeBlock";
5-
rows: Row[];
5+
rows: [Row, ...Row[]];
66
}
77

88
/**
@@ -17,9 +17,8 @@ export interface CodeBlock {
1717

1818
export const convertToCodeBlock = (pack: CodeBlockPack): CodeBlock => {
1919
const {
20-
rows: [head, ...body],
20+
rows: [{ indent, text }, ...body],
2121
} = pack;
22-
const { indent = 0, text = "" } = head ?? {};
2322
const fileName: string = text.replace(/^\s*code:/, "");
2423

2524
return {

src/block/Pack.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,35 @@ import type { TitlePack } from "./Title.ts";
77

88
export type Pack = TitlePack | CodeBlockPack | TablePack | LinePack;
99

10-
const isChildRowOfPack = (pack: Pack, row: Row): boolean =>
11-
(pack.type === "codeBlock" || pack.type === "table") &&
12-
row.indent > (pack.rows[0]?.indent ?? 0);
10+
const isChildRowOfPack = (
11+
{ type, rows: [firstRow] }: Pack,
12+
row: Row,
13+
): boolean =>
14+
(type === "codeBlock" || type === "table") && row.indent > firstRow.indent;
1315

1416
const packing = (packs: Pack[], row: Row): Pack[] => {
1517
const lastPack = packs[packs.length - 1];
1618
if (lastPack !== undefined && isChildRowOfPack(lastPack, row)) {
1719
lastPack.rows.push(row);
18-
return packs;
20+
} else {
21+
packs.push({
22+
type: /^\s*code:/.test(row.text)
23+
? "codeBlock"
24+
: /^\s*table:/.test(row.text)
25+
? "table"
26+
: "line",
27+
rows: [row],
28+
});
1929
}
2030

21-
packs.push({
22-
type: /^\s*code:/.test(row.text)
23-
? "codeBlock"
24-
: /^\s*table:/.test(row.text)
25-
? "table"
26-
: "line",
27-
rows: [row],
28-
});
29-
3031
return packs;
3132
};
3233

33-
export const packRows = (rows: Row[], opts: ParserOption): Pack[] => {
34-
if (opts.hasTitle ?? true) {
34+
export const packRows = (
35+
rows: Row[],
36+
{ hasTitle = true }: ParserOption,
37+
): Pack[] => {
38+
if (hasTitle) {
3539
const [title, ...body] = rows;
3640
if (title === undefined) return [];
3741
return [

src/block/Row.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export interface Row {
55

66
export const parseToRows = (input: string): Row[] =>
77
input.split("\n").map((text) => ({
8-
indent: /^\s+/.exec(text)?.[0]?.length ?? 0,
8+
indent: /^\s+/.exec(text)?.[0].length ?? 0,
99
text,
1010
}));

src/block/Table.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Row } from "./Row.ts";
44

55
export interface TablePack {
66
type: "table";
7-
rows: Row[];
7+
rows: [Row, ...Row[]];
88
}
99

1010
/**
@@ -19,9 +19,8 @@ export interface Table {
1919

2020
export const convertToTable = (pack: TablePack): Table => {
2121
const {
22-
rows: [head, ...body],
22+
rows: [{ indent, text }, ...body],
2323
} = pack;
24-
const { indent = 0, text = "" } = head ?? {};
2524
const fileName = text.replace(/^\s*table:/, "");
2625

2726
return {

src/block/node/ExternalLinkNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const createExternalLinkNode: NodeCreator<LinkNode | PlainNode> = (
2626
const match = (
2727
isHrefFirst ? /^https?:\/\/[^\s\]]+/ : /https?:\/\/[^\s\]]+$/
2828
).exec(inner);
29-
if (match?.[0] === undefined) return [];
29+
if (match === null) return [];
3030

3131
const content = isHrefFirst
3232
? inner.substring(match[0].length)

src/block/node/PlainNode.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { NodeCreator } from "./creator.ts";
2-
import { createNodeParser } from "./creator.ts";
3-
import type { NodeParser } from "./index.ts";
2+
import type { TerminateNodeParser } from "./index.ts";
43
import type { PlainNode } from "./type.ts";
54

65
export const createPlainNode: NodeCreator<PlainNode> = (raw) => [
@@ -11,8 +10,4 @@ export const createPlainNode: NodeCreator<PlainNode> = (raw) => [
1110
},
1211
];
1312

14-
export const PlainNodeParser: NodeParser = createNodeParser(createPlainNode, {
15-
parseOnNested: true,
16-
parseOnQuoted: true,
17-
patterns: [/^()(.*)()$/],
18-
});
13+
export const PlainNodeParser: TerminateNodeParser = createPlainNode;

src/block/node/creator.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,40 @@ export type NodeCreator<T extends Node> = (
77
opts: NodeParserOption,
88
) => T[];
99

10+
type NodeParserCreatorOptions = {
11+
parseOnNested: boolean;
12+
parseOnQuoted: boolean;
13+
patterns: RegExp[];
14+
};
15+
1016
type NodeParserCreator<T extends Node> = (
1117
nodeCreator: NodeCreator<T>,
12-
opts: { parseOnNested: boolean; parseOnQuoted: boolean; patterns: RegExp[] },
18+
opts: NodeParserCreatorOptions,
1319
) => NodeParser;
1420

1521
export const createNodeParser: NodeParserCreator<Node> = (
1622
nodeCreator,
1723
{ parseOnNested, parseOnQuoted, patterns },
1824
) => {
1925
return (text, opts, next) => {
20-
if (!parseOnNested && opts.nested) return next?.() ?? [];
21-
if (!parseOnQuoted && opts.quoted) return next?.() ?? [];
26+
if (!parseOnNested && opts.nested) return next();
27+
if (!parseOnQuoted && opts.quoted) return next();
2228

2329
for (const pattern of patterns) {
2430
const match = pattern.exec(text);
2531
if (match === null) continue;
2632

2733
const left = text.substring(0, match.index);
28-
const right = text.substring(match.index + (match[0]?.length ?? 0));
34+
const right = text.substring(match.index + match[0].length);
2935

30-
const node = nodeCreator(match[0] ?? "", opts);
36+
const node = nodeCreator(match[0], opts);
3137
return [
3238
...convertToNodes(left, opts),
3339
...node,
3440
...convertToNodes(right, opts),
3541
];
3642
}
3743

38-
return next?.() ?? [];
44+
return next();
3945
};
4046
};

src/block/node/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ export type NextNodeParser = () => Node[];
2828
export type NodeParser = (
2929
text: string,
3030
opts: NodeParserOption,
31-
next?: NextNodeParser,
31+
next: NextNodeParser,
32+
) => Node[];
33+
export type TerminateNodeParser = (
34+
text: string,
35+
opts: NodeParserOption,
3236
) => Node[];
3337

34-
const FalsyEliminator: NodeParser = (text, _, next) => {
35-
if (text === "") return [];
36-
return next?.() ?? [];
37-
};
38+
const FalsyEliminator: NodeParser = (text, _, next) =>
39+
text !== "" ? next() : [];
3840

3941
const combineNodeParsers =
4042
(...parsers: NodeParser[]) =>

src/parse.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ export type Page = Block[];
2424
* @param opts parser options
2525
* @returns syntax tree of parsed input
2626
*/
27-
export const parse = (input: string, opts?: ParserOption): Page => {
27+
export const parse = (
28+
input: string,
29+
{ hasTitle = true }: ParserOption = {},
30+
): Page => {
2831
const rows = parseToRows(input);
29-
const packs = packRows(rows, { hasTitle: opts?.hasTitle ?? true });
32+
const packs = packRows(rows, { hasTitle });
3033
return packs.map(convertToBlock);
3134
};
3235

@@ -37,5 +40,5 @@ export const parse = (input: string, opts?: ParserOption): Page => {
3740
*/
3841
export const getTitle = (input: string): string => {
3942
const match = /^\s*\S.*$/m.exec(input);
40-
return match?.[0]?.trim() ?? "Untitled";
43+
return match?.[0].trim() ?? "Untitled";
4144
};

0 commit comments

Comments
 (0)