glossarist 0.4.44 → 0.4.46

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.44",
3
+ "version": "0.4.46",
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",
@@ -111,6 +111,7 @@
111
111
  "devDependencies": {
112
112
  "@eslint/js": "^10.0.1",
113
113
  "@rdfjs/types": "^2.0.1",
114
+ "@types/n3": "^1.26.1",
114
115
  "@types/node": "^26.1.2",
115
116
  "@typescript-eslint/eslint-plugin": "^8.65.0",
116
117
  "@typescript-eslint/parser": "^8.65.0",
@@ -1,4 +1,3 @@
1
- // @ts-nocheck — TEMPORARY during TS migration. TODO(Phase 2e): remove and type fully.
2
1
  // Deterministic blank node ID strategy.
3
2
  //
4
3
  // Single source of truth for bnode IDs across all emitters. Previously
@@ -10,7 +9,7 @@
10
9
  // Browser safety: node:crypto is loaded lazily via runtime check so
11
10
  // bundlers don't tree-shake the import into the browser bundle.
12
11
 
13
- let nodeCreateHash = null;
12
+ let nodeCreateHash: ((alg: string) => { update(data: string): { digest(enc: string): string } }) | null = null;
14
13
  let nodeCryptoChecked = false;
15
14
 
16
15
  function getNodeCreateHash() {
@@ -22,7 +21,7 @@ function getNodeCreateHash() {
22
21
  const mod = (Function('m', 'return require(m)'))('node:module');
23
22
  requireFn = mod.createRequire(import.meta.url);
24
23
  } catch {
25
- requireFn = (m) => (Function('m', 'return require(m)'))(m);
24
+ requireFn = (m: string) => (Function('m', 'return require(m)'))(m);
26
25
  }
27
26
  const crypto = requireFn('node:crypto');
28
27
  nodeCreateHash = crypto.createHash.bind(crypto);
@@ -32,7 +31,7 @@ function getNodeCreateHash() {
32
31
  return nodeCreateHash;
33
32
  }
34
33
 
35
- function fnv1a(seed) {
34
+ function fnv1a(seed: string): string {
36
35
  let h = 0x811c9dc5;
37
36
  for (let i = 0; i < seed.length; i++) {
38
37
  h ^= seed.charCodeAt(i);
@@ -41,7 +40,7 @@ function fnv1a(seed) {
41
40
  return (h >>> 0).toString(16).padStart(8, '0');
42
41
  }
43
42
 
44
- export function deterministicBnodeId(...parts) {
43
+ export function deterministicBnodeId(...parts: ReadonlyArray<unknown>): string {
45
44
  const seed = parts.filter(p => p != null && p !== '').join('|');
46
45
  if (!seed) return `b${fnv1a('empty')}`;
47
46
  const createHash = getNodeCreateHash();
package/src/rdf/curie.ts CHANGED
@@ -1,4 +1,3 @@
1
- // @ts-nocheck — TEMPORARY during TS migration. TODO(Phase 2e): remove and type fully.
2
1
  // CURIE helpers — resolve/compact IRIs against the canonical PREFIXES.
3
2
  //
4
3
  // Single source of truth for CURIE ↔ IRI conversion. Previously
@@ -9,7 +8,7 @@ import { PREFIXES } from './prefixes.js';
9
8
 
10
9
  const ABSOLUTE_SCHEMES = new Set(['http', 'https', 'urn', 'file', 'mailto', 'ftp']);
11
10
 
12
- export function isAbsoluteIri(s) {
11
+ export function isAbsoluteIri(s: string): boolean {
13
12
  if (typeof s !== 'string' || s.length === 0) return false;
14
13
  if (s.startsWith('urn:')) return true;
15
14
  const colon = s.indexOf(':');
@@ -17,17 +16,17 @@ export function isAbsoluteIri(s) {
17
16
  return ABSOLUTE_SCHEMES.has(s.slice(0, colon));
18
17
  }
19
18
 
20
- export function resolveIri(iri, prefixes = PREFIXES) {
19
+ export function resolveIri(iri: string, prefixes: Record<string, string> = PREFIXES): string {
21
20
  if (isAbsoluteIri(iri)) return iri;
22
21
  const colon = iri.indexOf(':');
23
22
  if (colon < 1) return iri;
24
23
  const prefix = iri.slice(0, colon);
25
24
  const local = iri.slice(colon + 1);
26
- const base = prefixes[prefix];
25
+ const base = prefixes[prefix as keyof typeof PREFIXES];
27
26
  return base ? `${base}${local}` : iri;
28
27
  }
29
28
 
30
- export function compactIri(iri, prefixes = PREFIXES) {
29
+ export function compactIri(iri: string, prefixes: Record<string, string> = PREFIXES): string {
31
30
  let best = null;
32
31
  let bestLen = 0;
33
32
  for (const [prefix, base] of Object.entries(prefixes)) {
@@ -1,4 +1,3 @@
1
- // @ts-nocheck — TEMPORARY during TS migration. TODO(Phase 2e): remove and type fully.
2
1
  // Stable ID helpers for RDF emission. Mirrors glossarist-ruby's
3
2
  // Digest::MD5.hexdigest(content)[0..11] for cross-runtime stability.
4
3
  //
@@ -7,7 +6,7 @@
7
6
  // `createRequire` from `node:module` (also lazy) which is the
8
7
  // documented way to use CommonJS `require` from ESM.
9
8
 
10
- let nodeCreateHash = null;
9
+ let nodeCreateHash: ((alg: string) => { update(data: string): { digest(enc: string): string } }) | null = null;
11
10
  let nodeCryptoChecked = false;
12
11
 
13
12
  function getNodeCreateHash() {
@@ -23,7 +22,7 @@ function getNodeCreateHash() {
23
22
  requireFn = mod.createRequire(import.meta.url);
24
23
  } catch {
25
24
  // CJS or already-require-available environment
26
- requireFn = (m) => (Function('m', 'return require(m)'))(m);
25
+ requireFn = (m: string) => (Function('m', 'return require(m)'))(m);
27
26
  }
28
27
  const crypto = requireFn('node:crypto');
29
28
  nodeCreateHash = crypto.createHash.bind(crypto);
@@ -37,7 +36,7 @@ function getNodeCreateHash() {
37
36
  // `Digest::MD5.hexdigest(content)[0..11]`. Same content → same ID across
38
37
  // processes, machines, and language runtimes (Ruby vs JS) when MD5 is
39
38
  // available. Falls back to a 12-char FNV-1a hash in the browser.
40
- export function deterministicId(...parts) {
39
+ export function deterministicId(...parts: ReadonlyArray<unknown>): string {
41
40
  const seed = parts.filter(p => p !== null && p !== undefined).join('|');
42
41
  const createHash = getNodeCreateHash();
43
42
  if (createHash) {
@@ -48,14 +47,12 @@ export function deterministicId(...parts) {
48
47
 
49
48
  // Stable blank-node label for an RDF fragment identified by its parent
50
49
  // subject + role + index.
51
- export function deterministicBnode(subject, role, index) {
50
+ export function deterministicBnode(subject: string, role: string, index: number): string {
52
51
  return `_:b${deterministicId(subject, role, index)}`;
53
52
  }
54
53
 
55
- // 12-char FNV-1a hash. Two 32-bit passes concatenated and truncated.
56
- // Not as collision-resistant as MD5 but adequate for bnode IDs in a
57
- // single document. Used only when node:crypto is unavailable.
58
- function fnv1a12(seed) {
54
+ // 12-char FNV-1a hash.
55
+ function fnv1a12(seed: string): string {
59
56
  let h1 = 0x811c9dc5;
60
57
  for (let i = 0; i < seed.length; i++) {
61
58
  h1 ^= seed.charCodeAt(i);
@@ -1,4 +1,3 @@
1
- // @ts-nocheck — TEMPORARY during TS migration. TODO(Phase 2e): remove and type fully.
2
1
  // Helpers for normalizing enumeration values that get interpolated into
3
2
  // URIs. Source data may contain compound forms ("preferred/admitted") or
4
3
  // path-like values that must be reduced to a single local name before
@@ -10,15 +9,14 @@
10
9
  // 'preferred/admitted' → 'admitted'
11
10
  // 'http://x/ns#preferred' → 'preferred'
12
11
  // '' / null / undefined → '' (caller decides whether to skip emission)
13
- export function normalizeEnum(value) {
12
+ export function normalizeEnum(value: string | null | undefined): string {
14
13
  if (value == null) return '';
15
14
  const s = String(value).trim();
16
15
  if (!s) return '';
17
16
  const parts = s.split(/[/#]/);
18
- return parts[parts.length - 1];
17
+ return parts[parts.length - 1] ?? '';
19
18
  }
20
19
 
21
- // True when normalizeEnum would produce a non-empty token.
22
- export function hasEnumValue(value) {
20
+ export function hasEnumValue(value: string | null | undefined): boolean {
23
21
  return normalizeEnum(value).length > 0;
24
22
  }
@@ -1,4 +1,3 @@
1
- // @ts-nocheck — TEMPORARY during TS migration. TODO(Phase 2e): remove and type fully.
2
1
  // Re-export prefix map and predicate constants generated from the vendored
3
2
  // concept-model JSON-LD context. Downstream code should never duplicate
4
3
  // prefix bindings — always import from here.
package/src/rdf/terms.ts CHANGED
@@ -1,4 +1,3 @@
1
- // @ts-nocheck — TEMPORARY during TS migration. TODO(Phase 2e): remove and type fully.
2
1
  // Shared RDF/JS term constructors. Replaces the per-emitter
3
2
  // `const { namedNode, literal, quad } = DataFactory;` pattern that was
4
3
  // repeated across the RDF layer.
@@ -1,16 +1,19 @@
1
- // @ts-nocheck — TEMPORARY during TS migration. TODO(Phase 2e): remove and type fully.
2
1
  export class ValidationError {
3
- constructor(path, message, severity = 'error') {
2
+ readonly path: string;
3
+ readonly message: string;
4
+ readonly severity: 'error' | 'warning';
5
+
6
+ constructor(path: string, message: string, severity: 'error' | 'warning' = 'error') {
4
7
  this.path = path;
5
8
  this.message = message;
6
9
  this.severity = severity;
7
10
  }
8
11
 
9
- toString() {
12
+ toString(): string {
10
13
  return `[${this.severity.toUpperCase()}] ${this.path}: ${this.message}`;
11
14
  }
12
15
 
13
- toJSON() {
16
+ toJSON(): { path: string; message: string; severity: string } {
14
17
  return { path: this.path, message: this.message, severity: this.severity };
15
18
  }
16
19
  }
@@ -1,53 +1,52 @@
1
- // @ts-nocheck — TEMPORARY during TS migration. TODO(Phase 2e): remove and type fully.
2
1
  import { ValidationError } from './validation-error.js';
3
2
 
4
3
  export class ValidationResult {
5
- constructor() {
6
- this._issues = [];
7
- }
4
+ private readonly _issues: ValidationError[] = [];
5
+
6
+ constructor() {}
8
7
 
9
- get valid() {
8
+ get valid(): boolean {
10
9
  return this._issues.filter(e => e.severity === 'error').length === 0;
11
10
  }
12
11
 
13
- get errors() {
12
+ get errors(): ReadonlyArray<ValidationError> {
14
13
  return this._issues.filter(e => e.severity === 'error');
15
14
  }
16
15
 
17
- get warnings() {
16
+ get warnings(): ReadonlyArray<ValidationError> {
18
17
  return this._issues.filter(e => e.severity === 'warning');
19
18
  }
20
19
 
21
- addError(pathOrMessage, message) {
20
+ addError(path: string, message?: string): this {
22
21
  if (message === undefined) {
23
- this._issues.push(new ValidationError('', pathOrMessage, 'error'));
22
+ this._issues.push(new ValidationError('', path, 'error'));
24
23
  } else {
25
- this._issues.push(new ValidationError(pathOrMessage, message, 'error'));
24
+ this._issues.push(new ValidationError(path, message, 'error'));
26
25
  }
27
26
  return this;
28
27
  }
29
28
 
30
- addWarning(pathOrMessage, message) {
29
+ addWarning(path: string, message?: string): this {
31
30
  if (message === undefined) {
32
- this._issues.push(new ValidationError('', pathOrMessage, 'warning'));
31
+ this._issues.push(new ValidationError('', path, 'warning'));
33
32
  } else {
34
- this._issues.push(new ValidationError(pathOrMessage, message, 'warning'));
33
+ this._issues.push(new ValidationError(path, message, 'warning'));
35
34
  }
36
35
  return this;
37
36
  }
38
37
 
39
- merge(other) {
38
+ merge(other: ValidationResult): this {
40
39
  if (other instanceof ValidationResult) {
41
40
  for (const issue of other._issues) this._issues.push(issue);
42
41
  }
43
42
  return this;
44
43
  }
45
44
 
46
- toJSON() {
45
+ toJSON(): { valid: boolean; errors: unknown[]; warnings: unknown[] } {
47
46
  return {
48
47
  valid: this.valid,
49
- errors: this.errors.map(e => e.toJSON ? e.toJSON() : e),
50
- warnings: this.warnings.map(e => e.toJSON ? e.toJSON() : e),
48
+ errors: this.errors.map(e => typeof e.toJSON === 'function' ? e.toJSON() : e),
49
+ warnings: this.warnings.map(e => typeof e.toJSON === 'function' ? e.toJSON() : e),
51
50
  };
52
51
  }
53
52
  }
@@ -1,15 +1,17 @@
1
- // @ts-nocheck — TEMPORARY during TS migration. TODO(Phase 2e): remove and type fully.
2
- export class ValidationRule {
3
- constructor(name, severity = 'error') {
1
+ import type { ValidationResult } from './validation-result.js';
2
+
3
+ export abstract class ValidationRule {
4
+ readonly name: string;
5
+ readonly severity: 'error' | 'warning';
6
+
7
+ constructor(name: string, severity: 'error' | 'warning' = 'error') {
4
8
  this.name = name;
5
9
  this.severity = severity;
6
10
  }
7
11
 
8
- validate(_value, _path, _result) {
9
- throw new Error(`${this.constructor.name} must implement validate()`);
10
- }
12
+ abstract validate(concept: unknown, path: string, result: ValidationResult): void;
11
13
 
12
- addIssue(result, path, message) {
14
+ addIssue(result: ValidationResult, path: string, message: string): void {
13
15
  if (this.severity === 'warning') {
14
16
  result.addWarning(path, message);
15
17
  } else {