@fpw/en-wiktionary-la-modules 0.0.10 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/LaEngine.js +6 -6
  2. package/dist/LaEngine.js.map +1 -1
  3. package/dist/index.js +5 -1
  4. package/dist/index.js.map +1 -1
  5. package/dist/modules/conjugation/LaVerb.d.ts +1 -1
  6. package/dist/modules/conjugation/LaVerb.js +379 -375
  7. package/dist/modules/conjugation/LaVerb.js.map +1 -1
  8. package/dist/modules/conjugation/VerbForm.js +2 -2
  9. package/dist/modules/conjugation/VerbForm.js.map +1 -1
  10. package/dist/modules/conjugation/VerbType.d.ts +1 -0
  11. package/dist/modules/conjugation/VerbType.js +3 -2
  12. package/dist/modules/conjugation/VerbType.js.map +1 -1
  13. package/dist/modules/declination/LaAdjData.d.ts +1 -1
  14. package/dist/modules/declination/LaAdjData.js +505 -428
  15. package/dist/modules/declination/LaAdjData.js.map +1 -1
  16. package/dist/modules/declination/LaNominal.d.ts +10 -0
  17. package/dist/modules/declination/LaNominal.js +79 -73
  18. package/dist/modules/declination/LaNominal.js.map +1 -1
  19. package/dist/modules/declination/LaNounData.js +467 -417
  20. package/dist/modules/declination/LaNounData.js.map +1 -1
  21. package/dist/modules/declination/NominalForm.js +2 -2
  22. package/dist/modules/declination/NominalForm.js.map +1 -1
  23. package/dist/modules/headword/HeadwordParser.js +17 -17
  24. package/dist/modules/headword/HeadwordParser.js.map +1 -1
  25. package/package.json +9 -8
  26. package/src/LaEngine.ts +82 -0
  27. package/src/index.ts +16 -0
  28. package/src/modules/common.ts +164 -0
  29. package/src/modules/conjugation/LaVerb.ts +2490 -0
  30. package/src/modules/conjugation/VerbAffix.ts +18 -0
  31. package/src/modules/conjugation/VerbForm.ts +223 -0
  32. package/src/modules/conjugation/VerbType.ts +51 -0
  33. package/src/modules/declination/LaAdjData.ts +913 -0
  34. package/src/modules/declination/LaNominal.ts +1992 -0
  35. package/src/modules/declination/LaNounData.ts +896 -0
  36. package/src/modules/declination/LaPersonalPronoun.ts +77 -0
  37. package/src/modules/declination/NominalForm.ts +89 -0
  38. package/src/modules/headword/HeadWord.ts +132 -0
  39. package/src/modules/headword/HeadwordParser.ts +514 -0
  40. package/dist/modules/LaEngine.d.ts +0 -15
  41. package/dist/modules/LaEngine.js +0 -46
  42. package/dist/modules/LaEngine.js.map +0 -1
  43. package/dist/modules/LaWiktionary.d.ts +0 -18
  44. package/dist/modules/LaWiktionary.js +0 -171
  45. package/dist/modules/LaWiktionary.js.map +0 -1
