Skip to content

Commit f73a6b9

Browse files
committed
refactor: the Output class
1 parent 44a3bad commit f73a6b9

File tree

3 files changed

+110
-4
lines changed

3 files changed

+110
-4
lines changed
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import test from "ava";
2+
import { Output } from "./output.ts";
3+
4+
test("append", (t) => {
5+
const output = new Output(100);
6+
t.is(String(output), "");
7+
8+
output.append("1");
9+
t.is(String(output), "1");
10+
11+
output.separate(",");
12+
t.is(String(output), "1");
13+
14+
output.append("2");
15+
t.is(String(output), "1,2");
16+
17+
output.append("3");
18+
t.is(String(output), "1,23");
19+
20+
output.separate("");
21+
output.append("4");
22+
t.is(String(output), "1,234");
23+
24+
output.separate(";");
25+
output.append("");
26+
t.is(String(output), "1,234");
27+
28+
output.append("5");
29+
t.is(String(output), "1,234;5");
30+
});
31+
32+
test("limits", (t) => {
33+
const output = new Output(3);
34+
t.is(output.length, 0);
35+
t.is(output.remaining, 3);
36+
t.is(String(output), "");
37+
38+
output.append("1");
39+
t.is(output.length, 1);
40+
t.is(output.remaining, 2);
41+
t.is(String(output), "1");
42+
43+
t.throws(
44+
() => {
45+
output.append("234");
46+
},
47+
{ is: Output.Stop },
48+
);
49+
t.is(output.length, 1);
50+
t.is(output.remaining, 2);
51+
t.is(String(output), "1");
52+
53+
t.throws(
54+
() => {
55+
output.separate(",,,");
56+
},
57+
{ is: Output.Stop },
58+
);
59+
t.is(output.length, 1);
60+
t.is(output.remaining, 2);
61+
t.is(String(output), "1");
62+
63+
output.separate(",");
64+
t.is(output.length, 2);
65+
t.is(output.remaining, 1);
66+
t.is(String(output), "1");
67+
68+
t.throws(
69+
() => {
70+
output.append("23");
71+
},
72+
{ is: Output.Stop },
73+
);
74+
t.is(output.length, 2);
75+
t.is(output.remaining, 1);
76+
t.is(String(output), "1");
77+
78+
output.append("2");
79+
t.is(output.length, 3);
80+
t.is(output.remaining, 0);
81+
t.is(String(output), "1,2");
82+
83+
output.clear();
84+
t.is(output.length, 0);
85+
t.is(output.remaining, 3);
86+
t.is(String(output), "");
87+
});

packages/keybr-code/lib/output.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export class Output {
44
readonly #limit: number;
55
readonly #data: string[] = [];
66
#length: number = 0;
7+
#separator: string = "";
78
#text: string | null = null;
89

910
constructor(limit: number = 1000) {
@@ -12,21 +13,38 @@ export class Output {
1213

1314
/** Returns the number of characters accumulated in the output. */
1415
get length(): number {
15-
return this.#length;
16+
return this.#length + this.#separator.length;
1617
}
1718

1819
/** Returns the number of remaining characters before the limit is reached. */
1920
get remaining(): number {
20-
return this.#limit - this.#length;
21+
return this.#limit - (this.#length + this.#separator.length);
22+
}
23+
24+
/** Appends the given text, but only if followed by the `append(...)` method. */
25+
separate(text: string): this {
26+
const { length } = text;
27+
if (length > 0) {
28+
if (this.#length + length > this.#limit) {
29+
throw Output.Stop;
30+
}
31+
}
32+
this.#separator = text;
33+
return this;
2134
}
2235

2336
/** Appends the given text, or throws the stop error if the limit is reached. */
2437
append(text: string): this {
2538
const { length } = text;
2639
if (length > 0) {
27-
if (this.#length + length > this.#limit) {
40+
if (this.#length + this.#separator.length + length > this.#limit) {
2841
throw Output.Stop;
2942
}
43+
if (this.#separator.length > 0) {
44+
this.#data.push(this.#separator);
45+
this.#length += this.#separator.length;
46+
this.#separator = "";
47+
}
3048
this.#data.push(text);
3149
this.#length += length;
3250
this.#text = null;
@@ -38,6 +56,7 @@ export class Output {
3856
clear(): this {
3957
this.#data.length = 0;
4058
this.#length = 0;
59+
this.#separator = "";
4160
this.#text = null;
4261
return this;
4362
}

packages/keybr-code/lib/syntax.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class Syntax implements EnumItem {
7676
while (true) {
7777
try {
7878
if (output.length > 0) {
79-
output.append(" ");
79+
output.separate(" ");
8080
}
8181
generate(this.rules, this.start, { output, rng });
8282
} catch (err) {

0 commit comments

Comments
 (0)