glossarist 0.4.39 → 0.4.40

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.
@@ -0,0 +1,315 @@
1
+ import { GlossaristModel } from './base.js';
2
+ import { Section } from './section.js';
3
+ import type { SectionJson } from './section.js';
4
+ import { resolveColor } from './dataset-color.js';
5
+ import type { DatasetColor } from './dataset-color.js';
6
+ import type { LocalizedString } from './generic-member.js';
7
+
8
+ export const REGISTER_STATUSES: ReadonlyArray<string> = Object.freeze([
9
+ 'current',
10
+ 'superseded',
11
+ 'withdrawn',
12
+ 'draft',
13
+ ]);
14
+
15
+ export interface RegisterJson {
16
+ schema_version?: string;
17
+ schemaVersion?: string;
18
+ id?: string | null;
19
+ name?: string | LocalizedString | null;
20
+ ref?: string | null;
21
+ refAliases?: ReadonlyArray<string>;
22
+ ref_aliases?: ReadonlyArray<string>;
23
+ year?: number | string | null;
24
+ urn?: string | null;
25
+ urnAliases?: ReadonlyArray<string>;
26
+ urn_aliases?: ReadonlyArray<string>;
27
+ status?: string | null;
28
+ supersedes?: string | null;
29
+ owner?: string | null;
30
+ sourceRepo?: string | null;
31
+ source_repo?: string | null;
32
+ tags?: ReadonlyArray<string>;
33
+ languages?: ReadonlyArray<string>;
34
+ languageOrder?: ReadonlyArray<string>;
35
+ language_order?: ReadonlyArray<string>;
36
+ ordering?: string | null;
37
+ logo?: Record<string, unknown> | null;
38
+ color?: DatasetColor | null;
39
+ description?: LocalizedString;
40
+ about?: LocalizedString;
41
+ provenance?: ReadonlyArray<Record<string, unknown>>;
42
+ contributors?: ReadonlyArray<Record<string, unknown>>;
43
+ sections?: ReadonlyArray<SectionJson | Section>;
44
+ [key: string]: unknown;
45
+ }
46
+
47
+ export class Register extends GlossaristModel {
48
+ readonly schemaVersion: string;
49
+ readonly id: string | null;
50
+ readonly name: string | LocalizedString | null;
51
+ readonly ref: string | null;
52
+ readonly refAliases: ReadonlyArray<string>;
53
+ readonly year: number | string | null;
54
+ readonly urn: string | null;
55
+ readonly urnAliases: ReadonlyArray<string>;
56
+ readonly status: string | null;
57
+ readonly supersedes: string | null;
58
+ readonly owner: string | null;
59
+ readonly sourceRepo: string | null;
60
+ readonly tags: ReadonlyArray<string>;
61
+ readonly languages: ReadonlyArray<string>;
62
+ readonly languageOrder: ReadonlyArray<string>;
63
+ readonly ordering: string | null;
64
+ readonly logo: Record<string, unknown> | null;
65
+ readonly color: DatasetColor | null;
66
+ readonly description: LocalizedString;
67
+ readonly about: LocalizedString;
68
+ readonly provenance: ReadonlyArray<Record<string, unknown>>;
69
+ readonly contributors: ReadonlyArray<Record<string, unknown>>;
70
+ readonly sections: ReadonlyArray<Section>;
71
+ private readonly _raw: Record<string, unknown>;
72
+
73
+ constructor(data: RegisterJson = {}) {
74
+ super();
75
+ this.schemaVersion = data.schema_version ?? data.schemaVersion ?? '3';
76
+ this.id = data.id ?? null;
77
+ this.name = (data.name as string | LocalizedString | null) ?? null;
78
+ this.ref = data.ref ?? null;
79
+ this.refAliases = data.refAliases ?? data.ref_aliases ?? [];
80
+ this.year = (data.year as number | string | null) ?? null;
81
+ this.urn = data.urn ?? null;
82
+ this.urnAliases = data.urnAliases ?? data.urn_aliases ?? [];
83
+ this.status = data.status ?? null;
84
+ this.supersedes = data.supersedes ?? null;
85
+ this.owner = data.owner ?? null;
86
+ this.sourceRepo = data.sourceRepo ?? data.source_repo ?? null;
87
+ this.tags = (data.tags as ReadonlyArray<string>) ?? [];
88
+ this.languages = (data.languages as ReadonlyArray<string>) ?? [];
89
+ this.languageOrder = data.languageOrder ?? data.language_order ?? [];
90
+ this.ordering = data.ordering ?? null;
91
+ this.logo = (data.logo as Record<string, unknown> | null) ?? null;
92
+ this.color = (data.color as DatasetColor | null) ?? null;
93
+ this.description = (data.description as LocalizedString) ?? {};
94
+ this.about = (data.about as LocalizedString) ?? {};
95
+ this.provenance = (data.provenance as ReadonlyArray<Record<string, unknown>>) ?? [];
96
+ this.contributors = (data.contributors as ReadonlyArray<Record<string, unknown>>) ?? [];
97
+ this.sections = (data.sections ?? []).map((s) =>
98
+ s instanceof Section ? s : new Section(s as SectionJson),
99
+ );
100
+ this._raw = this._extractRaw(data);
101
+ }
102
+
103
+ private _extractRaw(data: RegisterJson): Record<string, unknown> {
104
+ const known = new Set([
105
+ 'schema_version', 'schemaVersion',
106
+ 'id', 'name', 'ref', 'refAliases', 'ref_aliases',
107
+ 'year', 'urn', 'urnAliases', 'urn_aliases',
108
+ 'status', 'supersedes', 'owner',
109
+ 'sourceRepo', 'source_repo', 'tags',
110
+ 'languages', 'languageOrder', 'language_order',
111
+ 'ordering', 'logo', 'color', 'description', 'about',
112
+ 'provenance', 'contributors', 'sections',
113
+ ]);
114
+ const extra: Record<string, unknown> = {};
115
+ for (const [k, v] of Object.entries(data)) {
116
+ if (!known.has(k)) extra[k] = v;
117
+ }
118
+ return extra;
119
+ }
120
+
121
+ sectionById(id: string): Section | null {
122
+ for (const section of this.sections) {
123
+ if (section.id === id) return section;
124
+ const found = section.descendantById(id);
125
+ if (found) return found;
126
+ }
127
+ return null;
128
+ }
129
+
130
+ /**
131
+ * Walks the section tree upward from `sectionId`, returning the
132
+ * ancestor chain in immediate-parent-first order. The returned array
133
+ * does NOT include sectionId itself. Returns [] when sectionId is
134
+ * unknown or is a top-level section.
135
+ */
136
+ sectionAncestorIds(sectionId: string | null | undefined): string[] {
137
+ if (!sectionId) return [];
138
+ for (const root of this.sections) {
139
+ const chain = _ancestorChain(root, sectionId);
140
+ if (chain) return chain;
141
+ }
142
+ return [];
143
+ }
144
+
145
+ /**
146
+ * Returns the closure of `sectionId`: the section plus all of its
147
+ * ancestors. Concept-section membership tests should intersect the
148
+ * concept's section list with this closure.
149
+ */
150
+ sectionClosure(sectionId: string | null | undefined): string[] {
151
+ return sectionId ? [sectionId, ...this.sectionAncestorIds(sectionId)] : [];
152
+ }
153
+
154
+ /**
155
+ * Walks the section tree DOWNWARD from `sectionId`, returning all
156
+ * descendant section IDs in pre-order.
157
+ */
158
+ sectionDescendantIds(sectionId: string | null | undefined): string[] {
159
+ if (!sectionId) return [];
160
+ const section = this.sectionById(sectionId);
161
+ if (!section) return [];
162
+ const out: string[] = [];
163
+ for (const child of section.children) {
164
+ if (child.id) out.push(child.id);
165
+ out.push(...this.sectionDescendantIds(child.id));
166
+ }
167
+ return out;
168
+ }
169
+
170
+ sectionDescendantClosure(sectionId: string | null | undefined): string[] {
171
+ return sectionId ? [sectionId, ...this.sectionDescendantIds(sectionId)] : [];
172
+ }
173
+
174
+ /**
175
+ * Returns all section IDs the concept belongs to: the concept's own
176
+ * sections plus every ancestor of each.
177
+ */
178
+ conceptSectionIds(concept: { sections?: unknown; groups?: unknown }): string[] {
179
+ const own = conceptSectionIdList(concept);
180
+ if (own.length === 0) return [];
181
+ const closure = new Set<string>();
182
+ for (const id of own) {
183
+ for (const ancestor of this.sectionClosure(id)) closure.add(ancestor);
184
+ }
185
+ return [...closure];
186
+ }
187
+
188
+ sectionName(sectionId: string, lang: string): string | null {
189
+ const section = this.sectionById(sectionId);
190
+ return section ? section.name(lang) : null;
191
+ }
192
+
193
+ /**
194
+ * Localized display name. Returns the best-matching name string for
195
+ * the requested language, falling back to English, then to the first
196
+ * available language, then to null.
197
+ */
198
+ displayName(lang: string): string | null {
199
+ if (this.name == null) return null;
200
+ if (typeof this.name === 'string') return this.name;
201
+ if (typeof this.name === 'object') {
202
+ return (
203
+ this.name[lang] ??
204
+ this.name['eng'] ??
205
+ (Object.values(this.name)[0] as string | undefined) ??
206
+ null
207
+ );
208
+ }
209
+ return null;
210
+ }
211
+
212
+ resolvedColor(mode: 'light' | 'dark'): string | null {
213
+ return resolveColor(this.color, mode);
214
+ }
215
+
216
+ override toJSON(): RegisterJson {
217
+ const obj = { ...this._raw, schema_version: this.schemaVersion } as RegisterJson;
218
+ if (this.id != null) obj.id = this.id;
219
+ if (this.name != null) obj.name = this.name;
220
+ if (this.ref != null) obj.ref = this.ref;
221
+ if (this.refAliases.length > 0) obj.refAliases = [...this.refAliases];
222
+ if (this.year != null) obj.year = this.year;
223
+ if (this.urn != null) obj.urn = this.urn;
224
+ if (this.urnAliases.length > 0) obj.urnAliases = [...this.urnAliases];
225
+ if (this.status != null) obj.status = this.status;
226
+ if (this.supersedes != null) obj.supersedes = this.supersedes;
227
+ if (this.owner != null) obj.owner = this.owner;
228
+ if (this.sourceRepo != null) obj.sourceRepo = this.sourceRepo;
229
+ if (this.tags.length > 0) obj.tags = [...this.tags];
230
+ if (this.languages.length > 0) obj.languages = [...this.languages];
231
+ if (this.languageOrder.length > 0) obj.languageOrder = [...this.languageOrder];
232
+ if (this.ordering != null) obj.ordering = this.ordering;
233
+ if (this.logo != null) obj.logo = { ...this.logo };
234
+ if (this.color != null) {
235
+ obj.color =
236
+ typeof this.color === 'object' && this.color !== null
237
+ ? { ...this.color }
238
+ : this.color;
239
+ }
240
+ if (Object.keys(this.description).length > 0) obj.description = { ...this.description };
241
+ if (Object.keys(this.about).length > 0) obj.about = { ...this.about };
242
+ if (this.provenance.length > 0) obj.provenance = this.provenance.map((p) => ({ ...p }));
243
+ if (this.contributors.length > 0) obj.contributors = this.contributors.map((c) => ({ ...c }));
244
+ if (this.sections.length > 0) obj.sections = this.sections.map((s) => s.toJSON());
245
+ return obj;
246
+ }
247
+
248
+ static override fromJSON(data: RegisterJson): Register {
249
+ const instance = new Register(data);
250
+ const snakeToCamel = (k: string): string =>
251
+ k.replace(/_([a-z])/g, (_, c: string) => c.toUpperCase());
252
+ return new Proxy(instance, {
253
+ get(target: Register, prop: string | symbol): unknown {
254
+ if (typeof prop === 'string') {
255
+ if (prop in target) return (target as unknown as Record<string, unknown>)[prop];
256
+ if (prop in target._raw) return target._raw[prop];
257
+ const camel = snakeToCamel(prop);
258
+ if (camel in target) return (target as unknown as Record<string, unknown>)[camel];
259
+ }
260
+ return undefined;
261
+ },
262
+ });
263
+ }
264
+ }
265
+
266
+ /**
267
+ * Recursive walk: returns the ancestor chain of `targetId` within the
268
+ * subtree rooted at `section`, or null if targetId is not in this
269
+ * subtree.
270
+ */
271
+ function _ancestorChain(
272
+ section: Section,
273
+ targetId: string,
274
+ ancestors: string[] = [],
275
+ ): string[] | null {
276
+ if (section.id === targetId) return [...ancestors].reverse();
277
+ for (const child of section.children) {
278
+ const childId = child.id;
279
+ if (childId == null) continue;
280
+ const nextAncestors = section.id != null ? [...ancestors, section.id] : [...ancestors];
281
+ const found = _ancestorChain(child, targetId, nextAncestors);
282
+ if (found) return found;
283
+ }
284
+ return null;
285
+ }
286
+
287
+ /**
288
+ * Returns the list of section IDs a concept claims membership in.
289
+ * Concepts may carry section IDs via either `sections` (preferred) or
290
+ * `groups` (legacy). Each entry may be a string or an object with an
291
+ * `id` field; both forms are flattened to a string list.
292
+ */
293
+ function conceptSectionIdList(concept: { sections?: unknown; groups?: unknown }): string[] {
294
+ if (!concept) return [];
295
+ const fromSections = concept.sections
296
+ ? _flattenSectionIds(concept.sections)
297
+ : [];
298
+ const fromGroups = concept.groups ? _flattenSectionIds(concept.groups) : [];
299
+ return [...fromSections, ...fromGroups];
300
+ }
301
+
302
+ function _flattenSectionIds(value: unknown): string[] {
303
+ if (!Array.isArray(value)) return [];
304
+ const out: string[] = [];
305
+ for (const entry of value) {
306
+ if (typeof entry === 'string') {
307
+ out.push(entry);
308
+ } else if (entry && typeof entry === 'object') {
309
+ const e = entry as { id?: string; sectionId?: string; ref?: { id?: string } };
310
+ const id = e.id ?? e.sectionId ?? e.ref?.id;
311
+ if (id) out.push(String(id));
312
+ }
313
+ }
314
+ return out;
315
+ }
@@ -0,0 +1,167 @@
1
+ // Categorization SSOT for the 52 glossarist relationship types.
2
+
3
+ import { RELATIONSHIP_TYPES } from './related-concept.js';
4
+
5
+ export interface CategoryDefinition {
6
+ label: string;
7
+ description: string;
8
+ types: ReadonlyArray<string>;
9
+ }
10
+
11
+ type CategoryKey =
12
+ | 'lifecycle'
13
+ | 'hierarchical'
14
+ | 'associative'
15
+ | 'comparative'
16
+ | 'spatiotemporal'
17
+ | 'lexical'
18
+ | 'mapping'
19
+ | 'definitional';
20
+
21
+ /**
22
+ * Each type in RELATIONSHIP_TYPES belongs to exactly one category. The
23
+ * categories mirror the concept-model taxonomy. Adding a new type
24
+ * means adding a taxonomy entry — no code changes here.
25
+ */
26
+ export const RELATION_CATEGORIES: Readonly<Record<CategoryKey, CategoryDefinition>> = Object.freeze({
27
+ lifecycle: Object.freeze({
28
+ label: 'Lifecycle',
29
+ description: 'Concept lifecycle transitions: supersession, deprecation, replacement.',
30
+ types: Object.freeze([
31
+ 'deprecates', 'deprecated_by',
32
+ 'supersedes', 'superseded_by',
33
+ 'replaces', 'replaced_by',
34
+ 'invalidates', 'invalidated_by',
35
+ 'retires', 'retired_by',
36
+ ]),
37
+ }) as CategoryDefinition,
38
+
39
+ hierarchical: Object.freeze({
40
+ label: 'Hierarchical',
41
+ description: 'Broader/narrower relations, including SKOS and ISO 25964 generic/partitive/instantial.',
42
+ types: Object.freeze([
43
+ 'broader', 'narrower',
44
+ 'broader_generic', 'narrower_generic',
45
+ 'broader_partitive', 'narrower_partitive',
46
+ 'broader_instantial', 'narrower_instantial',
47
+ ]),
48
+ }) as CategoryDefinition,
49
+
50
+ associative: Object.freeze({
51
+ label: 'Associative',
52
+ description: 'Generic see-also references between concepts.',
53
+ types: Object.freeze([
54
+ 'see', 'related_concept',
55
+ 'related_concept_broader', 'related_concept_narrower',
56
+ 'references',
57
+ ]),
58
+ }) as CategoryDefinition,
59
+
60
+ comparative: Object.freeze({
61
+ label: 'Comparative',
62
+ description: 'Compare / contrast relations between concepts.',
63
+ types: Object.freeze(['compare', 'contrast']),
64
+ }) as CategoryDefinition,
65
+
66
+ spatiotemporal: Object.freeze({
67
+ label: 'Spatiotemporal',
68
+ description: 'Sequential, spatial, and temporal relations.',
69
+ types: Object.freeze([
70
+ 'sequentially_related_concept',
71
+ 'spatially_related_concept',
72
+ 'temporally_related_concept',
73
+ ]),
74
+ }) as CategoryDefinition,
75
+
76
+ lexical: Object.freeze({
77
+ label: 'Lexical',
78
+ description: 'Lexical relations between designations across languages.',
79
+ types: Object.freeze(['homograph', 'false_friend']),
80
+ }) as CategoryDefinition,
81
+
82
+ mapping: Object.freeze({
83
+ label: 'Mapping',
84
+ description: 'SKOS mapping properties for cross-vocabulary alignment.',
85
+ types: Object.freeze([
86
+ 'equivalent', 'close_match',
87
+ 'broad_match', 'narrow_match', 'related_match',
88
+ ]),
89
+ }) as CategoryDefinition,
90
+
91
+ definitional: Object.freeze({
92
+ label: 'Definitional',
93
+ description: 'ISO 19135 concept-to-concept relations: definition, part, instance, inheritance, versioning.',
94
+ types: Object.freeze([
95
+ 'has_concept', 'is_concept_of',
96
+ 'instance_of', 'has_instance',
97
+ 'has_definition', 'definition_of',
98
+ 'has_part', 'is_part_of',
99
+ 'inherits', 'inherited_by',
100
+ 'has_version', 'version_of',
101
+ 'current_version', 'current_version_of',
102
+ 'provides', 'provided_by',
103
+ ]),
104
+ }) as CategoryDefinition,
105
+ });
106
+
107
+ const _CATEGORY_BY_TYPE: ReadonlyMap<string, string> = (() => {
108
+ const map = new Map<string, string>();
109
+ for (const [cat, def] of Object.entries(RELATION_CATEGORIES)) {
110
+ for (const t of def.types) map.set(t, cat);
111
+ }
112
+ return map;
113
+ })();
114
+
115
+ const _CATEGORY_ALIASES: Readonly<Record<string, string>> = {
116
+ conceptInstance: 'definitional',
117
+ versioning: 'definitional',
118
+ };
119
+
120
+ /**
121
+ * Returns the category key for the given relationship type, or null
122
+ * when the type is not categorized.
123
+ */
124
+ export function categoryOf(type: string): string | null {
125
+ return _CATEGORY_BY_TYPE.get(type) ?? null;
126
+ }
127
+
128
+ /**
129
+ * Returns the category definition for the given category key.
130
+ * Accepts both canonical names (definitional) and aliases from
131
+ * earlier glossarist-js versions.
132
+ */
133
+ export function categoryDefinition(categoryKey: string): CategoryDefinition | null {
134
+ const canonical = _CATEGORY_ALIASES[categoryKey] ?? categoryKey;
135
+ return (RELATION_CATEGORIES as Readonly<Record<string, CategoryDefinition>>)[canonical] ?? null;
136
+ }
137
+
138
+ /**
139
+ * MECE sanity check. Returns the list of relationship types that are
140
+ * NOT categorized.
141
+ */
142
+ export function uncategorizedTypes(): string[] {
143
+ return RELATIONSHIP_TYPES.filter((t) => !_CATEGORY_BY_TYPE.has(t));
144
+ }
145
+
146
+ /**
147
+ * MECE sanity check. Returns a list of { type, count } entries for any
148
+ * type that appears in MORE than one category.
149
+ */
150
+ export function duplicatedTypes(): ReadonlyArray<{ type: string; count: number }> {
151
+ const counts = new Map<string, number>();
152
+ for (const def of Object.values(RELATION_CATEGORIES)) {
153
+ for (const t of def.types) {
154
+ counts.set(t, (counts.get(t) ?? 0) + 1);
155
+ }
156
+ }
157
+ const out: { type: string; count: number }[] = [];
158
+ for (const [type, count] of counts) {
159
+ if (count > 1) out.push({ type, count });
160
+ }
161
+ return out;
162
+ }
163
+
164
+ export {
165
+ RELATION_CATEGORIES as RELATION_CATEGORIES_READONLY,
166
+ type CategoryKey as RelationCategoryKey,
167
+ };
@@ -6,10 +6,27 @@
6
6
 
