@pubid/pubid 0.1.42 → 0.1.44

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.
Binary file
@@ -1,4 +1,6 @@
1
1
  import type { CorpusCase, FlavorStatus } from "./types.js";
2
+ /** Parse corpus YAML under the psych wire contract (see above). */
3
+ export declare function parseCorpusYaml(text: string): unknown;
2
4
  export interface FlavorPayloads {
3
5
  /** Non-underscore payload files: canonical cases + aliases. */
4
6
  cases: CorpusCase[];
@@ -1,13 +1,56 @@
1
1
  import { readdirSync, readFileSync, existsSync, statSync } from "node:fs";
2
2
  import { join } from "node:path";
3
3
  import { parse as parseYaml } from "yaml";
4
+ /**
5
+ * The corpus files are written by the ruby exporter's psych dump, so
6
+ * their wire form carries YAML 1.1 scalar resolution: psych keeps
7
+ * exponent spellings without a dot (`102E-42`, `45E-36`) as Strings,
8
+ * while the npm yaml package's 1.2 core schema floats them — the
9
+ * divergence behind pubid-ts#177. Loading the corpus is the boundary
10
+ * where the parse type is decided, so the loader reads it back under
11
+ * psych's float contract instead of patching serialization per site:
12
+ * a float needs the dot, and where psych keeps a String the loader
13
+ * keeps the String too. Other 1.1-vs-1.2 classes (legacy octal
14
+ * `0777`, `yes`-bool) never occur in the corpus and are left on the
15
+ * package default.
16
+ */
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- the yaml
18
+ // package's Tags union makes a discriminating map awkward; the shapes
19
+ // below are the package's own ScalarTag objects with test/resolve swapped.
20
+ const psychFloatTags = (tags) => tags.map((tag) => {
21
+ if (tag?.tag !== "tag:yaml.org,2002:float")
22
+ return tag;
23
+ const source = String(tag.test?.source);
24
+ if (source.includes("inf"))
25
+ return tag; // floatNaN: .inf/.nan as psych has them
26
+ if (tag.format === "EXP") {
27
+ // The exponent form: psych requires the dot AND the exponent sign
28
+ // (`1.0E-5` floats; `102E-42`, `1.5e3` stay Strings).
29
+ return {
30
+ ...tag,
31
+ test: /^[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*[eE][-+][0-9]+$/,
32
+ resolve: (str) => parseFloat(str.replace(/_/g, "")),
33
+ };
34
+ }
35
+ // The decimal form: dot required (psych accepts a bare `.5`, an
36
+ // optional fraction `1.`, and `1_000.5`-style underscores).
37
+ return {
38
+ ...tag,
39
+ test: /^[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*$/,
40
+ resolve: (str) => parseFloat(str.replace(/_/g, "")),
41
+ };
42
+ });
43
+ /** Parse corpus YAML under the psych wire contract (see above). */
44
+ export function parseCorpusYaml(text) {
45
+ return parseYaml(text, { customTags: psychFloatTags });
46
+ }
4
47
  /**
5
48
  * The wire schema uses snake_case for exactly one field
6
49
  * (non_normalized_aliases); the loader is the boundary that normalises
7
50
  * it, so a missed mapping can never silently disable a check.
8
51
  */
9
52
  function loadPayload(path) {
10
- const doc = parseYaml(readFileSync(path, "utf8"));
53
+ const doc = parseCorpusYaml(readFileSync(path, "utf8"));
11
54
  if (!Array.isArray(doc))
12
55
  return [];
13
56
  return doc.map((row) => {
@@ -26,7 +69,7 @@ function loadStatus(dir) {
26
69
  if (!existsSync(path)) {
27
70
  throw new Error(`missing _status.yaml in ${dir} - every flavor has one`);
28
71
  }
29
- const doc = parseYaml(readFileSync(path, "utf8"));
72
+ const doc = parseCorpusYaml(readFileSync(path, "utf8"));
30
73
  if (typeof doc.clean !== "boolean") {
31
74
  throw new Error(`malformed _status.yaml in ${dir}: clean must be boolean`);
32
75
  }
@@ -0,0 +1,3 @@
1
+ import type { FlavorImplementation } from "../../conformance/implementation.js";
2
+ export * from "./model.js";
3
+ export declare function ccsdsGrammarImplementation(): FlavorImplementation;
@@ -0,0 +1,61 @@
1
+ import { ParseFailed } from "../../grammar/engine.js";
2
+ import { PubidRuntime } from "../../parg/runtime.js";
3
+ import { CcsdsBaseIdentifier, CcsdsCorrigendumIdentifier } from "./model.js";
4
+ export * from "./model.js";
5
+ /**
6
+ * The ccsds parse entry (pubid-ts#178): the grammar runtime is the SAME
7
+ * baked artifact the Ruby flavor runs (pubid-grammar
8
+ * grammars/ccsds.parg -> artifacts/ccsds.json, the bindings carry flat
9
+ * strings), loaded through the artifact runtime, so grammar changes
10
+ * land once and this side cannot drift. The builder mirrors
11
+ * Pubid::Ccsds::Builder: corrigenda fold last-wraps-first onto the
12
+ * base; the update-code line normalizes first (Core::UpdateCodes).
13
+ */
14
+ // data/ccsds/update_codes.yaml — pre-parse normalizations (full-line).
15
+ const UPDATE_CODE_LINES = {
16
+ "CCSDS 320.0-B-1-S Corrigendum 1": "CCSDS 320.0-B-1-S Cor. 1",
17
+ };
18
+ function applyUpdateCodes(input) {
19
+ return UPDATE_CODE_LINES[input] ?? input;
20
+ }
21
+ // --- Builder -----------------------------------------------------------------
22
+ const s = (v) => {
23
+ if (v === undefined || v === null)
24
+ return undefined;
25
+ const str = String(v);
26
+ return str.length > 0 ? str : undefined;
27
+ };
28
+ function buildBase(data) {
29
+ return new CcsdsBaseIdentifier({
30
+ number: s(data["number"]) ?? "",
31
+ part: s(data["part"]),
32
+ type: s(data["type"]),
33
+ edition: s(data["edition"]),
34
+ suffix: s(data["suffix"]),
35
+ language: s(data["language"]),
36
+ });
37
+ }
38
+ /** ruby Builder.build: corrigenda fold last-wraps-first onto the base. */
39
+ function build(data) {
40
+ const corrigenda = data["corrigenda"];
41
+ if (Array.isArray(corrigenda) && corrigenda.length > 0) {
42
+ const base = buildBase({ ...data, corrigenda: undefined });
43
+ return corrigenda.reduce((current, c) => new CcsdsCorrigendumIdentifier({ base: current, number: s(c["cor_number"]) ?? "" }), base);
44
+ }
45
+ return buildBase(data);
46
+ }
47
+ // --- Implementation ----------------------------------------------------------
48
+ let cachedRuntime;
49
+ export function ccsdsGrammarImplementation() {
50
+ return {
51
+ parse(input) {
52
+ cachedRuntime ??= PubidRuntime.load("ccsds");
53
+ const normalized = applyUpdateCodes(input);
54
+ const bound = cachedRuntime.parseAndBind(normalized);
55
+ if (bound === null || typeof bound !== "object") {
56
+ throw new ParseFailed("ccsds: the artifact rejected the input", 0);
57
+ }
58
+ return build(bound);
59
+ },
60
+ };
61
+ }
@@ -0,0 +1,66 @@
1
+ import { BaseIdentifier } from "../../model/identifier.js";
2
+ import { BaseUrnGenerator } from "../../model/urn-generator.js";
3
+ /**
4
+ * The ccsds model declarations (pubid-ts#178) — the parg-free
5
+ * registration module (the worker manifest imports THIS file; the
6
+ * artifact-backed parse entry lives on the flavor index). 1:1 port of
7
+ * lib/pubid/ccsds/identifier.rb + identifiers/ + urn_generator.rb.
8
+ *
9
+ * Faces pinned by the reference: Base#to_s composes by hand ("CCSDS "
10
+ * publisher lead); UrnGenerator#generate_base_urn assembles
11
+ * `urn:ccsds:<number>[.part][-type][-edition][-suffix]`, with the
12
+ * supplement chain emitting `:cor.<n>` segments. A language face makes
13
+ * the reference's own to_urn RAISE (`language&.code` on a plain String —
14
+ * NoMethodError; the corpus exporter omits the `urn` representation for
15
+ * those rows exactly because of this) — mirrored, not papered over.
16
+ */
17
+ /** ruby UrnGenerator — the base body is ONE segment; supplements chain
18
+ * `:cor.<n>` segments onto it. */
19
+ export declare class CcsdsUrnGenerator extends BaseUrnGenerator<CcsdsIdentifier> {
20
+ generate(): string;
21
+ private generateBaseUrn;
22
+ private generateSupplementUrn;
23
+ }
24
+ /**
25
+ * Abstract root for every CCSDS identifier AND the polymorphic type the
26
+ * supplements' `base` attribute points at — never itself a serialized
27
+ * type (absent from the registry, like ruby's CCSDS_TYPE_MAP omits it;
28
+ * that is what breaks the supplement recursion). Every code component is
29
+ * a plain string, overriding the inherited component types.
30
+ */
31
+ export declare abstract class CcsdsIdentifier extends BaseIdentifier {
32
+ static attributes: import("../../model/attribute.js").AttributeTable;
33
+ static subsetStrict: string[];
34
+ static mappings: import("../../model/attribute.js").FieldMapping[];
35
+ readonly number: string;
36
+ readonly part: string | undefined;
37
+ readonly type: string | undefined;
38
+ readonly edition: string | undefined;
39
+ readonly suffix: string | undefined;
40
+ readonly language: string | undefined;
41
+ }
42
+ /** The published standard (ruby Identifiers::Base). */
43
+ export declare class CcsdsBaseIdentifier extends CcsdsIdentifier {
44
+ static polymorphicName: string;
45
+ static urnGenerator: typeof CcsdsUrnGenerator;
46
+ /** ruby Base#to_s: hand composition, publisher lead hard-coded. */
47
+ render(): string;
48
+ }
49
+ /**
50
+ * Base class for CCSDS supplements; `base` points at the abstract root
51
+ * (never a concrete subclass of itself — the recursion break).
52
+ */
53
+ export declare abstract class CcsdsSupplementIdentifier extends CcsdsIdentifier {
54
+ static attributes: import("../../model/attribute.js").AttributeTable;
55
+ static mappings: import("../../model/attribute.js").FieldMapping[];
56
+ readonly base: CcsdsIdentifier | undefined;
57
+ }
58
+ /** The corrigendum (ruby Identifiers::Corrigendum); its `number` is the
59
+ * corrigendum sequence, mirroring ISO. */
60
+ export declare class CcsdsCorrigendumIdentifier extends CcsdsSupplementIdentifier {
61
+ static polymorphicName: string;
62
+ static mappings: import("../../model/attribute.js").FieldMapping[];
63
+ static urnGenerator: typeof CcsdsUrnGenerator;
64
+ /** ruby Corrigendum#to_s: the base spelling, then " Cor. n". */
65
+ render(): string;
66
+ }
@@ -0,0 +1,136 @@
1
+ import { BaseIdentifier, registerType } from "../../model/identifier.js";
2
+ import { extendAttributes, keyValue } from "../../model/attribute.js";
3
+ import { BaseUrnGenerator } from "../../model/urn-generator.js";
4
+ /**
5
+ * The ccsds model declarations (pubid-ts#178) — the parg-free
6
+ * registration module (the worker manifest imports THIS file; the
7
+ * artifact-backed parse entry lives on the flavor index). 1:1 port of
8
+ * lib/pubid/ccsds/identifier.rb + identifiers/ + urn_generator.rb.
9
+ *
10
+ * Faces pinned by the reference: Base#to_s composes by hand ("CCSDS "
11
+ * publisher lead); UrnGenerator#generate_base_urn assembles
12
+ * `urn:ccsds:<number>[.part][-type][-edition][-suffix]`, with the
13
+ * supplement chain emitting `:cor.<n>` segments. A language face makes
14
+ * the reference's own to_urn RAISE (`language&.code` on a plain String —
15
+ * NoMethodError; the corpus exporter omits the `urn` representation for
16
+ * those rows exactly because of this) — mirrored, not papered over.
17
+ */
18
+ /** ruby UrnGenerator — the base body is ONE segment; supplements chain
19
+ * `:cor.<n>` segments onto it. */
20
+ export class CcsdsUrnGenerator extends BaseUrnGenerator {
21
+ generate() {
22
+ if (this.identifier instanceof CcsdsSupplementIdentifier) {
23
+ return this.generateSupplementUrn(this.identifier);
24
+ }
25
+ return this.generateBaseUrn(this.identifier);
26
+ }
27
+ generateBaseUrn(id) {
28
+ const parts = ["urn", "ccsds"];
29
+ const identifierParts = [];
30
+ if (id.number)
31
+ identifierParts.push(id.number);
32
+ if (id.part)
33
+ identifierParts.push(`.${id.part}`);
34
+ // book_color never populates on the modern hierarchy; type carries
35
+ // the letter (ruby: `elsif identifier.type`).
36
+ if (id.type)
37
+ identifierParts.push(`-${id.type}`);
38
+ if (id.edition)
39
+ identifierParts.push(`-${id.edition}`);
40
+ // ruby: retired ? "-S" : suffix — retired never populates here.
41
+ if (id.suffix)
42
+ identifierParts.push(`-${id.suffix}`);
43
+ if (identifierParts.length > 0)
44
+ parts.push(identifierParts.join(""));
45
+ if (id.language !== undefined) {
46
+ // Mirrored reference failure: `language&.code` on a plain String
47
+ // raises NoMethodError in ruby (the exporter omits these URNs).
48
+ throw new TypeError("ccsds: the reference raises on a language face (undefined method 'code' for String); no URN exists for it");
49
+ }
50
+ return parts.join(":");
51
+ }
52
+ generateSupplementUrn(supp) {
53
+ const parts = ["urn", "ccsds"];
54
+ const chain = [];
55
+ let current = supp;
56
+ while (current instanceof CcsdsSupplementIdentifier) {
57
+ chain.unshift(current);
58
+ current = current.base;
59
+ }
60
+ if (current === undefined) {
61
+ throw new TypeError("ccsds: a supplement without a base has no URN body");
62
+ }
63
+ const baseUrn = this.generateBaseUrn(current);
64
+ parts.push(baseUrn.replace(/^urn:ccsds:/, ""));
65
+ for (const link of chain) {
66
+ if (link.number !== undefined && link.number !== "") {
67
+ parts.push(`cor.${link.number}`);
68
+ }
69
+ }
70
+ return parts.join(":");
71
+ }
72
+ }
73
+ /**
74
+ * Abstract root for every CCSDS identifier AND the polymorphic type the
75
+ * supplements' `base` attribute points at — never itself a serialized
76
+ * type (absent from the registry, like ruby's CCSDS_TYPE_MAP omits it;
77
+ * that is what breaks the supplement recursion). Every code component is
78
+ * a plain string, overriding the inherited component types.
79
+ */
80
+ export class CcsdsIdentifier extends BaseIdentifier {
81
+ static attributes = extendAttributes(BaseIdentifier, {
82
+ edition: { type: "string" },
83
+ type: { type: "string" },
84
+ suffix: { type: "string" },
85
+ language: { type: "string" },
86
+ });
87
+ // ruby subset_strict :language / :suffix — a nil language means the
88
+ // original English document, a nil suffix the current document.
89
+ static subsetStrict = ["language", "suffix"];
90
+ static mappings = keyValue({ wire: "number", to: "number" }, { wire: "part", to: "part" }, { wire: "edition", to: "edition" }, { wire: "type", to: "type" }, { wire: "suffix", to: "suffix" }, { wire: "language", to: "language" });
91
+ }
92
+ /** The published standard (ruby Identifiers::Base). */
93
+ export class CcsdsBaseIdentifier extends CcsdsIdentifier {
94
+ static polymorphicName = "pubid:ccsds:base";
95
+ static urnGenerator = CcsdsUrnGenerator;
96
+ /** ruby Base#to_s: hand composition, publisher lead hard-coded. */
97
+ render() {
98
+ let result = `CCSDS ${this.number ?? ""}`;
99
+ if (this.part !== undefined)
100
+ result += `.${this.part}`;
101
+ if (this.type !== undefined)
102
+ result += `-${this.type}`;
103
+ if (this.edition !== undefined)
104
+ result += `-${this.edition}`;
105
+ if (this.suffix !== undefined)
106
+ result += `-${this.suffix}`;
107
+ if (this.language !== undefined)
108
+ result += ` - ${this.language} Translated`;
109
+ return result;
110
+ }
111
+ }
112
+ registerType(CcsdsBaseIdentifier);
113
+ /**
114
+ * Base class for CCSDS supplements; `base` points at the abstract root
115
+ * (never a concrete subclass of itself — the recursion break).
116
+ */
117
+ export class CcsdsSupplementIdentifier extends CcsdsIdentifier {
118
+ static attributes = extendAttributes(CcsdsIdentifier, {
119
+ base: { type: CcsdsIdentifier },
120
+ });
121
+ static mappings = keyValue({ wire: "base", to: "base" });
122
+ }
123
+ /** The corrigendum (ruby Identifiers::Corrigendum); its `number` is the
124
+ * corrigendum sequence, mirroring ISO. */
125
+ export class CcsdsCorrigendumIdentifier extends CcsdsSupplementIdentifier {
126
+ static polymorphicName = "pubid:ccsds:corrigendum";
127
+ // The merged mapping list (lutaml semantics): the root's code fields
128
+ // first (a supplement's own stay nil), then base.
129
+ static mappings = keyValue({ wire: "number", to: "number" }, { wire: "part", to: "part" }, { wire: "edition", to: "edition" }, { wire: "type", to: "type" }, { wire: "suffix", to: "suffix" }, { wire: "language", to: "language" }, { wire: "base", to: "base" });
130
+ static urnGenerator = CcsdsUrnGenerator;
131
+ /** ruby Corrigendum#to_s: the base spelling, then " Cor. n". */
132
+ render() {
133
+ return `${this.base?.render() ?? ""} Cor. ${this.number ?? ""}`;
134
+ }
135
+ }
136
+ registerType(CcsdsCorrigendumIdentifier);
@@ -28,6 +28,7 @@ import { cieGrammarImplementation } from "./cie/index.js";
28
28
  import { bipmGrammarImplementation } from "./bipm/index.js";
29
29
  import { isoGrammarImplementation } from "./iso/index.js";
30
30
  import { nistGrammarImplementation } from "./nist/index.js";
31
+ import { ccsdsGrammarImplementation } from "./ccsds/index.js";
31
32
  import { ieeeGrammarImplementation } from "./ieee/index.js";
32
33
  import { cenCenelecGrammarImplementation } from "./cen_cenelec/index.js";
33
34
  import { astmGrammarImplementation } from "./astm/index.js";
@@ -136,6 +137,8 @@ function grammarImplementationSwitch(flavor) {
136
137
  return isoGrammarImplementation();
137
138
  case "nist":
138
139
  return nistGrammarImplementation();
140
+ case "ccsds":
141
+ return ccsdsGrammarImplementation();
139
142
  case "ieee":
140
143
  return ieeeGrammarImplementation();
141
144
  case "cen_cenelec":
@@ -393,20 +393,18 @@ export class NistIdentifier extends BaseIdentifier {
393
393
  static mappings = keyValue(scalarCode("series", NistCode), scalarCode("subseries", NistCode), { wire: "publisher", to: "publisher" }, {
394
394
  wire: "number",
395
395
  to: "number",
396
- toWire: (m) => {
397
- const v = m["number"];
398
- if (typeof v === "string" &&
399
- (/^\d+E-\d+$/.test(v) || /^[+-]?\d+(\.\d+)?[eE][+-]?\d+$/.test(v))) {
400
- return Number(v);
401
- }
402
- return v;
403
- },
396
+ // The wire face is the String ruby keeps (psych never floats a
397
+ // dot-less exponent spelling, pubid-ts#177): no numeric coercion
398
+ // here — a hash carrying a Float is non-canonical input and the
399
+ // URN schema (string-kind) rejects it.
400
+ toWire: (m) => m["number"],
404
401
  fromWire: (h) => {
405
402
  const v = h["number"];
406
403
  if (v === undefined || v === null)
407
404
  return undefined;
408
- // A numeric value came from the exporter's YAML float coercion —
409
- // keep it so the round trip stays idempotent.
405
+ // A numeric value came from a non-psych YAML load — keep it so
406
+ // the round trip stays idempotent (ruby does the same: the
407
+ // attribute carries whatever the hash held).
410
408
  return typeof v === "number" ? v : String(v);
411
409
  },
412
410
  }, { wire: "parsed_format", to: "parsed_format" }, { wire: "publisher_was_parsed", to: "publisher_was_parsed" }, ...componentWire("edition"), ...componentWire("part"), ...componentWire("supplement"), ...componentWire("stage"), ...componentWire("version_component"), ...componentWire("update_component"), ...componentWire("update"), ...componentWire("translation_component"), ...componentWire("issue_number"), {
@@ -54,6 +54,7 @@ import "./oiml/model.js";
54
54
  import "./ansi/index.js";
55
55
  import "./bipm/index.js";
56
56
  import "./calconnect/index.js";
57
+ import "./ccsds/model.js";
57
58
  import "./doi/index.js";
58
59
  import "./easc/index.js";
59
60
  import "./ecma/index.js";
@@ -56,6 +56,11 @@ import "./oiml/model.js";
56
56
  import "./ansi/index.js";
57
57
  import "./bipm/index.js";
58
58
  import "./calconnect/index.js";
59
+ // ccsds (pubid-ts#178): the registration classes live on ccsds/model.js
60
+ // (parg-free); the index re-exports them beside the artifact-backed
61
+ // parse entry — but the index itself reaches parg/runtime.js, so the
62
+ // manifest imports the MODEL module like the model-registrant flavors.
63
+ import "./ccsds/model.js";
59
64
  import "./doi/index.js";
60
65
  import "./easc/index.js";
61
66
  import "./ecma/index.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pubid/pubid",
3
- "version": "0.1.42",
3
+ "version": "0.1.44",
4
4
  "description": "TypeScript PubID identifiers: resolution, normalization and canonical hashes for every identifier in the pubid-testsuite corpus (41 standards bodies), with a conformance runner mirroring the Ruby reference",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -19,7 +19,7 @@
19
19
  "doc:api": "node scripts/api-reference.mjs"
20
20
  },
21
21
  "dependencies": {
22
- "parsanol": "0.10.46",
22
+ "parsanol": "0.10.51",
23
23
  "yaml": "^2.5.0"
24
24
  },
25
25
  "devDependencies": {