@pubid/pubid 0.1.65 → 0.1.66

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/README.md CHANGED
@@ -110,6 +110,41 @@ ships per flavor where the Ruby reference implements a URN parser
110
110
  (oiml today; the remaining flavors throw a clear "not yet supported"
111
111
  error naming this issue until their ports land).
112
112
 
113
+ ## The parse surface: one call per parse concern
114
+
115
+ pubid/pubid-ts#226 exports the full parse surface from the package root
116
+ so consumers — relaton-api-js first (their #176/#182/#165) — can drop
117
+ their internal derivation entirely. Every export mirrors the Ruby
118
+ reference's semantics; the lane keys are the corpus lanes (the goldens
119
+ their key generator computes) and are parity-tested against them:
120
+
121
+ ```ts
122
+ import { parse, parseOk, render, laneKeys, ParseError } from "@pubid/pubid";
123
+
124
+ const r = parse("ISO 19115-1:2014", "iso");
125
+ r.components; // the structured components: the reference's canonical
126
+ // toHash record, every facet typed per the corpus schema
127
+ r.canonical; // the canonical human spelling
128
+ r.urn; // the URN (undefined when the flavor emits none)
129
+ r.year; r.part; // the year and the structural part (base-inherited)
130
+ r.keys; // { dated: "ISO19115-1:2014", undated: "ISO19115-1",
131
+ // all_parts: "ISO19115" }
132
+
133
+ render("ISO 9001", "iso"); // "ISO 9001" — the canonical render
134
+ parseOk("ISO 9001", "iso"); // true — the verdict helper; never throws
135
+ parseOk("garbage", "iso"); // false — rejections raise ParseError
136
+ ```
137
+
138
+ The lane keys: `dated` (the norm — the canonical uppercased and
139
+ de-spaced), `undated` (every colon-year not followed by a dash is gone,
140
+ base and supplement alike) and `all_parts` (the parsed part stripped —
141
+ structurally only, never by guessing). The derivation helpers
142
+ `norm` / `undatedNorm` / `allPartsNorm` compose into them and are
143
+ exported individually. The flavor must be named: grammar-ported flavors
144
+ parse open-endedly; the rest resolve over the bundled corpus. `parse`
145
+ raises `ParseError` (with `input`, `flavor` and the wrapped engine
146
+ `cause`) on every rejection.
147
+
113
148
  ## Worker-safe registration: the pubid-scheme URN codec
114
149
 
