Skip to content

Commit c19913e

Browse files
committed
refactor misc items
1 parent 8326aaf commit c19913e

File tree

6 files changed

+53
-44
lines changed

6 files changed

+53
-44
lines changed

dictionary/misc.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

misc/misc.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,18 @@ export function flattenError(error: unknown): ReadonlyArray<unknown> {
3434
export function throwError(error: unknown): never {
3535
throw error;
3636
}
37+
export function compound(
38+
elements: ReadonlyArray<string>,
39+
conjunction: string,
40+
repeat: boolean,
41+
): string {
42+
if (repeat || elements.length <= 2) {
43+
return elements.join(` ${conjunction} `);
44+
} else {
45+
const lastIndex = elements.length - 1;
46+
const init = elements.slice(0, lastIndex);
47+
const last = elements[lastIndex];
48+
const initText = init.map((item) => `${item},`).join(" ");
49+
return `${initText} ${conjunction} ${last}`;
50+
}
51+
}

src/main.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ if (typeof LIVE_RELOAD !== "undefined" && LIVE_RELOAD) {
1010
}
1111

1212
import { dictionary } from "../dictionary/dictionary.ts";
13-
import { asComment } from "../dictionary/misc.ts";
1413
import PROJECT_DATA from "../project_data.json" with { type: "json" };
1514
import { ArrayResultError, isArrayResult } from "./array_result.ts";
1615
import { loadCustomDictionary } from "./dictionary.ts";
@@ -293,13 +292,6 @@ function main(): void {
293292
}
294293
});
295294
}
296-
function extractErrorMessage(error: unknown): string {
297-
if (error instanceof Error) {
298-
return error.message;
299-
} else {
300-
return `${error}`;
301-
}
302-
}
303295

304296
if (document.readyState === "loading") {
305297
document.addEventListener("DOMContentLoaded", main);
@@ -315,3 +307,16 @@ const unused = [...new Array(localStorage.length).keys()]
315307
for (const key of unused) {
316308
localStorage.removeItem(key);
317309
}
310+
311+
function extractErrorMessage(error: unknown): string {
312+
if (error instanceof Error) {
313+
return error.message;
314+
} else {
315+
return `${error}`;
316+
}
317+
}
318+
export function asComment(text: string): string {
319+
return text
320+
.replaceAll(/^/mg, "# ")
321+
.replaceAll(/^#\s+$/mg, "#");
322+
}

src/parser/filter.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { extractArrayResultError } from "../array_result.ts";
2-
import { flattenError, throwError } from "../../misc/misc.ts";
2+
import { compound, flattenError, throwError } from "../../misc/misc.ts";
33
import { settings } from "../settings.ts";
44
import {
55
Clause,
@@ -150,10 +150,18 @@ export const MULTIPLE_MODIFIERS_RULES: ReadonlyArray<
150150
}
151151
});
152152
const duplicate = findDuplicate(words);
153-
if (duplicate == null) {
153+
if (duplicate.size === 0) {
154154
return true;
155155
} else {
156-
throw new UnrecognizedError(`duplicate "${duplicate}" in modifier`);
156+
const repeatConjunction = false;
157+
const list = compound(
158+
[...duplicate].map((word) => `"${word}"`),
159+
"and",
160+
repeatConjunction,
161+
);
162+
throw new UnrecognizedError(
163+
`duplicate ${list} in modifier`,
164+
);
157165
}
158166
}
159167
},
@@ -428,14 +436,15 @@ function phraseHasTopLevelEmphasis(phrase: Phrase): boolean {
428436
return phrase.emphasis != null;
429437
}
430438
}
431-
function findDuplicate<T>(iterable: Iterable<T>): null | T {
432-
const set = new Set();
439+
function findDuplicate<T>(iterable: Iterable<T>): Set<T> {
440+
const unique = new Set<T>();
441+
const duplicates = new Set<T>();
433442
for (const value of iterable) {
434-
if (set.has(value)) {
435-
return value;
443+
if (unique.has(value)) {
444+
duplicates.add(value);
436445
} else {
437-
set.add(value);
446+
unique.add(value);
438447
}
439448
}
440-
return null;
449+
return duplicates;
441450
}

src/translator/composer.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { nullableAsArray } from "../../misc/misc.ts";
1+
import { compound, nullableAsArray } from "../../misc/misc.ts";
22
import * as English from "./ast.ts";
33

44
const EMPHASIS_STARTING_TAG = "<strong>";
@@ -11,21 +11,6 @@ function word(word: English.Word): string {
1111
return word.word;
1212
}
1313
}
14-
function compound(
15-
elements: ReadonlyArray<string>,
16-
conjunction: string,
17-
depth: number,
18-
): string {
19-
if (depth !== 0 || elements.length <= 2) {
20-
return elements.join(` ${conjunction} `);
21-
} else {
22-
const lastIndex = elements.length - 1;
23-
const init = elements.slice(0, lastIndex);
24-
const last = elements[lastIndex];
25-
const initText = init.map((item) => `${item},`).join(" ");
26-
return `${initText} ${conjunction} ${last}`;
27-
}
28-
}
2914
export function noun(phrases: English.NounPhrase, depth: number): string {
3015
switch (phrases.type) {
3116
case "simple": {
@@ -44,7 +29,7 @@ export function noun(phrases: English.NounPhrase, depth: number): string {
4429
return compound(
4530
phrases.nouns.map((phrase) => noun(phrase, depth + 1)),
4631
phrases.conjunction,
47-
depth,
32+
depth !== 0,
4833
);
4934
}
5035
}
@@ -62,7 +47,7 @@ export function adjective(
6247
text = compound(
6348
phrases.adjective.map((phrase) => adjective(phrase, depth + 1)),
6449
phrases.conjunction,
65-
depth,
50+
depth !== 0,
6651
);
6752
}
6853
return word({ word: text, emphasis: phrases.emphasis });
@@ -111,7 +96,7 @@ export function verb(phrase: English.VerbPhrase, depth: number): string {
11196
text = compound(
11297
phrase.verbs.map((item) => verb(item, depth + 1)),
11398
phrase.conjunction,
114-
depth,
99+
depth !== 0,
115100
);
116101
}
117102
return [

src/translator/determiner.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export function fixDeterminer(
140140
function prettyPrintDeterminers(
141141
determiners: ReadonlyArray<English.Determiner>,
142142
): string {
143-
return `(${determiners.map(({ determiner }) => determiner).join(` `)})`;
143+
return `(${determiners.map(({ determiner }) => determiner).join(" ")})`;
144144
}
145145
function encodeDeterminer(
146146
strings: TemplateStringsArray,
@@ -152,7 +152,7 @@ function encodeDeterminer(
152152
.join("");
153153
}
154154
function filterSet<T>(
155-
set: Iterable<readonly [condition: boolean, value: T]>,
155+
set: ReadonlyArray<readonly [condition: boolean, value: T]>,
156156
): ReadonlyArray<T> {
157-
return [...set].filter(([condition]) => condition).map(([_, value]) => value);
157+
return set.filter(([condition]) => condition).map(([_, value]) => value);
158158
}

0 commit comments

Comments
 (0)