Skip to content

Commit 76a07db

Browse files
committed
WebStorm is 2 smart 5 me
1 parent f4da943 commit 76a07db

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/.vscode/
2-
/src/dictionary/custom_dictionary.ts
2+
/src/dictionary/dictionary.ts
33
/dist/main.js
44
/dist/main.js.map
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { dictionary as globalDictionary } from "./custom_dictionary.ts";
2+
import { Definition, Dictionary } from "./type.ts";
3+
4+
// all of these global constants are mutable
5+
6+
const customDictionary: Dictionary = new Map();
7+
export const dictionary: Dictionary = new Map();
8+
9+
export const contentWordSet: Set<string> = new Set();
10+
export const prepositionSet: Set<string> = new Set();
11+
export const preverbSet: Set<string> = new Set();
12+
export const fillerSet: Set<string> = new Set();
13+
export const numeralSet: Set<string> = new Set();
14+
export const tokiPonaWordSet: Set<string> = new Set();
15+
16+
update();
17+
18+
export function loadCustomDictionary(dictionary: Dictionary): void {
19+
customDictionary.clear();
20+
for (const [key, value] of dictionary) {
21+
customDictionary.set(key, value);
22+
}
23+
update();
24+
}
25+
function update() {
26+
dictionary.clear();
27+
const words = new Set([
28+
...globalDictionary.keys(),
29+
...customDictionary.keys(),
30+
]);
31+
for (const word of words) {
32+
const entry = customDictionary.get(word) ?? globalDictionary.get(word)!;
33+
if (entry.definitions.length > 0) {
34+
dictionary.set(word, entry);
35+
}
36+
}
37+
redefineSet(
38+
contentWordSet,
39+
({ type }) => !["filler", "particle definition"].includes(type),
40+
);
41+
redefineSetWithType(prepositionSet, "preposition");
42+
redefineSet(
43+
preverbSet,
44+
(definition) =>
45+
(definition.type === "verb" && definition.predicateType != null) ||
46+
definition.type === "modal verb",
47+
);
48+
redefineSetWithType(fillerSet, "filler");
49+
redefineSetWithType(numeralSet, "numeral");
50+
redefineSet(tokiPonaWordSet, () => true);
51+
}
52+
function redefineSet(
53+
set: Set<string>,
54+
filter: (definition: Definition) => boolean,
55+
) {
56+
set.clear();
57+
for (const [word, { definitions }] of dictionary) {
58+
if (definitions.some(filter)) {
59+
set.add(word);
60+
}
61+
}
62+
}
63+
function redefineSetWithType(set: Set<string>, type: Definition["type"]) {
64+
redefineSet(set, ({ type: compareType }) => compareType === type);
65+
}

0 commit comments

Comments
 (0)