7
7
  import { categoryOf, categoryDefinition } from './relation-categories.js';
8
8
 
9
- // One { light, dark } pair per category. Keeping these in glossarist-js
10
- // lets the defaults ship with the library; consumers only need to
11
- // customize when they want to deviate.
12
- export const RELATION_COLOR_DEFAULTS = Object.freeze({
9
+ export interface ColorPair {
10
+ light: string;
11
+ dark: string;
12
+ }
13
+
14
+ export interface RelationColorPalette {
15
+ byCategory: Readonly<Record<string, ColorPair>>;
16
+ byType: Readonly<Record<string, ColorPair>>;
17
+ }
18
+
19
+ export interface ResolveRelationColorOptions {
20
+ overrides?: Partial<RelationColorPalette>;
21
+ mode?: 'light' | 'dark';
22
+ }
23
+
24
+ /**
25
+ * One { light, dark } pair per category. Keeping these in glossarist-js
26
+ * lets the defaults ship with the library; consumers only need to
27
+ * customize when they want to deviate.
28
+ */
29
+ export const RELATION_COLOR_DEFAULTS: RelationColorPalette = Object.freeze({
13
30
  byCategory: Object.freeze({
14
31
  lifecycle: { light: '#B43A2E', dark: '#F87171' },
15
32
  hierarchical: { light: '#1F6FEB', dark: '#6EA8FE' },
@@ -19,39 +36,32 @@ export const RELATION_COLOR_DEFAULTS = Object.freeze({
19
36
  lexical: { light: '#D97706', dark: '#FBBF24' },
20
37
  mapping: { light: '#2563EB', dark: '#93BBFD' },
21
38
  definitional: { light: '#65A30D', dark: '#A3E635' },
22
- }),
23
-
24
- // Per-type overrides. Use the same { light, dark } shape. Keys are
25
- // relationship-type local names. Empty by default — consumers may
26
- // populate this through `overrides.byType` at call time.
27
- byType: Object.freeze({}),
39
+ }) as Readonly<Record<string, ColorPair>>,
40
+ byType: Object.freeze({}) as Readonly<Record<string, ColorPair>>,
28
41
  });
29
42
 
30
43
  /**
31
- * Resolves the color for a relationship type.
32
- *
33
- * Resolution order:
44
+ * Resolves the color for a relationship type. Resolution order:
34
45
  * 1. overrides.byType[type]
35
46
  * 2. RELATION_COLOR_DEFAULTS.byType[type]
36
47
  * 3. overrides.byCategory[category]
37
48
  * 4. RELATION_COLOR_DEFAULTS.byCategory[category]
38
49
  * 5. null (uncategorized or unrecognized type)
39
- *
40
- * @param {string} type - relationship type local name
41
- * @param {{ overrides?: { byType?: object, byCategory?: object }, mode?: 'light'|'dark' }} [options]
42
- * @returns {string | null}
43
50
  */
44
- export function resolveRelationColor(type, options = {}) {
51
+ export function resolveRelationColor(
52
+ type: string,
53
+ options: ResolveRelationColorOptions = {},
54
+ ): string | null {
45
55
  const mode = options.mode ?? 'light';
46
56
  const overrides = options.overrides ?? {};
47
-
48
57
  const typeOverride = overrides.byType?.[type] ?? RELATION_COLOR_DEFAULTS.byType[type];
49
58
  if (typeOverride) return pickMode(typeOverride, mode);
50
59
 
51
60
  const category = categoryOf(type);
52
61
  if (!category) return null;
53
62
 
54
- const categoryOverride = overrides.byCategory?.[category] ?? RELATION_COLOR_DEFAULTS.byCategory[category];
63
+ const categoryOverride =
64
+ overrides.byCategory?.[category] ?? RELATION_COLOR_DEFAULTS.byCategory[category];
55
65
  if (!categoryOverride) return null;
56
66
 
57
67
  return pickMode(categoryOverride, mode);
@@ -61,16 +71,19 @@ export function resolveRelationColor(type, options = {}) {
61
71
  * Returns the { light, dark } pair for a category, merging overrides
62
72
  * over defaults. Useful for UIs that want to render a legend.
63
73
  */
64
- export function categoryColorPair(categoryKey, overrides = {}) {
74
+ export function categoryColorPair(
75
+ categoryKey: string,
76
+ overrides: { byCategory?: Record<string, Partial<ColorPair>> } = {},
77
+ ): ColorPair | null {
65
78
  const def = categoryDefinition(categoryKey);
66
79
  if (!def) return null;
67
80
  const fromOverride = overrides.byCategory?.[categoryKey];
68
81
  const fromDefault = RELATION_COLOR_DEFAULTS.byCategory[categoryKey];
69
82
  if (!fromDefault && !fromOverride) return null;
70
- return { ...(fromDefault ?? {}), ...(fromOverride ?? {}) };
83
+ return { ...(fromDefault ?? { light: '', dark: '' }), ...(fromOverride ?? {}) } as ColorPair;
71
84
  }
72
85
 
73
- function pickMode(pair, mode) {
86
+ function pickMode(pair: ColorPair | string | null, mode: 'light' | 'dark'): string | null {
74
87
  if (pair == null) return null;
75
88
  if (typeof pair === 'string') return pair;
76
89
  return pair[mode] ?? pair.light ?? pair.dark ?? null;
@@ -0,0 +1,61 @@
1
+ import { GlossaristModel } from './base.js';
2
+ import type { LocalizedString } from './generic-member.js';
3
+
4
+ export const ORDERING_METHODS: ReadonlyArray<string> = Object.freeze([
5
+ 'systematic',
6
+ 'mixed',
7
+ 'alphabetical',
8
+ ]);
9
+
10
+ export type OrderingMethod = string;
11
+
12
+ export interface SectionJson {
13
+ id?: string | null;
14
+ names?: LocalizedString;
15
+ ordering?: OrderingMethod | null;
16
+ children?: ReadonlyArray<SectionJson | Section>;
17
+ }
18
+
19
+ export class Section extends GlossaristModel {
20
+ readonly id: string | null;
21
+ readonly names: LocalizedString;
22
+ readonly ordering: OrderingMethod | null;
23
+ readonly children: ReadonlyArray<Section>;
24
+
25
+ constructor(data: SectionJson = {}) {
26
+ super();
27
+ this.id = data.id ?? null;
28
+ this.names = (data.names as LocalizedString) ?? {};
29
+ this.ordering = data.ordering ?? null;
30
+ this.children = (data.children ?? []).map((c) =>
31
+ c instanceof Section ? c : new Section(c as SectionJson),
32
+ );
33
+ }
34
+
35
+ name(lang: string): string | null {
36
+ return this.names[lang] ?? this.names['eng'] ?? null;
37
+ }
38
+
39
+ descendantById(id: string): Section | null {
40
+ for (const child of this.children) {
41
+ if (child.id === id) return child;
42
+ const found = child.descendantById(id);
43
+ if (found) return found;
44
+ }
45
+ return null;
46
+ }
47
+
48
+ override toJSON(): SectionJson {
49
+ const obj: SectionJson = { id: this.id };
50
+ if (Object.keys(this.names).length > 0) obj.names = { ...this.names };
51
+ if (this.ordering != null) obj.ordering = this.ordering;
52
+ if (this.children.length > 0) {
53
+ obj.children = this.children.map((c) => c.toJSON());
54
+ }
55
+ return obj;
56
+ }
57
+
58
+ static override fromJSON(data: SectionJson): Section {
59
+ return new Section(data);
60
+ }
61
+ }