glossarist 0.4.37 → 0.4.38

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glossarist",
3
- "version": "0.4.37",
3
+ "version": "0.4.38",
4
4
  "description": "JavaScript SDK for Glossarist GCR packages — read, write, validate, and manage terminology concepts",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -0,0 +1,64 @@
1
+ import * as yaml from 'js-yaml';
2
+ import { GlossaristModel } from './base.js';
3
+ import { BibliographyEntry } from './bibliography-entry.js';
4
+ import type { BibliographyEntryJson } from './bibliography-entry.js';
5
+
6
+ export interface BibliographyDataJson {
7
+ bibliography?: ReadonlyArray<BibliographyEntryJson | BibliographyEntry>;
8
+ entries?: ReadonlyArray<BibliographyEntryJson | BibliographyEntry>;
9
+ }
10
+
11
+ export class BibliographyData extends GlossaristModel {
12
+ private readonly _rawEntries: ReadonlyArray<BibliographyEntryJson | BibliographyEntry>;
13
+ private _entries: ReadonlyArray<BibliographyEntry> | null = null;
14
+
15
+ constructor(data: BibliographyDataJson | ReadonlyArray<BibliographyEntryJson | BibliographyEntry> = {}) {
16
+ super();
17
+ let entriesData: ReadonlyArray<BibliographyEntryJson | BibliographyEntry> | undefined;
18
+ if (Array.isArray(data)) {
19
+ entriesData = data;
20
+ } else {
21
+ entriesData = (data as BibliographyDataJson)?.bibliography ?? (data as BibliographyDataJson)?.entries ?? [];
22
+ }
23
+ this._rawEntries = Array.isArray(entriesData) ? entriesData : [];
24
+ }
25
+
26
+ get entries(): ReadonlyArray<BibliographyEntry> {
27
+ return this._lazy<BibliographyEntry>(
28
+ '_entries',
29
+ '_rawEntries',
30
+ (e) => (e instanceof BibliographyEntry ? e : new BibliographyEntry(e as BibliographyEntryJson)),
31
+ );
32
+ }
33
+
34
+ find(id: string): BibliographyEntry | null {
35
+ return this.entries.find((e) => e.id === id) ?? null;
36
+ }
37
+
38
+ get keys(): ReadonlyArray<string | null> {
39
+ return this.entries.map((e) => e.id);
40
+ }
41
+
42
+ override toJSON(): BibliographyDataJson {
43
+ if (this.entries.length === 0) return { bibliography: [] };
44
+ return { bibliography: this.entries.map((e) => e.toJSON()) };
45
+ }
46
+
47
+ toYAML(): string {
48
+ return yaml.dump(this.toJSON());
49
+ }
50
+
51
+ static fromYAML(yamlString: string): BibliographyData {
52
+ const parsed = yaml.load(yamlString) as
53
+ | BibliographyDataJson
54
+ | ReadonlyArray<BibliographyEntryJson>
55
+ | undefined;
56
+ return new BibliographyData(parsed ?? {});
57
+ }
58
+
59
+ static override fromJSON(
60
+ data: BibliographyDataJson | ReadonlyArray<BibliographyEntryJson>,
61
+ ): BibliographyData {
62
+ return new BibliographyData(data);
63
+ }
64
+ }
@@ -1,7 +1,21 @@
1
1
  import { GlossaristModel } from './base.js';
2
2
 