115
150
  The package root (and the grammar barrel `dist/flavors/index.js` it
package/dist/index.d.ts CHANGED
@@ -8,4 +8,6 @@ export { checkCase, runFlavor, runCorpus } from "./conformance/runner.js";
8
8
  export { corpusModeImplementation, CorpusBackedIdentifier } from "./implementations/corpus-mode.js";
9
9
  export { grammarImplementation } from "./flavors/index.js";
10
10
  export { subsetMatch, valueMatch, exactMatch } from "./model/subset-match.js";
11
+ export { parse, parseOk, render, laneKeys, norm, undatedNorm, allPartsNorm, ParseError, } from "./parse-surface.js";
12
+ export type { ParseResult, LaneKeys } from "./parse-surface.js";
11
13
  export * as PubidUrn from "./pubid-urn/index.js";
package/dist/index.js CHANGED
@@ -8,5 +8,8 @@ export { corpusModeImplementation, CorpusBackedIdentifier } from "./implementati
8
8
  // (pubid/pubid-ts#63 - the estate serving codec).
9
9
  export { grammarImplementation } from "./flavors/index.js";
10
10
  export { subsetMatch, valueMatch, exactMatch } from "./model/subset-match.js";
11
+ // The parse surface: structured components, canonical render, the lane
12
+ // keys and the parse_ok verdict (pubid/pubid-ts#226).
13
+ export { parse, parseOk, render, laneKeys, norm, undatedNorm, allPartsNorm, ParseError, } from "./parse-surface.js";
11
14
  // The pubid-scheme URN codec and the slug form (pubid-ts#161).
12
15
  export * as PubidUrn from "./pubid-urn/index.js";
@@ -0,0 +1,96 @@
1
+ /** The failure class of the parse surface: every rejected input and
2
+ * unknown flavor raises this (wrapped engine errors ride as `cause`). */
3
+ export declare class ParseError extends Error {
4
+ /** The rejected input. */
5
+ readonly input: string;
6
+ /** The flavor the parse ran under, when one was named. */
7
+ readonly flavor: string | undefined;
8
+ constructor(message: string, opts: {
9
+ input: string;
10
+ flavor?: string;
11
+ cause?: unknown;
12
+ });
13
+ }
14
+ /** The lane keys of one identifier: the lookup lanes the corpus and the
15
+ * consumers index by. */
16
+ export interface LaneKeys {
17
+ /** The dated lane: the normalized canonical (uppercased, de-spaced). */
18
+ dated: string;
19
+ /** The undated lane: every colon-year not followed by a dash is gone
20
+ * (base and supplement alike — one rule, no per-flavor casing). */
21
+ undated: string;
22
+ /** The all-parts lane: the parsed part stripped from the undated lane.
23
+ * Structural only — an unparsed trailing "-N" is never a part. */
24
+ all_parts: string;
25
+ }
26
+ /** One successful parse: the structured components, the canonical render
27
+ * and the lane keys, per the export contract of pubid-ts#226. */
28
+ export interface ParseResult {
29
+ /** The flavor the parse ran under. */
30
+ flavor: string;
31
+ /** The input as given. */
32
+ input: string;
33
+ /** The canonical human spelling (the normalized render). */
34
+ canonical: string;
35
+ /** The URN; undefined when the flavor emits none. */
36
+ urn?: string;
37
+ /** The structured components — the reference's own canonical toHash
38
+ * record (the wire shape), every facet typed per the corpus schema. */
39
+ components: Record<string, unknown>;
40
+ /** The year facet, string-normalized; undefined when undated. */
41
+ year?: string;
42
+ /** The structural part: the identifier's own part facet, else the
43
+ * base identifier's (amendments and corrigenda inherit it). */
44
+ part?: string;
45
+ /** The dated / undated / all_parts lane keys. */
46
+ keys: LaneKeys;
47
+ }
48
+ /**
49
+ * The dated lane: the canonical render uppercased and de-spaced. Ruby's
50
+ * `canonical.upcase.delete(" ")` — exactly the space character is
51
+ * deleted, not every whitespace run.
52
+ */
53
+ export declare function norm(canonical: string): string;
54
+ /**
55
+ * The undated lane from a norm key: every `:?`-year not followed by a
56
+ * dash is stripped — globally, base and supplement years alike. Ruby's
57
+ * `gsub(/:?(?:19|20)\d{2}(?=[^-]*$)/, "")` verbatim.
58
+ */
59
+ export declare function undatedNorm(normKey: string): string;
60
+ /**
61
+ * The all-parts lane from an undated key: the FIRST `-<part>` not
62
+ * followed by a dash is stripped. Ruby's
63
+ * `sub(/-#{Regexp.escape(part)}(?=[^-]*$)/, "")` — only a structurally
64
+ * parsed part is ever stripped; a blank part strips nothing.
65
+ */
66
+ export declare function allPartsNorm(undated: string, part?: string): string;
67
+ /**
68
+ * Parse one identifier: the structured components per parse, the
69
+ * canonical render, the URN and the lane keys in one result. Rejections
70
+ * raise {@link ParseError} — the surfaced failure class the verdict
71
+ * helper {@link parseOk} folds to `false`.
72
+ *
73
+ * The flavor must be named: grammar-ported flavors parse open-endedly;
74
+ * the rest resolve over the bundled corpus (the same coverage the
75
+ * conformance surface offers). URN spellings parse where the reference
76
+ * implements a URN parser.
77
+ */
78
+ export declare function parse(input: string, flavor: string): ParseResult;
79
+ /**
80
+ * The verdict helper: true when the input parses under the flavor, false
81
+ * when it is rejected (the citation-validation primitive — never an
82
+ * exception path).
83
+ */
84
+ export declare function parseOk(input: string, flavor: string): boolean;
85
+ /**
86
+ * The canonical render: the normalized human form of one identifier.
87
+ * Rejections raise {@link ParseError}.
88
+ */
89
+ export declare function render(input: string, flavor: string): string;
90
+ /**
91
+ * The lane keys of one identifier: `dated` / `undated` / `all_parts`,
92
+ * derived through the full parse — the corpus lane semantics (see
93
+ * {@link norm}, {@link undatedNorm}, {@link allPartsNorm}). Rejections
94
+ * raise {@link ParseError}.
95
+ */
96
+ export declare function laneKeys(input: string, flavor: string): LaneKeys;
@@ -0,0 +1,190 @@
1
+ import { grammarImplementation } from "./flavors/index.js";
2
+ import { corpusModeImplementation } from "./implementations/corpus-mode.js";
3
+ import { loadBundledCorpus } from "./corpus/artifact.js";
4
+ /** The failure class of the parse surface: every rejected input and
5
+ * unknown flavor raises this (wrapped engine errors ride as `cause`). */
6
+ export class ParseError extends Error {
7
+ /** The rejected input. */
8
+ input;
9
+ /** The flavor the parse ran under, when one was named. */
10
+ flavor;
11
+ constructor(message, opts) {
12
+ super(message, opts.cause !== undefined ? { cause: opts.cause } : undefined);
13
+ this.name = "ParseError";
14
+ this.input = opts.input;
15
+ this.flavor = opts.flavor;
16
+ }
17
+ }
18
+ /** Memoized corpus-mode implementations for the flavors whose grammar is
19
+ * not ported yet (currently adobe and sae): they parse over the bundled
20
+ * corpus, loaded lazily — the grammar flavors never touch it. */
21
+ let bundledCorpusImplementations;
22
+ function corpusImplementation(flavor) {
23
+ if (bundledCorpusImplementations === undefined) {
24
+ const corpus = loadBundledCorpus();
25
+ bundledCorpusImplementations = new Map([...corpus.flavors].map(([name, payloads]) => [
26
+ name,
27
+ corpusModeImplementation(payloads.cases),
28
+ ]));
29
+ }
30
+ return bundledCorpusImplementations.get(flavor);
31
+ }
32
+ function implementationFor(flavor) {
33
+ const grammar = grammarImplementation(flavor);
34
+ if (grammar !== undefined)
35
+ return grammar;
36
+ const corpus = corpusImplementation(flavor);
37
+ if (corpus !== undefined)
38
+ return corpus;
39
+ throw new ParseError(`unknown flavor: ${flavor}`, { input: "", flavor });
40
+ }
41
+ /** Ruby Regexp.escape, for the all-parts strip of an arbitrary part. */
42
+ function escapeRegExp(text) {
43
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
44
+ }
45
+ /**
46
+ * The dated lane: the canonical render uppercased and de-spaced. Ruby's
47
+ * `canonical.upcase.delete(" ")` — exactly the space character is
48
+ * deleted, not every whitespace run.
49
+ */
50
+ export function norm(canonical) {
51
+ return canonical.toUpperCase().replaceAll(" ", "");
52
+ }
53
+ /**
54
+ * The undated lane from a norm key: every `:?`-year not followed by a
55
+ * dash is stripped — globally, base and supplement years alike. Ruby's
56
+ * `gsub(/:?(?:19|20)\d{2}(?=[^-]*$)/, "")` verbatim.
57
+ */
58
+ export function undatedNorm(normKey) {
59
+ return normKey.replace(/:?(?:19|20)\d{2}(?=[^-]*$)/g, "");
60
+ }
61
+ /**
62
+ * The all-parts lane from an undated key: the FIRST `-<part>` not
63
+ * followed by a dash is stripped. Ruby's
64
+ * `sub(/-#{Regexp.escape(part)}(?=[^-]*$)/, "")` — only a structurally
65
+ * parsed part is ever stripped; a blank part strips nothing.
66
+ */
67
+ export function allPartsNorm(undated, part) {
68
+ if (part === undefined || part === "")
69
+ return undated;
70
+ return undated.replace(new RegExp(`-${escapeRegExp(part)}(?=[^-]*$)`), "");
71
+ }
72
+ /**
73
+ * The structural part of a canonical component record: the identifier's
74
+ * own `part` facet, else the base's (Ruby's `id.part || id.base&.part`).
75
+ * A part facet is either a string or a typed record carrying `value`
76
+ * (the NIST lettered parts); anything else is no part.
77
+ */
78
+ function structuralPart(components) {
79
+ const base = components["base"];
80
+ const raw = components["part"] !== undefined && components["part"] !== null
81
+ ? components["part"]
82
+ : base !== undefined && base !== null && typeof base === "object"
83
+ ? base["part"]
84
+ : undefined;
85
+ if (typeof raw === "string")
86
+ return raw;
87
+ if (raw !== undefined && raw !== null && typeof raw === "object" && !Array.isArray(raw)) {
88
+ const value = raw["value"];
89
+ if (typeof value === "string")
90
+ return value;
91
+ if (typeof value === "number")
92
+ return String(value);
93
+ }
94
+ return undefined;
95
+ }
96
+ /** The lane keys for an already-canonical record: norm from the render,
97
+ * undated from the norm, all_parts from the parsed part — the corpus
98
+ * lane derivations, nothing guessed. */
99
+ function laneKeysFrom(canonical, components) {
100
+ const dated = norm(canonical);
101
+ const undated = undatedNorm(dated);
102
+ return {
103
+ dated,
104
+ undated,
105
+ all_parts: allPartsNorm(undated, structuralPart(components)),
106
+ };
107
+ }
108
+ /** The year facet of a component record, string-normalized. */
109
+ function structuralYear(components) {
110
+ const year = components["year"];
111
+ if (typeof year === "string")
112
+ return year;
113
+ if (typeof year === "number")
114
+ return String(year);
115
+ return undefined;
116
+ }
117
+ /**
118
+ * Parse one identifier: the structured components per parse, the
119
+ * canonical render, the URN and the lane keys in one result. Rejections
120
+ * raise {@link ParseError} — the surfaced failure class the verdict
121
+ * helper {@link parseOk} folds to `false`.
122
+ *
123
+ * The flavor must be named: grammar-ported flavors parse open-endedly;
124
+ * the rest resolve over the bundled corpus (the same coverage the
125
+ * conformance surface offers). URN spellings parse where the reference
126
+ * implements a URN parser.
127
+ */
128
+ export function parse(input, flavor) {
129
+ let identifier;
130
+ try {
131
+ identifier = implementationFor(flavor).parse(input);
132
+ }
133
+ catch (cause) {
134
+ // Unknown-flavor ParseErrors carry no input (implementationFor sees
135
+ // only the flavor) - rebuild with the input, preserving the cause.
136
+ if (cause instanceof ParseError) {
137
+ throw new ParseError(cause.message, { input, flavor, cause });
138
+ }
139
+ throw new ParseError(`cannot parse ${JSON.stringify(input)} as ${flavor}`, {
140
+ input,
141
+ flavor,
142
+ cause,
143
+ });
144
+ }
145
+ const canonical = identifier.toHuman();
146
+ const urn = identifier.toUrn();
147
+ const components = identifier.toHash();
148
+ const year = structuralYear(components);
149
+ const part = structuralPart(components);
150
+ return {
151
+ flavor,
152
+ input,
153
+ canonical,
154
+ ...(urn !== undefined ? { urn } : {}),
155
+ components,
156
+ ...(year !== undefined ? { year } : {}),
157
+ ...(part !== undefined ? { part } : {}),
158
+ keys: laneKeysFrom(canonical, components),
159
+ };
160
+ }
161
+ /**
162
+ * The verdict helper: true when the input parses under the flavor, false
163
+ * when it is rejected (the citation-validation primitive — never an
164
+ * exception path).
165
+ */
166
+ export function parseOk(input, flavor) {
167
+ try {
168
+ parse(input, flavor);
169
+ return true;
170
+ }
171
+ catch {
172
+ return false;
173
+ }
174
+ }
175
+ /**
176
+ * The canonical render: the normalized human form of one identifier.
177
+ * Rejections raise {@link ParseError}.
178
+ */
179
+ export function render(input, flavor) {
180
+ return parse(input, flavor).canonical;
181
+ }
182
+ /**
183
+ * The lane keys of one identifier: `dated` / `undated` / `all_parts`,
184
+ * derived through the full parse — the corpus lane semantics (see
185
+ * {@link norm}, {@link undatedNorm}, {@link allPartsNorm}). Rejections
186
+ * raise {@link ParseError}.
187
+ */
188
+ export function laneKeys(input, flavor) {
189
+ return parse(input, flavor).keys;
190
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pubid/pubid",
3
- "version": "0.1.65",
3
+ "version": "0.1.66",
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",