@taekwondo-db/database 0.0.0-stage → 0.2.2
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.
- package/LICENSE +21 -0
- package/README.md +282 -2
- package/db/chagi.json +720 -0
- package/db/chigi.json +1153 -0
- package/db/ditgi.json +101 -0
- package/db/japgi.json +154 -0
- package/db/jireugi.json +458 -0
- package/db/jjireugi.json +111 -0
- package/db/kkeokgi.json +58 -0
- package/db/makgi.json +1447 -0
- package/db/milgi.json +63 -0
- package/db/neomgigi.json +45 -0
- package/db/pihagi.json +68 -0
- package/db/ppaegi.json +174 -0
- package/db/schemas/grades.json +22 -0
- package/db/schemas/historic.json +21 -0
- package/db/schemas/image.json +24 -0
- package/db/schemas/include.json +31 -0
- package/db/schemas/localized-string.json +17 -0
- package/db/schemas/patches.json +12 -0
- package/db/schemas/source.json +12 -0
- package/db/schemas/sources.json +22 -0
- package/db/schemas/techniques.json +86 -0
- package/db/schemas/terms.json +52 -0
- package/db/seogi.json +602 -0
- package/db/sources.json +29 -0
- package/db/terms.json +995 -0
- package/db/ttwigi.json +59 -0
- package/dist/search.browser.js +1197 -0
- package/index.js +1 -0
- package/package.json +50 -5
- package/src/builtins-empty.d.ts +1 -0
- package/src/builtins-empty.js +10 -0
- package/src/builtins.d.ts +10 -0
- package/src/builtins.js +46 -0
- package/src/compose-builtins.d.ts +11 -0
- package/src/compose-builtins.js +39 -0
- package/src/compose.d.ts +76 -0
- package/src/compose.js +313 -0
- package/src/database.ts +457 -0
- package/src/description.d.ts +13 -0
- package/src/description.js +63 -0
- package/src/grade.d.ts +22 -0
- package/src/grade.js +44 -0
- package/src/search.d.ts +31 -0
- package/src/search.js +59 -0
- package/types/historic.d.ts +13 -0
- package/types/image.d.ts +9 -0
- package/types/localized-string.d.ts +3 -0
- package/types/source.d.ts +34 -0
- package/types/technique.d.ts +114 -0
- package/types/term.d.ts +38 -0
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { builtinTechniqueGroups, TaekwondoDatabase } from "./src/database.ts";
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"dependencies": { "fuse.js": "^7.5.0" },
|
|
3
|
+
"devDependencies": {
|
|
4
|
+
"ajv": "^8.20.0",
|
|
5
|
+
"json-schema-to-typescript": "^16.0.0",
|
|
6
|
+
"vite": "^8.3.8",
|
|
7
|
+
"vitest": "^5.1.1"
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./src/database.ts",
|
|
11
|
+
"./builtins": "./src/builtins.js",
|
|
12
|
+
"./builtins-empty": "./src/builtins-empty.js",
|
|
13
|
+
"./compose": "./src/compose-builtins.js",
|
|
14
|
+
"./db/*": "./db/*",
|
|
15
|
+
"./img/*": "./img/*",
|
|
16
|
+
"./package.json": "./package.json",
|
|
17
|
+
"./search": "./src/search.js",
|
|
18
|
+
"./search.browser.js": "./dist/search.browser.js",
|
|
19
|
+
"./types/*": "./types/*.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"db",
|
|
23
|
+
"dist",
|
|
24
|
+
"img",
|
|
25
|
+
"src",
|
|
26
|
+
"types",
|
|
27
|
+
"index.js",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"license": "MIT AND CC-BY-4.0",
|
|
32
|
+
"main": "./src/database.ts",
|
|
33
|
+
"name": "@taekwondo-db/database",
|
|
34
|
+
"publishConfig": { "access": "public" },
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://gitlab.com/taekwondo-db/database.git"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "vite build -c vite.search.config.js",
|
|
41
|
+
"fix": "biome check . --write",
|
|
42
|
+
"lint": "biome check .",
|
|
43
|
+
"prepack": "npm run build",
|
|
44
|
+
"test": "vitest",
|
|
45
|
+
"typecheck": "tsc -p .",
|
|
46
|
+
"validate": "node script/validate-schema.js"
|
|
47
|
+
},
|
|
48
|
+
"type": "module",
|
|
49
|
+
"types": "./src/database.ts",
|
|
50
|
+
"version": "0.2.2"
|
|
51
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./builtins.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drop-in replacement for `./builtins` with no data.
|
|
3
|
+
*
|
|
4
|
+
* `defineAppConfig({ db: { includeBuiltins: false } })` in the web app aliases
|
|
5
|
+
* `@taekwondo-db/database/builtins` here, which keeps the roughly 140 kB of
|
|
6
|
+
* bundled Kukkiwon JSON out of a derived app's bundle.
|
|
7
|
+
*/
|
|
8
|
+
export const builtinTechniqueGroups = {};
|
|
9
|
+
export const builtinTerms = {};
|
|
10
|
+
export const builtinSources = {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { SourcesJson } from "../types/source";
|
|
2
|
+
import type { BuiltinTechniqueGroup, TechniqueJson } from "../types/technique";
|
|
3
|
+
import type { TermJson } from "../types/term";
|
|
4
|
+
|
|
5
|
+
export declare const builtinTechniqueGroups: Record<
|
|
6
|
+
BuiltinTechniqueGroup,
|
|
7
|
+
Record<string, TechniqueJson | string>
|
|
8
|
+
>;
|
|
9
|
+
export declare const builtinTerms: Record<string, TermJson | string>;
|
|
10
|
+
export declare const builtinSources: SourcesJson;
|
package/src/builtins.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Kukkiwon data bundled with this package.
|
|
3
|
+
*
|
|
4
|
+
* It lives in its own module so that a consumer building an app with an
|
|
5
|
+
* entirely own technique tree can alias this specifier to `./builtins-empty`
|
|
6
|
+
* and keep the data out of the bundle.
|
|
7
|
+
*
|
|
8
|
+
* Plain JavaScript so that Node can load it at build time (see `./compose`);
|
|
9
|
+
* the types are in `./builtins.d.ts`.
|
|
10
|
+
*/
|
|
11
|
+
import chagi from "../db/chagi.json" with { type: "json" };
|
|
12
|
+
import chigi from "../db/chigi.json" with { type: "json" };
|
|
13
|
+
import ditgi from "../db/ditgi.json" with { type: "json" };
|
|
14
|
+
import japgi from "../db/japgi.json" with { type: "json" };
|
|
15
|
+
import jireugi from "../db/jireugi.json" with { type: "json" };
|
|
16
|
+
import jjireugi from "../db/jjireugi.json" with { type: "json" };
|
|
17
|
+
import kkeokgi from "../db/kkeokgi.json" with { type: "json" };
|
|
18
|
+
import makgi from "../db/makgi.json" with { type: "json" };
|
|
19
|
+
import milgi from "../db/milgi.json" with { type: "json" };
|
|
20
|
+
import neomgigi from "../db/neomgigi.json" with { type: "json" };
|
|
21
|
+
import pihagi from "../db/pihagi.json" with { type: "json" };
|
|
22
|
+
import ppaegi from "../db/ppaegi.json" with { type: "json" };
|
|
23
|
+
import seogi from "../db/seogi.json" with { type: "json" };
|
|
24
|
+
import sources from "../db/sources.json" with { type: "json" };
|
|
25
|
+
import terms from "../db/terms.json" with { type: "json" };
|
|
26
|
+
import ttwigi from "../db/ttwigi.json" with { type: "json" };
|
|
27
|
+
|
|
28
|
+
export const builtinTechniqueGroups = {
|
|
29
|
+
chagi,
|
|
30
|
+
chigi,
|
|
31
|
+
ditgi,
|
|
32
|
+
japgi,
|
|
33
|
+
jireugi,
|
|
34
|
+
jjireugi,
|
|
35
|
+
kkeokgi,
|
|
36
|
+
makgi,
|
|
37
|
+
milgi,
|
|
38
|
+
neomgigi,
|
|
39
|
+
pihagi,
|
|
40
|
+
ppaegi,
|
|
41
|
+
seogi,
|
|
42
|
+
ttwigi,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const builtinTerms = terms;
|
|
46
|
+
export const builtinSources = sources;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ComposedDatabase, ComposeOptions } from "./compose.js";
|
|
2
|
+
|
|
3
|
+
export type { ComposedDatabase, ComposeOptions, Include } from "./compose.js";
|
|
4
|
+
export { applyPatch } from "./compose.js";
|
|
5
|
+
|
|
6
|
+
/** Absolute path of this package's `img/` directory. */
|
|
7
|
+
export declare const builtinImageDir: string;
|
|
8
|
+
|
|
9
|
+
export declare function composeDatabase(
|
|
10
|
+
options?: ComposeOptions & { includeBuiltins?: boolean },
|
|
11
|
+
): ComposedDatabase;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@taekwondo-db/database/compose` — composes a database's JSON at build time.
|
|
3
|
+
*
|
|
4
|
+
* Same as the constructor of `TaekwondoDatabase` does at runtime, but in plain
|
|
5
|
+
* JavaScript, so it runs in Node (e.g. in a `vite.config.js`). Apps pass the
|
|
6
|
+
* result to `new TaekwondoDatabase({ includeBuiltins: false, ...result })` and
|
|
7
|
+
* ship only what was included instead of the whole Kukkiwon tree.
|
|
8
|
+
*/
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import {
|
|
11
|
+
builtinSources,
|
|
12
|
+
builtinTechniqueGroups,
|
|
13
|
+
builtinTerms,
|
|
14
|
+
} from "./builtins.js";
|
|
15
|
+
import { composeDatabase as compose, NO_BUILTINS } from "./compose.js";
|
|
16
|
+
|
|
17
|
+
export { applyPatch } from "./compose.js";
|
|
18
|
+
|
|
19
|
+
/** Absolute path of this package's `img/` directory. */
|
|
20
|
+
export const builtinImageDir = fileURLToPath(
|
|
21
|
+
new URL("../img", import.meta.url),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {import("./compose").ComposeOptions & { includeBuiltins?: boolean }} [options]
|
|
26
|
+
* @returns {import("./compose").ComposedDatabase}
|
|
27
|
+
*/
|
|
28
|
+
export function composeDatabase(options = {}) {
|
|
29
|
+
return compose(
|
|
30
|
+
options,
|
|
31
|
+
options.includeBuiltins === false
|
|
32
|
+
? NO_BUILTINS
|
|
33
|
+
: {
|
|
34
|
+
sources: builtinSources,
|
|
35
|
+
techniqueGroups: builtinTechniqueGroups,
|
|
36
|
+
terms: builtinTerms,
|
|
37
|
+
},
|
|
38
|
+
);
|
|
39
|
+
}
|
package/src/compose.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { SourcesJson } from "../types/source";
|
|
2
|
+
import type {
|
|
3
|
+
CustomTechniqueJson,
|
|
4
|
+
PatchTechniqueJson,
|
|
5
|
+
TechniqueJson,
|
|
6
|
+
} from "../types/technique";
|
|
7
|
+
import type { CustomTermJson, PatchTermJson, TermJson } from "../types/term";
|
|
8
|
+
import type { GradesJson } from "./grade";
|
|
9
|
+
|
|
10
|
+
export type TechniqueGroupJson = Record<string, TechniqueJson | string>;
|
|
11
|
+
|
|
12
|
+
/** Which builtin techniques (and extra terms) a database loads. */
|
|
13
|
+
export interface Include {
|
|
14
|
+
/** Builtin techniques by group, in display order; `"*"` for a whole group. */
|
|
15
|
+
techniques?: Record<string, string[] | "*">;
|
|
16
|
+
/** Builtin terms to load although no included technique uses them. */
|
|
17
|
+
terms?: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ComposeOptions {
|
|
21
|
+
/**
|
|
22
|
+
* Load only these builtin techniques. Terms and sources then follow
|
|
23
|
+
* automatically: only those the loaded data refers to are kept.
|
|
24
|
+
* Omitted = every builtin technique, term and source.
|
|
25
|
+
*/
|
|
26
|
+
include?: Include | null;
|
|
27
|
+
/** Own terms; replace builtin terms with the same key. */
|
|
28
|
+
terms?: CustomTermJson | null;
|
|
29
|
+
/** Own sources; replace builtin sources with the same key. */
|
|
30
|
+
sources?: SourcesJson | null;
|
|
31
|
+
/** Own technique groups, keyed by group name; merged into a builtin group of the same name. */
|
|
32
|
+
techniqueGroups?: Record<string, TechniqueGroupJson> | null;
|
|
33
|
+
/** Own techniques that carry their own `group`. */
|
|
34
|
+
customTechniques?: CustomTechniqueJson | null;
|
|
35
|
+
/** Changes to existing techniques; see {@link applyPatch}. */
|
|
36
|
+
patchTechniques?: PatchTechniqueJson | null;
|
|
37
|
+
/** Changes to existing terms; see {@link applyPatch}. */
|
|
38
|
+
patchTerms?: PatchTermJson | null;
|
|
39
|
+
/**
|
|
40
|
+
* From which grade on a technique is relevant, e.g. `{ "9-kup": ["Apchagi"] }`.
|
|
41
|
+
* Every technique listed must be loaded, and listed only once.
|
|
42
|
+
*/
|
|
43
|
+
grades?: GradesJson | null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface Builtins {
|
|
47
|
+
sources: SourcesJson | Record<string, unknown>;
|
|
48
|
+
techniqueGroups: Record<string, TechniqueGroupJson>;
|
|
49
|
+
terms: Record<string, TermJson | string>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ComposedDatabase {
|
|
53
|
+
sources: SourcesJson;
|
|
54
|
+
techniqueGroups: Record<string, Record<string, TechniqueJson>>;
|
|
55
|
+
customTechniques: Record<string, TechniqueJson>;
|
|
56
|
+
terms: Record<string, TermJson>;
|
|
57
|
+
/** Validated and sorted from the lowest grade up. */
|
|
58
|
+
grades: Record<string, string[]>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export declare const NO_BUILTINS: Builtins;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Objects merge recursively, arrays are appended, `null` removes a field,
|
|
65
|
+
* `{ $replace: value }` replaces it, `{ $append: "text" }` appends to a text.
|
|
66
|
+
*/
|
|
67
|
+
export declare function applyPatch<T extends object>(
|
|
68
|
+
target: T,
|
|
69
|
+
patch: object,
|
|
70
|
+
path?: string,
|
|
71
|
+
): T;
|
|
72
|
+
|
|
73
|
+
export declare function composeDatabase(
|
|
74
|
+
options: ComposeOptions,
|
|
75
|
+
builtins: Builtins,
|
|
76
|
+
): ComposedDatabase;
|
package/src/compose.js
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Composes the JSON a database is built from: the builtin Kukkiwon data,
|
|
3
|
+
* optionally narrowed down by an `include` list, with patches applied and
|
|
4
|
+
* own groups, terms and sources on top, and the techniques assigned to grades.
|
|
5
|
+
*
|
|
6
|
+
* Plain JavaScript without data imports on purpose:
|
|
7
|
+
* - Node can load it from node_modules (it does not strip TypeScript there),
|
|
8
|
+
* so apps can compose at build time — see `@taekwondo-db/database/compose`.
|
|
9
|
+
* - It pulls no builtin JSON into a bundle; the caller passes the builtins.
|
|
10
|
+
*/
|
|
11
|
+
import { compareGrades, parseGrade } from "./grade.js";
|
|
12
|
+
|
|
13
|
+
/** Builtins of a database built with `includeBuiltins: false`. */
|
|
14
|
+
export const NO_BUILTINS = Object.freeze({
|
|
15
|
+
sources: {},
|
|
16
|
+
techniqueGroups: {},
|
|
17
|
+
terms: {},
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/** Entries of a JSON map without `$schema` and string placeholders. */
|
|
21
|
+
function entries(json) {
|
|
22
|
+
return Object.entries(json ?? {}).filter(
|
|
23
|
+
([key, value]) => key !== "$schema" && typeof value === "object",
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** `"./terms.json#/ap"` → `"ap"` — only the part behind `#/` is resolved. */
|
|
28
|
+
function refKey(ref) {
|
|
29
|
+
return ref.$ref.split("#/")[1];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isPlainObject(value) {
|
|
33
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Applies a patch to a technique or term JSON entry.
|
|
38
|
+
*
|
|
39
|
+
* - objects are merged recursively (`description: { de: "…" }` keeps `en`)
|
|
40
|
+
* - arrays are appended (`videos: [...]` adds videos)
|
|
41
|
+
* - `null` removes the field
|
|
42
|
+
* - `{ "$replace": value }` replaces the field as a whole
|
|
43
|
+
* - `{ "$append": "text" }` appends to a text, separated by a space
|
|
44
|
+
* - everything else overwrites
|
|
45
|
+
*
|
|
46
|
+
* @param {object} target
|
|
47
|
+
* @param {object} patch
|
|
48
|
+
* @param {string} [path] field path for error messages
|
|
49
|
+
* @returns {object} a new object; `target` is left untouched
|
|
50
|
+
*/
|
|
51
|
+
export function applyPatch(target, patch, path = "") {
|
|
52
|
+
const result = { ...target };
|
|
53
|
+
|
|
54
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
55
|
+
if (value === null) {
|
|
56
|
+
delete result[key];
|
|
57
|
+
} else if (isPlainObject(value) && "$append" in value) {
|
|
58
|
+
const current = result[key];
|
|
59
|
+
if (current !== undefined && typeof current !== "string") {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`Could not patch ${path}${key}: "$append" needs a text field`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
result[key] = current ? `${current} ${value.$append}` : value.$append;
|
|
65
|
+
} else if (isPlainObject(value) && "$replace" in value) {
|
|
66
|
+
result[key] = value.$replace;
|
|
67
|
+
} else if (Array.isArray(value)) {
|
|
68
|
+
result[key] = [
|
|
69
|
+
...(Array.isArray(result[key]) ? result[key] : []),
|
|
70
|
+
...value,
|
|
71
|
+
];
|
|
72
|
+
} else if (isPlainObject(value) && isPlainObject(result[key])) {
|
|
73
|
+
result[key] = applyPatch(result[key], value, `${path}${key}.`);
|
|
74
|
+
} else {
|
|
75
|
+
result[key] = value;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {import("./compose").ComposeOptions} options
|
|
84
|
+
* @param {import("./compose").Builtins} builtins
|
|
85
|
+
* @returns {import("./compose").ComposedDatabase}
|
|
86
|
+
*/
|
|
87
|
+
export function composeDatabase(options, builtins) {
|
|
88
|
+
const include = options.include;
|
|
89
|
+
|
|
90
|
+
// 1. Builtin techniques — all, or those listed in `include`.
|
|
91
|
+
/** @type {Record<string, Record<string, object>>} */
|
|
92
|
+
const groups = {};
|
|
93
|
+
/** technique name → group, for every technique that gets loaded */
|
|
94
|
+
const groupOf = new Map();
|
|
95
|
+
/** every builtin technique name, loaded or not */
|
|
96
|
+
const builtinNames = new Set();
|
|
97
|
+
|
|
98
|
+
for (const json of Object.values(builtins.techniqueGroups)) {
|
|
99
|
+
for (const [name] of entries(json)) builtinNames.add(name);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const selection = include
|
|
103
|
+
? Object.entries(include.techniques ?? {})
|
|
104
|
+
: Object.keys(builtins.techniqueGroups).map((group) => [group, "*"]);
|
|
105
|
+
|
|
106
|
+
for (const [group, names] of selection) {
|
|
107
|
+
const builtin = builtins.techniqueGroups[group];
|
|
108
|
+
if (!builtin) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`include: unknown group "${group}" — available: ${Object.keys(builtins.techniqueGroups).join(", ")}`,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
groups[group] = {};
|
|
115
|
+
const picked = names === "*" ? entries(builtin).map(([n]) => n) : names;
|
|
116
|
+
|
|
117
|
+
for (const name of picked) {
|
|
118
|
+
if (typeof builtin[name] !== "object") {
|
|
119
|
+
const elsewhere = Object.keys(builtins.techniqueGroups).find(
|
|
120
|
+
(g) => typeof builtins.techniqueGroups[g][name] === "object",
|
|
121
|
+
);
|
|
122
|
+
throw new Error(
|
|
123
|
+
`include: "${name}" is not a technique of group "${group}"${
|
|
124
|
+
elsewhere ? ` (it is in "${elsewhere}")` : ""
|
|
125
|
+
}`,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
if (groupOf.has(name)) {
|
|
129
|
+
throw new Error(`include: "${name}" is listed twice`);
|
|
130
|
+
}
|
|
131
|
+
groups[group][name] = structuredClone(builtin[name]);
|
|
132
|
+
groupOf.set(name, group);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 2. Own techniques. An own group may share its name with a builtin one;
|
|
137
|
+
// a technique name may not — change those through `patchTechniques`.
|
|
138
|
+
const addOwn = (name, technique, group, origin) => {
|
|
139
|
+
if (groupOf.has(name)) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`${origin}: "${name}" already exists — use patchTechniques to change it`,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
groups[group] ??= {};
|
|
145
|
+
groups[group][name] = technique;
|
|
146
|
+
groupOf.set(name, group);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
for (const [group, json] of Object.entries(options.techniqueGroups ?? {})) {
|
|
150
|
+
groups[group] ??= {};
|
|
151
|
+
for (const [name, technique] of entries(json)) {
|
|
152
|
+
addOwn(name, technique, group, `techniqueGroups.${group}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Techniques that carry their own `group`. They are kept apart because
|
|
157
|
+
// they do not make their group show up in `getGroups()`.
|
|
158
|
+
const customTechniques = {};
|
|
159
|
+
for (const [name, technique] of entries(options.customTechniques)) {
|
|
160
|
+
if (groupOf.has(name)) {
|
|
161
|
+
throw new Error(
|
|
162
|
+
`customTechniques: "${name}" already exists — use patchTechniques to change it`,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
customTechniques[name] = technique;
|
|
166
|
+
groupOf.set(name, technique.group);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// 3. Technique patches.
|
|
170
|
+
for (const [name, patch] of entries(options.patchTechniques)) {
|
|
171
|
+
const target =
|
|
172
|
+
name in customTechniques ? customTechniques : groups[groupOf.get(name)];
|
|
173
|
+
if (!target?.[name]) {
|
|
174
|
+
throw new Error(
|
|
175
|
+
builtinNames.has(name)
|
|
176
|
+
? `Could not patch technique "${name}": it is not included`
|
|
177
|
+
: `Could not patch non-existing technique: ${name}`,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
target[name] = applyPatch(target[name], patch, `${name}.`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// References to builtin techniques that were left out simply do not exist
|
|
184
|
+
// in this database. Anything else unresolvable is left for the loader.
|
|
185
|
+
if (include) {
|
|
186
|
+
const all = [...Object.values(groups), customTechniques];
|
|
187
|
+
for (const json of all) {
|
|
188
|
+
for (const technique of Object.values(json)) {
|
|
189
|
+
if (!technique.references) continue;
|
|
190
|
+
technique.references = technique.references.filter((ref) => {
|
|
191
|
+
const key = refKey(ref);
|
|
192
|
+
return groupOf.has(key) || !builtinNames.has(key);
|
|
193
|
+
});
|
|
194
|
+
if (technique.references.length === 0) delete technique.references;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// 4. Terms. Own terms replace builtin ones with the same key.
|
|
200
|
+
const termPool = Object.fromEntries(entries(builtins.terms));
|
|
201
|
+
for (const [key, term] of entries(options.terms)) termPool[key] = term;
|
|
202
|
+
|
|
203
|
+
/** @type {Record<string, object>} */
|
|
204
|
+
let terms;
|
|
205
|
+
if (include) {
|
|
206
|
+
// Only what is needed: terms of loaded techniques and of group names,
|
|
207
|
+
// all own terms, extra ones from `include.terms` — and transitively
|
|
208
|
+
// whatever those point to via `lemma` and `terms`.
|
|
209
|
+
terms = {};
|
|
210
|
+
const pending = [
|
|
211
|
+
...Object.keys(groups).filter((group) => termPool[group]),
|
|
212
|
+
...entries(options.terms).map(([key]) => key),
|
|
213
|
+
...(include.terms ?? []),
|
|
214
|
+
];
|
|
215
|
+
for (const json of [...Object.values(groups), customTechniques]) {
|
|
216
|
+
for (const technique of Object.values(json)) {
|
|
217
|
+
pending.push(...(technique.terms ?? []).map(refKey));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
for (const key of include.terms ?? []) {
|
|
222
|
+
if (!termPool[key]) throw new Error(`include: unknown term "${key}"`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
while (pending.length > 0) {
|
|
226
|
+
const key = pending.pop();
|
|
227
|
+
// Unknown keys are left for the loader, which names the referrer.
|
|
228
|
+
if (key in terms || !termPool[key]) continue;
|
|
229
|
+
terms[key] = structuredClone(termPool[key]);
|
|
230
|
+
if (terms[key].lemma) pending.push(refKey(terms[key].lemma));
|
|
231
|
+
pending.push(...(terms[key].terms ?? []).map(refKey));
|
|
232
|
+
}
|
|
233
|
+
} else {
|
|
234
|
+
terms = structuredClone(termPool);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
for (const [key, patch] of entries(options.patchTerms)) {
|
|
238
|
+
if (!terms[key]) {
|
|
239
|
+
throw new Error(
|
|
240
|
+
termPool[key]
|
|
241
|
+
? `Could not patch term "${key}": no included technique uses it — add it to include.terms`
|
|
242
|
+
: `Could not patch non-existing term: ${key}`,
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
terms[key] = applyPatch(terms[key], patch, `${key}.`);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// 5. Sources. Own sources replace builtin ones with the same key.
|
|
249
|
+
/** @type {Record<string, object>} */
|
|
250
|
+
let sources;
|
|
251
|
+
if (include) {
|
|
252
|
+
const used = new Set();
|
|
253
|
+
const collectHistoric = (historic = []) => {
|
|
254
|
+
for (const entry of historic) {
|
|
255
|
+
for (const source of entry.sources ?? []) used.add(refKey(source));
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
for (const json of [...Object.values(groups), customTechniques]) {
|
|
259
|
+
for (const technique of Object.values(json)) {
|
|
260
|
+
for (const source of technique.sources ?? []) used.add(refKey(source));
|
|
261
|
+
collectHistoric(technique.historic);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
for (const term of Object.values(terms)) collectHistoric(term.historic);
|
|
265
|
+
|
|
266
|
+
sources = {};
|
|
267
|
+
for (const [key, source] of entries(builtins.sources)) {
|
|
268
|
+
if (used.has(key)) sources[key] = source;
|
|
269
|
+
}
|
|
270
|
+
} else {
|
|
271
|
+
sources = Object.fromEntries(entries(builtins.sources));
|
|
272
|
+
}
|
|
273
|
+
for (const [key, source] of entries(options.sources)) sources[key] = source;
|
|
274
|
+
|
|
275
|
+
// 6. Grades — from which grade on a technique is relevant.
|
|
276
|
+
/** @type {Record<string, string[]>} */
|
|
277
|
+
const grades = {};
|
|
278
|
+
/** technique name → grade it is listed at */
|
|
279
|
+
const gradeOf = new Map();
|
|
280
|
+
const listed = Object.entries(options.grades ?? {})
|
|
281
|
+
.filter(([key]) => key !== "$schema")
|
|
282
|
+
.map(([key, names]) => {
|
|
283
|
+
const grade = parseGrade(key);
|
|
284
|
+
if (!grade) {
|
|
285
|
+
throw new Error(
|
|
286
|
+
`grades: unknown grade "${key}" — expected e.g. "9-kup", "1-poom" or "1-dan"`,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
return { grade, names };
|
|
290
|
+
})
|
|
291
|
+
.sort((a, b) => compareGrades(a.grade, b.grade));
|
|
292
|
+
|
|
293
|
+
for (const { grade, names } of listed) {
|
|
294
|
+
for (const name of names) {
|
|
295
|
+
if (!groupOf.has(name)) {
|
|
296
|
+
throw new Error(
|
|
297
|
+
builtinNames.has(name)
|
|
298
|
+
? `grades: "${name}" (${grade.key}) is not included`
|
|
299
|
+
: `grades: unknown technique "${name}" (${grade.key})`,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
if (gradeOf.has(name)) {
|
|
303
|
+
throw new Error(
|
|
304
|
+
`grades: "${name}" is listed at "${gradeOf.get(name)}" and "${grade.key}" — list it only at the first grade`,
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
gradeOf.set(name, grade.key);
|
|
308
|
+
}
|
|
309
|
+
grades[grade.key] = [...names];
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return { customTechniques, grades, sources, techniqueGroups: groups, terms };
|
|
313
|
+
}
|