3
+ export interface BibliographyEntryJson {
4
+ id?: string | null;
5
+ reference?: string | null;
6
+ title?: string | null;
7
+ link?: string | null;
8
+ type?: string | null;
9
+ }
10
+
3
11
  export class BibliographyEntry extends GlossaristModel {
4
- constructor(data = {}) {
12
+ readonly id: string | null;
13
+ readonly reference: string | null;
14
+ readonly title: string | null;
15
+ readonly link: string | null;
16
+ readonly type: string | null;
17
+
18
+ constructor(data: BibliographyEntryJson = {}) {
5
19
  super();
6
20
  this.id = data.id ?? null;
7
21
  this.reference = data.reference ?? null;
@@ -10,8 +24,8 @@ export class BibliographyEntry extends GlossaristModel {
10
24
  this.type = data.type ?? null;
11
25
  }
12
26
 
13
- toJSON() {
14
- const obj = {};
27
+ override toJSON(): BibliographyEntryJson {
28
+ const obj: BibliographyEntryJson = {};
15
29
  if (this.id != null) obj.id = this.id;
16
30
  if (this.reference != null) obj.reference = this.reference;
17
31
  if (this.title != null) obj.title = this.title;
@@ -20,7 +34,7 @@ export class BibliographyEntry extends GlossaristModel {
20
34
  return obj;
21
35
  }
22
36
 
23
- static fromJSON(data) {
37
+ static override fromJSON(data: BibliographyEntryJson): BibliographyEntry {
24
38
  return new BibliographyEntry(data);
25
39
  }
26
40
  }
@@ -0,0 +1,92 @@
1
+ import { GlossaristModel } from './base.js';
2
+ import { Citation } from './citation.js';
3
+ import { canonicalJson } from '../diff/canonical-json.js';
4
+
5
+ export interface DetailedDefinitionJson {
6
+ content?: string;
7
+ sources?: ReadonlyArray<unknown>;
8
+ examples?: ReadonlyArray<unknown>;
9
+ }
10
+
11
+ export interface WalkTextEntry {
12
+ text: string;
13
+ source: string;
14
+ }
15
+
16
+ export class DetailedDefinition extends GlossaristModel {
17
+ readonly content: string;
18
+ private readonly _rawSources: ReadonlyArray<unknown>;
19
+ private readonly _rawExamples: ReadonlyArray<unknown>;
20
+ private _sources: ReadonlyArray<Citation> | null = null;
21
+ private _examples: ReadonlyArray<DetailedDefinition> | null = null;
22
+
23
+ constructor(data: DetailedDefinitionJson = {}) {
24
+ super();
25
+ this.content = data.content ?? '';
26
+ this._rawSources = data.sources ?? [];
27
+ this._rawExamples = data.examples ?? [];
28
+ }
29
+
30
+ get sources(): ReadonlyArray<Citation> {
31
+ return this._lazy<Citation>(
32
+ '_sources',
33
+ '_rawSources',
34
+ (s) => (s instanceof Citation ? s : new Citation(s as never)),
35
+ );
36
+ }
37
+
38
+ get examples(): ReadonlyArray<DetailedDefinition> {
39
+ return this._lazy<DetailedDefinition>(
40
+ '_examples',
41
+ '_rawExamples',
42
+ (e) =>
43
+ e instanceof DetailedDefinition
44
+ ? e
45
+ : new DetailedDefinition(e as DetailedDefinitionJson),
46
+ );
47
+ }
48
+
49
+ override toJSON(): DetailedDefinitionJson {
50
+ const obj: DetailedDefinitionJson = { content: this.content };
51
+ this._serialize(obj as Record<string, unknown>, 'sources', '_sources', '_rawSources');
52
+ this._serialize(obj as Record<string, unknown>, 'examples', '_examples', '_rawExamples');
53
+ return obj;
54
+ }
55
+
56
+ /**
57
+ * Yield this definition's content and the content of every nested
58
+ * example (recursively). Each item carries `{ text, source }` where
59
+ * `source` is `<path>.content` rooted at the `path` argument.
60
+ */
61
+ *walkTexts(path: string): Generator<WalkTextEntry> {
62
+ if (typeof this.content === 'string' && this.content.length > 0) {
63
+ yield { text: this.content, source: `${path}.content` };
64
+ }
65
+ const examples = this.examples;
66
+ for (let i = 0; i < examples.length; i++) {
67
+ const example = examples[i];
68
+ if (example) {
69
+ yield* example.walkTexts(`${path}.examples[${i}]`);
70
+ }
71
+ }
72
+ }
73
+
74
+ static identityOf(
75
+ value: { toJSON?: () => unknown } | unknown | null,
76
+ ): string {
77
+ if (value == null) return '';
78
+ if (typeof value === 'object' && value !== null && 'toJSON' in value &&
79
+ typeof (value as { toJSON: unknown }).toJSON === 'function') {
80
+ return canonicalJson((value as { toJSON: () => unknown }).toJSON());
81
+ }
82
+ return canonicalJson(value);
83
+ }
84
+
85
+ override identity(): string {
86
+ return DetailedDefinition.identityOf(this);
87
+ }
88
+
89
+ static override fromJSON(data: DetailedDefinitionJson): DetailedDefinition {
90
+ return new DetailedDefinition(data);
91
+ }
92
+ }
@@ -0,0 +1,78 @@
1
+ import { GlossaristModel } from './base.js';
2
+
3
+ export type GrammarGender = 'm' | 'f' | 'n' | 'c' | string;
4
+ export type GrammarNumber = 'singular' | 'dual' | 'plural' | string;
5
+ export type GrammarPartOfSpeech =
6
+ | 'noun'
7
+ | 'verb'
8
+ | 'adj'
9
+ | 'adverb'
10
+ | 'preposition'
11
+ | 'participle'
12
+ | string;
13
+
14
+ export const GRAMMAR_GENDERS: ReadonlyArray<string> = Object.freeze([
15
+ 'm', 'f', 'n', 'c',
16
+ ]);
17
+ export const GRAMMAR_NUMBERS: ReadonlyArray<string> = Object.freeze([
18
+ 'singular', 'dual', 'plural',
19
+ ]);
20
+ export const GRAMMAR_PARTS_OF_SPEECH: ReadonlyArray<string> = Object.freeze([
21
+ 'noun', 'verb', 'adj', 'adverb', 'preposition', 'participle',
22
+ ]);
23
+
24
+ export interface GrammarInfoJson {
25
+ gender?: GrammarGender | null;
26
+ number?: GrammarNumber | null;
27
+ part_of_speech?: GrammarPartOfSpeech | null;
28
+ partOfSpeech?: GrammarPartOfSpeech | null;
29
+ noun?: boolean;
30
+ verb?: boolean;
31
+ adj?: boolean;
32
+ adverb?: boolean;
33
+ preposition?: boolean;
34
+ participle?: boolean;
35
+ }
36
+
37
+ export class GrammarInfo extends GlossaristModel {
38
+ readonly gender: GrammarGender | null;
39
+ readonly number: GrammarNumber | null;
40
+ readonly partOfSpeech: GrammarPartOfSpeech | null;
41
+ readonly noun: boolean;
42
+ readonly verb: boolean;
43
+ readonly adj: boolean;
44
+ readonly adverb: boolean;
45
+ readonly preposition: boolean;
46
+ readonly participle: boolean;
47
+
48
+ constructor(data: GrammarInfoJson = {}) {
49
+ super();
50
+ this.gender = data.gender ?? null;
51
+ this.number = data.number ?? null;
52
+ this.partOfSpeech = data.part_of_speech ?? data.partOfSpeech ?? null;
53
+ this.noun = data.noun ?? false;
54
+ this.verb = data.verb ?? false;
55
+ this.adj = data.adj ?? false;
56
+ this.adverb = data.adverb ?? false;
57
+ this.preposition = data.preposition ?? false;
58
+ this.participle = data.participle ?? false;
59
+ }
60
+
61
+ override toJSON(): GrammarInfoJson {
62
+ const obj: GrammarInfoJson = {};
63
+ if (this.gender != null) obj.gender = this.gender;
64
+ if (this.number != null) obj.number = this.number;
65
+ if (this.partOfSpeech != null) obj.part_of_speech = this.partOfSpeech;
66
+ if (this.noun) obj.noun = true;
67
+ if (this.verb) obj.verb = true;
68
+ if (this.adj) obj.adj = true;
69
+ if (this.adverb) obj.adverb = true;
70
+ if (this.preposition) obj.preposition = true;
71
+ if (this.participle) obj.participle = true;
72
+ return obj;
73
+ }
74
+
75
+ static override fromJSON(data: GrammarInfoJson): GrammarInfo {
76
+ return new GrammarInfo(data);
77
+ }
78
+ }
@@ -1,22 +1,34 @@
1
1
  import { GlossaristModel } from './base.js';
2
2
 
3
+ export interface LocalityJson {
4
+ type?: string | null;
5
+ reference_from?: string | null;
6
+ referenceFrom?: string | null;
7
+ reference_to?: string | null;
8
+ referenceTo?: string | null;
9
+ }
10
+
3
11
  export class Locality extends GlossaristModel {
4
- constructor(data = {}) {
12
+ readonly type: string | null;
13
+ readonly referenceFrom: string | null;
14
+ readonly referenceTo: string | null;
15
+
16
+ constructor(data: LocalityJson = {}) {
5
17
  super();
6
18
  this.type = data.type ?? null;
7
19
  this.referenceFrom = data.reference_from ?? data.referenceFrom ?? null;
8
20
  this.referenceTo = data.reference_to ?? data.referenceTo ?? null;
9
21
  }
10
22
 
11
- toJSON() {
12
- const obj = {};
23
+ override toJSON(): LocalityJson {
24
+ const obj: LocalityJson = {};
13
25
  if (this.type != null) obj.type = this.type;
14
26
  if (this.referenceFrom != null) obj.reference_from = this.referenceFrom;
15
27
  if (this.referenceTo != null) obj.reference_to = this.referenceTo;
16
28
  return obj;
17
29
  }
18
30
 
19
- static fromJSON(data) {
31
+ static override fromJSON(data: LocalityJson): Locality {
20
32
  return new Locality(data);
21
33
  }
22
34
  }
@@ -0,0 +1,32 @@
1
+ const FALLBACK_LANG = 'eng';
2
+
3
+ export type LocalizedStringHash = Readonly<Record<string, string>>;
4
+
5
+ export function fetchLocalizedString(
6
+ hash: LocalizedStringHash | null | undefined,
7
+ lang: string,
8
+ fallback: string | null = FALLBACK_LANG,
9
+ ): string | null {
10
+ if (hash == null || typeof hash !== 'object') return null;
11
+ const direct = hash[lang] ?? hash[String(lang)];
12
+ if (direct != null) return direct;
13
+ if (fallback != null && fallback !== lang) {
14
+ return hash[fallback] ?? hash[String(fallback)] ?? null;
15
+ }
16
+ return null;
17
+ }
18
+
19
+ export function localizedStringIsEmpty(
20
+ hash: LocalizedStringHash | null | undefined,
21
+ ): boolean {
22
+ return (
23
+ hash == null ||
24
+ (typeof hash === 'object' && Object.keys(hash).length === 0)
25
+ );
26
+ }
27
+
28
+ export function localizedStringIsPresent(
29
+ hash: LocalizedStringHash | null | undefined,
30
+ ): boolean {
31
+ return !localizedStringIsEmpty(hash);
32
+ }
@@ -1,7 +1,21 @@
1
1
  import { GlossaristModel } from './base.js';
2
2
 
3
+ export interface PronunciationJson {
4
+ content?: string | null;
5
+ language?: string | null;
6
+ script?: string | null;
7
+ system?: string | null;
8
+ country?: string | null;
9
+ }
10
+
3
11
  export class Pronunciation extends GlossaristModel {
4
- constructor(data = {}) {
12
+ readonly content: string | null;
13
+ readonly language: string | null;
14
+ readonly script: string | null;
15
+ readonly system: string | null;
16
+ readonly country: string | null;
17
+
18
+ constructor(data: PronunciationJson = {}) {
5
19
  super();
6
20
  this.content = data.content ?? null;
7
21
  this.language = data.language ?? null;
@@ -10,8 +24,8 @@ export class Pronunciation extends GlossaristModel {
10
24
  this.country = data.country ?? null;
11
25
  }
12
26
 
13
- toJSON() {
14
- const obj = {};
27
+ override toJSON(): PronunciationJson {
28
+ const obj: PronunciationJson = {};
15
29
  if (this.content != null) obj.content = this.content;
16
30
  if (this.language != null) obj.language = this.language;
17
31
  if (this.script != null) obj.script = this.script;
@@ -20,7 +34,7 @@ export class Pronunciation extends GlossaristModel {
20
34
  return obj;
21
35
  }
22
36
 
23
- static fromJSON(data) {
37
+ static override fromJSON(data: PronunciationJson): Pronunciation {
24
38
  return new Pronunciation(data);
25
39
  }
26
40
  }
@@ -1,48 +0,0 @@
1
- import * as yaml from 'js-yaml';
2
- import { GlossaristModel } from './base.js';
3
- import { BibliographyEntry } from './bibliography-entry.js';
4
-
5
- export class BibliographyData extends GlossaristModel {
6
- constructor(data = {}) {
7
- super();
8
- let entriesData;
9
- if (Array.isArray(data)) {
10
- entriesData = data;
11
- } else {
12
- entriesData = data?.bibliography ?? data?.entries ?? [];
13
- }
14
- this._rawEntries = Array.isArray(entriesData) ? entriesData : [];
15
- this._entries = null;
16
- }
17
-
18
- get entries() {
19
- return this._lazy('_entries', '_rawEntries',
20
- e => e instanceof BibliographyEntry ? e : new BibliographyEntry(e));
21
- }
22
-
23
- find(id) {
24
- return this.entries.find(e => e.id === id) ?? null;
25
- }
26
-
27
- get keys() {
28
- return this.entries.map(e => e.id);
29
- }
30
-
31
- toJSON() {
32
- if (this.entries.length === 0) return { bibliography: [] };
33
- return { bibliography: this.entries.map(e => e.toJSON()) };
34
- }
35
-
36
- toYAML() {
37
- return yaml.dump(this.toJSON());
38
- }
39
-
40
- static fromYAML(yamlString) {
41
- const parsed = yaml.load(yamlString);
42
- return new BibliographyData(parsed ?? {});
43
- }
44
-
45
- static fromJSON(data) {
46
- return new BibliographyData(data);
47
- }
48
- }
@@ -1,59 +0,0 @@
1
- import { GlossaristModel } from './base.js';
2
- import { Citation } from './citation.js';
3
- import { canonicalJson } from '../diff/canonical-json.js';
4
-
5
- export class DetailedDefinition extends GlossaristModel {
6
- constructor(data = {}) {
7
- super();
8
- this.content = data.content ?? '';
9
- this._rawSources = data.sources ?? [];
10
- this._rawExamples = data.examples ?? [];
11
- this._sources = null;
12
- this._examples = null;
13
- }
14
-
15
- get sources() {
16
- return this._lazy('_sources', '_rawSources',
17
- s => s instanceof Citation ? s : new Citation(s));
18
- }
19
-
20
- get examples() {
21
- return this._lazy('_examples', '_rawExamples',
22
- e => e instanceof DetailedDefinition ? e : new DetailedDefinition(e));
23
- }
24
-
25
- toJSON() {
26
- const obj = { content: this.content };
27
- this._serialize(obj, 'sources', '_sources', '_rawSources');
28
- this._serialize(obj, 'examples', '_examples', '_rawExamples');
29
- return obj;
30
- }
31
-
32
- /**
33
- * Yield this definition's content and the content of every nested
34
- * example (recursively). Each item carries `{ text, source }` where
35
- * `source` is `<path>.content` rooted at the `path` argument.
36
- */
37
- *walkTexts(path) {
38
- if (typeof this.content === 'string' && this.content.length > 0) {
39
- yield { text: this.content, source: `${path}.content` };
40
- }
41
- for (let i = 0; i < this.examples.length; i++) {
42
- yield* this.examples[i].walkTexts(`${path}.examples[${i}]`);
43
- }
44
- }
45
-
46
- static identityOf(value) {
47
- if (value == null) return '';
48
- if (typeof value.toJSON === 'function') return canonicalJson(value.toJSON());
49
- return canonicalJson(value);
50
- }
51
-
52
- identity() {
53
- return DetailedDefinition.identityOf(this);
54
- }
55
-
56
- static fromJSON(data) {
57
- return new DetailedDefinition(data);
58
- }
59
- }
@@ -1,40 +0,0 @@
1
- import { GlossaristModel } from './base.js';
2
-
3
- export const GRAMMAR_GENDERS = Object.freeze(['m', 'f', 'n', 'c']);
4
- export const GRAMMAR_NUMBERS = Object.freeze(['singular', 'dual', 'plural']);
5
- export const GRAMMAR_PARTS_OF_SPEECH = Object.freeze([
6
- 'noun', 'verb', 'adj', 'adverb', 'preposition', 'participle',
7
- ]);
8
-
9
- export class GrammarInfo extends GlossaristModel {
10
- constructor(data = {}) {
11
- super();
12
- this.gender = data.gender ?? null;
13
- this.number = data.number ?? null;
14
- this.partOfSpeech = data.part_of_speech ?? data.partOfSpeech ?? null;
15
- this.noun = data.noun ?? false;
16
- this.verb = data.verb ?? false;
17
- this.adj = data.adj ?? false;
18
- this.adverb = data.adverb ?? false;
19
- this.preposition = data.preposition ?? false;
20
- this.participle = data.participle ?? false;
21
- }
22
-
23
- toJSON() {
24
- const obj = {};
25
- if (this.gender != null) obj.gender = this.gender;
26
- if (this.number != null) obj.number = this.number;
27
- if (this.partOfSpeech != null) obj.part_of_speech = this.partOfSpeech;
28
- if (this.noun) obj.noun = true;
29
- if (this.verb) obj.verb = true;
30
- if (this.adj) obj.adj = true;
31
- if (this.adverb) obj.adverb = true;
32
- if (this.preposition) obj.preposition = true;
33
- if (this.participle) obj.participle = true;
34
- return obj;
35
- }
36
-
37
- static fromJSON(data) {
38
- return new GrammarInfo(data);
39
- }
40
- }
@@ -1,19 +0,0 @@
1
- const FALLBACK_LANG = 'eng';
2
-
3
- export function fetchLocalizedString(hash, lang, fallback = FALLBACK_LANG) {
4
- if (hash == null || typeof hash !== 'object') return null;
5
- const direct = hash[lang] ?? hash[String(lang)];
6
- if (direct != null) return direct;
7
- if (fallback != null && fallback !== lang) {
8
- return hash[fallback] ?? hash[String(fallback)] ?? null;
9
- }
10
- return null;
11
- }
12
-
13
- export function localizedStringIsEmpty(hash) {
14
- return hash == null || (typeof hash === 'object' && Object.keys(hash).length === 0);
15
- }
16
-
17
- export function localizedStringIsPresent(hash) {
18
- return !localizedStringIsEmpty(hash);
19
- }