@@ -0,0 +1,164 @@
1
+ export type ArgMap = Map<string, string>;
2
+ export type FormMap<T> = Map<T, string[]>;
3
+
4
+ /**
5
+ * Parse a MediaWiki template string like {{a|b|c=d}} into a key-value map.
6
+ * Unnamed parameters will use number strings starting at '0'.
7
+ * @param desc the MediaWiki template string
8
+ * @returns a map of keys and values
9
+ */
10
+ export function parse_template(desc: string): ArgMap {
11
+ // remove {{ }}
12
+ const paramStr = desc
13
+ .replace(/[\r\n]/g, "")
14
+ .replace(/^{{(.*)}}$/, "$1");
15
+
16
+ const partStr = remove_links(paramStr);
17
+
18
+ const parts = partStr.split("|");
19
+
20
+ const res = new Map<string, string>();
21
+ let i = 0;
22
+ for (const part of parts) {
23
+ if (part.includes("=")) {
24
+ const [key, value] = part.split("=");
25
+ res.set(key, value);
26
+ } else {
27
+ res.set(`${i++}`, part);
28
+ }
29
+ }
30
+
31
+ return res;
32
+ }
33
+
34
+ /**
35
+ * Read a parameter list from a MediaWiki template string.
36
+ * Example: {{...|p=a,p2=b,p3=c}} will become [a, b, c] if p is read
37
+ * @param args a parsed MediaWiki template string
38
+ * @param elem the name of the list
39
+ * @returns elemnts of the lists
40
+ */
41
+ export function read_list(args: ArgMap, elem: string): string[] {
42
+ const entries: string[] = [];
43
+
44
+ for (const [key, value] of args.entries()) {
45
+ const match = key.match(new RegExp(`^${elem}([0-9]*)$`));
46
+ if (!match) {
47
+ continue;
48
+ }
49
+ let idx = 0;
50
+ if (match[1]) {
51
+ idx = Number.parseInt(match[1], 10) - 1;
52
+ }
53
+ entries[idx] = value;
54
+ }
55
+
56
+ return entries.filter(x => x !== undefined);
57
+ }
58
+
59
+ /**
60
+ * Removes MediaWiki links by replacing them with their displayed value.
61
+ * Example [[a|title]] will be replaced with 'title'
62
+ * @param str a string that can contain MediaWiki links
63
+ * @returns the string with all links replaced by their title
64
+ */
65
+ export function remove_links(str: string): string {
66
+ // flatten links [[a|b]] to [[b]]
67
+ const norm1 = str.replace(/\[\[[^\]]*?\|([^\]]*?)\]\]/g, "[[$1]]");
68
+
69
+ // replace [[b]] with b
70
+ const norm2 = norm1.replace(/\[\[([^\]]*?)\]\]/g, "$1");
71
+
72
+ return norm2;
73
+ }
74
+
75
+ /**
76
+ * Very basic HTML remover to remove simple, non-nested tags from a string.
77
+ * @param str a string containing simple HTML markup
78
+ * @returns a string with HTML tags removed
79
+ */
80
+ export function remove_html(str: string): string {
81
+ return str.replace(/<.*?>(.*?)<\/.*?>/g, "$1");
82
+ }
83
+
84
+ /**
85
+ * Removes macrons from a string.
86
+ * Example: 'Āfrica' will become 'Africa'
87
+ * @param str a string containing macrons
88
+ * @returns string without macrons
89
+ */
90
+ export function strip_macrons(str: string): string {
91
+ return str
92
+ .normalize("NFD")
93
+ .replace(/\u0304/g, "")
94
+ .normalize("NFC");
95
+ }
96
+
97
+ /**
98
+ * Removes all diacritical parts from a string except for macrons.
99
+ * Example: 'Thēse͡us' will become 'Thēseus'
100
+ * @param str string containing diactricial characters
101
+ * @returns string with all diactricial marks except macrons removed
102
+ */
103
+ export function remove_diacritical(str: string): string {
104
+ const noDias = str
105
+ .normalize("NFD")
106
+ .replace(/[\u0300-\u0303\u0305-\u036F]/g, "")
107
+ .normalize("NFC");
108
+
109
+ return noDias;
110
+ }
111
+
112
+ /**
113
+ * Given a word and an ending, returns the base part of the word excluding the ending.
114
+ * The given ending can be a RegEx with a capture group - in that case, the first group will be returned.
115
+ * @param lemma word to extract the base from
116
+ * @param ending ending to remove, can be a RegEx
117
+ * @returns
118
+ */
119
+ export function extract_base(lemma: string, ending: string): string | undefined {
120
+ let regex: string;
121
+ if (ending.includes("(")) {
122
+ regex = ending;
123
+ } else {
124
+ regex = `^(.*)${ending}$`;
125
+ }
126
+ const match = lemma.match(new RegExp(regex));
127
+ if (match) {
128
+ return match[1];
129
+ }
130
+ return undefined;
131
+ }
132
+
133
+ /**
134
+ * Checks if two one-dimensional arrays are equal.
135
+ * @param a array a
136
+ * @param b array b
137
+ * @returns true if the arrays are equal
138
+ */
139
+ export function array_equals(a: string[] | undefined, b: string[] | undefined): boolean {
140
+ if ((!a && b) || (a && !b)) {
141
+ return false;
142
+ }
143
+ if (!a || !b) {
144
+ return true;
145
+ }
146
+
147
+ return (a.length == b.length) && a.every((x, i) => b[i] == x);
148
+ }
149
+
150
+ /**
151
+ * Check if a value is a value of a given enum
152
+ * @param enumType pass the enum type as value
153
+ * @param val the value to check
154
+ * @returns whether the value is part of the enum values
155
+ */
156
+ export function is_enum_value<E>(enumType: E, val: any) : val is E[keyof E] {
157
+ for (const key in enumType) {
158
+ if (enumType[key] === val ) {
159
+ return true;
160
+ }
161
+ }
162
+
163
+ return false;
164
+ }