@pubid/pubid 0.1.57 → 0.1.58

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,3 @@
1
+ import type { FlavorImplementation } from "../../conformance/implementation.js";
2
+ export * from "./model.js";
3
+ export declare function arxivGrammarImplementation(): FlavorImplementation;
@@ -0,0 +1,105 @@
1
+ import { ParseFailed } from "../../grammar/engine.js";
2
+ import { PubidRuntime } from "../../parg/runtime.js";
3
+ import { ArxivEprint } from "./model.js";
4
+ export * from "./model.js";
5
+ /**
6
+ * The arxiv parse entry — the same baked artifact the Ruby flavor runs
7
+ * (pubid-grammar grammars/arxiv.parg -> artifacts/arxiv.json, sealed at
8
+ * parsanol 1.5.1.0), loaded through the artifact runtime. The builder
9
+ * mirrors Pubid::Arxiv::Builder: the grammar checks the shape, this
10
+ * side checks the rules the shape cannot express, raising (which the
11
+ * parse entry surfaces as a rejection):
12
+ *
13
+ * - the month is 01-12;
14
+ * - a 4-digit new number has a YYMM from 0704 to 1412;
15
+ * - a 5-digit new number has a YYMM from 1501;
16
+ * - an old-form id has a YYMM from 9108 to 0703;
17
+ * - the version is 1 or more, with no leading zero (so to_s is exact).
18
+ */
19
+ // --- Builder (lib/pubid/arxiv/builder.rb) ------------------------------------
20
+ const s = (v) => {
21
+ if (v === undefined || v === null)
22
+ return undefined;
23
+ const text = String(v);
24
+ return text.length > 0 ? text : undefined;
25
+ };
26
+ /** Old-form ids run from 9108 to 0703, so 91-99 is the 1990s; a new id
27
+ * is always in the 2000s. */
28
+ function yearMonth(yymmRaw, old) {
29
+ const yymm = String(yymmRaw ?? "");
30
+ const twoDigit = Number(yymm.slice(0, 2));
31
+ const month = Number(yymm.slice(2, 4));
32
+ if (!(month >= 1 && month <= 12)) {
33
+ throw new Error(`month out of range in YYMM ${yymm}`);
34
+ }
35
+ const century = old && twoDigit >= 91 ? 1900 : 2000;
36
+ return [century + twoDigit, month];
37
+ }
38
+ const ym = (year, month) => year * 100 + month;
39
+ function validateRange(year, month, number, old) {
40
+ if (old) {
41
+ if (!(ym(year, month) >= 199108 && ym(year, month) <= 200703)) {
42
+ throw new Error("an old-form id dates from 9108 to 0703");
43
+ }
44
+ return;
45
+ }
46
+ const valid = number.length === 4
47
+ ? ym(year, month) >= 200704 && ym(year, month) <= 201412
48
+ : ym(year, month) >= 201501;
49
+ if (!valid) {
50
+ const yy = String(year % 100).padStart(2, "0");
51
+ const mm = String(month).padStart(2, "0");
52
+ throw new Error(`a ${number.length}-digit number does not fit YYMM ${yy}${mm}`);
53
+ }
54
+ }
55
+ /** The version is 1 or more, with no leading zero (v01 would render v1,
56
+ * so to_s would not be exact). */
57
+ function parseVersion(value) {
58
+ const raw = s(value);
59
+ if (raw === undefined)
60
+ return undefined;
61
+ if (raw.startsWith("0")) {
62
+ throw new Error(`version v${raw} is not 1 or more without a leading zero`);
63
+ }
64
+ return Number(raw);
65
+ }
66
+ function buildArxiv(data) {
67
+ const archive = s(data["archive"]);
68
+ const [year, month] = yearMonth(String(data["yymm"] ?? ""), archive !== undefined);
69
+ const number = String(data["number"] ?? "");
70
+ validateRange(year, month, number, archive !== undefined);
71
+ return new ArxivEprint({
72
+ ...(archive !== undefined ? { archive } : {}),
73
+ ...(s(data["subject_class"]) !== undefined
74
+ ? { subject_class: s(data["subject_class"]) }
75
+ : {}),
76
+ year,
77
+ month,
78
+ number,
79
+ ...(parseVersion(data["version"]) !== undefined
80
+ ? { version: parseVersion(data["version"]) }
81
+ : {}),
82
+ });
83
+ }
84
+ // --- Implementation ----------------------------------------------------------
85
+ let cachedRuntime;
86
+ export function arxivGrammarImplementation() {
87
+ return {
88
+ parse(input) {
89
+ cachedRuntime ??= PubidRuntime.load("arxiv");
90
+ const bound = cachedRuntime.parseAndBind(input);
91
+ if (bound === null || typeof bound !== "object") {
92
+ throw new ParseFailed("arxiv: the artifact rejected the input", 0);
93
+ }
94
+ try {
95
+ return buildArxiv(bound);
96
+ }
97
+ catch (e) {
98
+ // The builder checks the date ranges, the month and the version;
99
+ // surface that as a parse failure, the class a grammar rejection
100
+ // has (the gem turns its ArgumentError into ParseError here).
101
+ throw new Error(`invalid arXiv identifier '${input}': ${e.message}`);
102
+ }
103
+ },
104
+ };
105
+ }
@@ -0,0 +1,51 @@
1
+ import { BaseIdentifier } from "../../model/identifier.js";
2
+ /**
3
+ * The arxiv identifier model — lib/pubid/arxiv/identifiers/eprint.rb.
4
+ * Registration lives HERE (parg-free) so the worker manifest
5
+ * (src/flavors/register-types.ts) can import it without reaching the
6
+ * artifact runtime; the parse entry (./index.js) is the parg-backed half.
7
+ *
8
+ * An arXiv e-print, new form (`2301.00001`) or old form
9
+ * (`hep-th/9901001`, `math.GT/0309136`). `archive`/`subject_class`
10
+ * (old ids only) are STRICT in the subset match — a nil archive means
11
+ * "this id has none", so a new id never matches an old one and
12
+ * `math/...` never matches `math.GT/...`. The `version` is the only
13
+ * === wildcard. `year`/`month` name the paper (its YYMM), not an
14
+ * edition.
15
+ */
16
+ export declare class ArxivIdentifier extends BaseIdentifier {
17
+ static polymorphicName: string;
18
+ static attributes: import("../../model/attribute.js").AttributeTable;
19
+ static mappings: import("../../model/attribute.js").FieldMapping[];
20
+ static subsetStrict: string[];
21
+ readonly archive: string | undefined;
22
+ readonly subject_class: string | undefined;
23
+ readonly year: number | undefined;
24
+ readonly month: number | undefined;
25
+ readonly version: number | undefined;
26
+ readonly number: string | undefined;
27
+ /** The four-digit YYMM the id prints. */
28
+ yymm(): string;
29
+ /** True for an old-form (archive) id. */
30
+ oldStyle(): boolean;
31
+ /** The canonical colon form `arXiv:<id>` — whatever prefix the input
32
+ * used; the `arXiv:` token is constant and every surface prints it. */
33
+ render(): string;
34
+ /** to_s(with_publisher: false): the bare id — the form the arXiv API
35
+ * takes in id_list (handled here, not by a publisher-stripper: the
36
+ * stripper cuts only before a space, "-", "/" or ".", never before
37
+ * the colon, and publisher_prefixes is [] for that reason). */
38
+ toHuman(opts?: {
39
+ withPublisher?: boolean;
40
+ }): string;
41
+ /** arXiv registers no official URN namespace, and the base fallback
42
+ * (urn:arxiv:<number>:<year>) maps different papers to one URN, so
43
+ * the official form is the lossless pubid-scheme URN. */
44
+ toUrn(): string;
45
+ protected body(): string;
46
+ protected versionSuffix(): string;
47
+ }
48
+ export declare class ArxivEprint extends ArxivIdentifier {
49
+ static polymorphicName: string;
50
+ }
51
+ export type ArxivLeafClass = typeof ArxivEprint;
@@ -0,0 +1,79 @@
1
+ import { BaseIdentifier, registerType } from "../../model/identifier.js";
2
+ import { extendAttributes, keyValue } from "../../model/attribute.js";
3
+ /**
4
+ * The arxiv identifier model — lib/pubid/arxiv/identifiers/eprint.rb.
5
+ * Registration lives HERE (parg-free) so the worker manifest
6
+ * (src/flavors/register-types.ts) can import it without reaching the
7
+ * artifact runtime; the parse entry (./index.js) is the parg-backed half.
8
+ *
9
+ * An arXiv e-print, new form (`2301.00001`) or old form
10
+ * (`hep-th/9901001`, `math.GT/0309136`). `archive`/`subject_class`
11
+ * (old ids only) are STRICT in the subset match — a nil archive means
12
+ * "this id has none", so a new id never matches an old one and
13
+ * `math/...` never matches `math.GT/...`. The `version` is the only
14
+ * === wildcard. `year`/`month` name the paper (its YYMM), not an
15
+ * edition.
16
+ */
17
+ export class ArxivIdentifier extends BaseIdentifier {
18
+ static polymorphicName = "pubid:arxiv:identifier";
19
+ static attributes = extendAttributes(BaseIdentifier, {
20
+ archive: { type: "string" },
21
+ subject_class: { type: "string" },
22
+ year: { type: "integer" },
23
+ month: { type: "integer" },
24
+ version: { type: "integer" },
25
+ });
26
+ // The gem's key_value whitelist.
27
+ static mappings = keyValue({ wire: "archive", to: "archive" }, { wire: "subject_class", to: "subject_class" }, { wire: "year", to: "year" }, { wire: "month", to: "month" }, { wire: "number", to: "number" }, { wire: "version", to: "version" });
28
+ // subset_strict :archive, :subject_class, :year, :month, :number — the
29
+ // version stays the only wildcard.
30
+ static subsetStrict = ["archive", "subject_class", "year", "month", "number"];
31
+ /** The four-digit YYMM the id prints. */
32
+ yymm() {
33
+ const yy = String((this.year ?? 0) % 100).padStart(2, "0");
34
+ const mm = String(this.month ?? 0).padStart(2, "0");
35
+ return `${yy}${mm}`;
36
+ }
37
+ /** True for an old-form (archive) id. */
38
+ oldStyle() {
39
+ return this.archive !== undefined && this.archive !== null;
40
+ }
41
+ /** The canonical colon form `arXiv:<id>` — whatever prefix the input
42
+ * used; the `arXiv:` token is constant and every surface prints it. */
43
+ render() {
44
+ return `arXiv:${this.body()}${this.versionSuffix()}`;
45
+ }
46
+ /** to_s(with_publisher: false): the bare id — the form the arXiv API
47
+ * takes in id_list (handled here, not by a publisher-stripper: the
48
+ * stripper cuts only before a space, "-", "/" or ".", never before
49
+ * the colon, and publisher_prefixes is [] for that reason). */
50
+ toHuman(opts) {
51
+ const human = super.toHuman();
52
+ if (opts?.withPublisher !== false)
53
+ return human;
54
+ return human.replace(/^arXiv:/, "");
55
+ }
56
+ /** arXiv registers no official URN namespace, and the base fallback
57
+ * (urn:arxiv:<number>:<year>) maps different papers to one URN, so
58
+ * the official form is the lossless pubid-scheme URN. */
59
+ toUrn() {
60
+ return this.toPubidUrn();
61
+ }
62
+ body() {
63
+ if (!this.oldStyle())
64
+ return `${this.yymm()}.${this.number ?? ""}`;
65
+ const archive = [this.archive, this.subject_class].filter((v) => v !== undefined).join(".");
66
+ return `${archive}/${this.yymm()}${this.number ?? ""}`;
67
+ }
68
+ versionSuffix() {
69
+ return this.version !== undefined && this.version !== null ? `v${this.version}` : "";
70
+ }
71
+ }
72
+ export class ArxivEprint extends ArxivIdentifier {
73
+ static polymorphicName = "pubid:arxiv:eprint";
74
+ }
75
+ // The ARXIV_TYPE_MAP role: the concrete e-print plus the base (a
76
+ // hand-built hash serializes as pubid:arxiv:identifier — the gem's
77
+ // Identifier class itself).
78
+ registerType(ArxivIdentifier);
79
+ registerType(ArxivEprint);
@@ -0,0 +1,3 @@
1
+ import type { FlavorImplementation } from "../../conformance/implementation.js";
2
+ export * from "./model.js";
3
+ export declare function euGrammarImplementation(): FlavorImplementation;
@@ -0,0 +1,113 @@
1
+ import { ParseFailed } from "../../grammar/engine.js";
2
+ import { PubidRuntime } from "../../parg/runtime.js";
3
+ import { EuDecision, EuDirective, EuRegulation, isPlausibleYear, numberFirstFor, stripLeadingZeros, } from "./model.js";
4
+ export * from "./model.js";
5
+ /**
6
+ * The eu parse entry — the same baked artifact the Ruby flavor runs
7
+ * (pubid-grammar grammars/eu.parg -> artifacts/eu.json, sealed at
8
+ * parsanol 1.5.1.0), loaded through the artifact runtime so grammar
9
+ * changes land once and this side cannot drift. The builder mirrors
10
+ * Pubid::Eu::Builder: CELEX and ELI fix year/number by position, the
11
+ * canonical and Official-Journal forms carry two numbers whose ORDER is
12
+ * not in the grammar (number_first_for), and the renderer's "No"
13
+ * disambiguation lives on the model (EuIdentifier#numbers).
14
+ */
15
+ // --- Builder (lib/pubid/eu/builder.rb) ---------------------------------------
16
+ const TYPES_BY_WORD = {
17
+ Regulation: EuRegulation,
18
+ Directive: EuDirective,
19
+ Decision: EuDecision,
20
+ };
21
+ const TYPES_BY_CELEX = {
22
+ R: EuRegulation,
23
+ L: EuDirective,
24
+ D: EuDecision,
25
+ };
26
+ const TYPES_BY_ELI = {
27
+ reg: EuRegulation,
28
+ dir: EuDirective,
29
+ dec: EuDecision,
30
+ };
31
+ const s = (v) => {
32
+ if (v === undefined || v === null)
33
+ return undefined;
34
+ const text = String(v);
35
+ return text.length > 0 ? text : undefined;
36
+ };
37
+ function positional(data) {
38
+ const letter = s(data["celex_letter"]);
39
+ const eli = s(data["eli_type"]);
40
+ const klass = letter !== undefined
41
+ ? TYPES_BY_CELEX[letter]
42
+ : eli !== undefined
43
+ ? TYPES_BY_ELI[eli]
44
+ : undefined;
45
+ if (klass === undefined)
46
+ return undefined;
47
+ // CELEX and ELI: the year and the number have fixed places.
48
+ return {
49
+ klass,
50
+ year: String(data["year"] ?? ""),
51
+ number: String(data["number"] ?? ""),
52
+ numberFirst: undefined,
53
+ };
54
+ }
55
+ function fromPair(data) {
56
+ const klass = TYPES_BY_WORD[String(data["type_word"])];
57
+ if (klass === undefined) {
58
+ throw new Error(`EU legal act: unknown act type ${String(data["type_word"])}`);
59
+ }
60
+ const first = String(data["first"] ?? "");
61
+ const second = String(data["second"] ?? "");
62
+ // "No" marks <number>/<year> (Regulation (EU) No 1227/2011, Decision
63
+ // No 1082/2013/EU); every other pair reads by the rule.
64
+ const numberFirst = (data["no"] !== undefined && data["no"] !== null) ||
65
+ numberFirstFor(klass.euType.title, first, second);
66
+ if (numberFirst === undefined) {
67
+ throw new Error(`EU legal act: no year in ${first}/${second}`);
68
+ }
69
+ const [year, number] = numberFirst ? [second, first] : [first, second];
70
+ return { klass, year, number, numberFirst };
71
+ }
72
+ function family(data) {
73
+ const bracket = s(data["family"]);
74
+ const suffix = s(data["family_suffix"]);
75
+ if (bracket !== undefined && suffix !== undefined && bracket !== suffix) {
76
+ throw new Error(`EU legal act: two families, ${bracket} and ${suffix}`);
77
+ }
78
+ return bracket ?? suffix;
79
+ }
80
+ function buildEu(data) {
81
+ const act = positional(data) ?? fromPair(data);
82
+ if (!isPlausibleYear(act.year)) {
83
+ throw new Error(`EU legal act: ${act.year} is not a year`);
84
+ }
85
+ const number = stripLeadingZeros(act.number);
86
+ if (number === "0") {
87
+ throw new Error("EU legal act: number 0");
88
+ }
89
+ const id = new act.klass({
90
+ number,
91
+ date: { year: act.year },
92
+ ...(family(data) !== undefined ? { family: family(data) } : {}),
93
+ });
94
+ // Keep the order only when it is not what the render rule prints.
95
+ if (act.numberFirst !== undefined && act.numberFirst !== id.defaultNumberFirst()) {
96
+ id["number_first"] = act.numberFirst;
97
+ }
98
+ return id;
99
+ }
100
+ // --- Implementation ----------------------------------------------------------
101
+ let cachedRuntime;
102
+ export function euGrammarImplementation() {
103
+ return {
104
+ parse(input) {
105
+ cachedRuntime ??= PubidRuntime.load("eu");
106
+ const bound = cachedRuntime.parseAndBind(input);
107
+ if (bound === null || typeof bound !== "object") {
108
+ throw new ParseFailed("eu: the artifact rejected the input", 0);
109
+ }
110
+ return buildEu(bound);
111
+ },
112
+ };
113
+ }
@@ -0,0 +1,108 @@
1
+ import { BaseIdentifier } from "../../model/identifier.js";
2
+ import type { PubidDate } from "../../model/component.js";
3
+ /**
4
+ * The eu identifier model — the classes of lib/pubid/eu/identifiers.rb.
5
+ * Registration lives HERE (parg-free) so the worker manifest
6
+ * (src/flavors/register-types.ts) can import it without reaching the
7
+ * artifact runtime; the parse entry (./index.js) is the parg-backed half.
8
+ *
9
+ * Identity is the leaf class (the act type), the year (`date`) and the
10
+ * `number` — the three parts of a CELEX number. `family` is the
11
+ * bracketed legal family of the Official Journal form ("(EU)", "(EC)"
12
+ * ...); the canonical reference omits it, so a reference with no family
13
+ * matches a row that has one (the subset-match wildcard).
14
+ * `number_first` is the print ORDER of the two numbers — spelling, not
15
+ * identity: it serializes but `==` skips it (equality_ignored), so
16
+ * "EU Decision 1082/2013" equals the CELEX number 32013D1082.
17
+ */
18
+ /** The act-type metadata each leaf class carries (Ruby self.type). */
19
+ export interface EuType {
20
+ key: string;
21
+ title: string;
22
+ celex: string | null;
23
+ eli: string | null;
24
+ }
25
+ /** From 2015 every act prints <year>/<number>; before it, a regulation
26
+ * printed <number>/<year> (the first Official Journal acts, ECSC,
27
+ * date from 1952). */
28
+ export declare const FIXED_ORDER_YEAR = 2015;
29
+ /** The canonical reference's publisher — the PUBLISHER constant, not an
30
+ * attribute: every surface prints it, so nothing stores it. */
31
+ export declare const EU_PUBLISHER = "EU";
32
+ export declare class EuIdentifier extends BaseIdentifier {
33
+ static polymorphicName: string;
34
+ static attributes: import("../../model/attribute.js").AttributeTable;
35
+ static mappings: import("../../model/attribute.js").FieldMapping[];
36
+ static euType: EuType;
37
+ readonly number: string | undefined;
38
+ readonly date: PubidDate | undefined;
39
+ readonly family: string | undefined;
40
+ readonly number_first: boolean | undefined;
41
+ /** The year, read off the date component (Ruby #year). */
42
+ get year(): string | undefined;
43
+ /** A regulation with a year before 2015 prints <number>/<year>;
44
+ * everything else prints <year>/<number>. (Ruby year.to_i: a missing
45
+ * year reads as 0, so a year-less regulation defaults number-first.) */
46
+ defaultNumberFirst(): boolean;
47
+ /** The effective print order: the stored flag when the builder set it
48
+ * (the input used the non-default order), else the default rule. */
49
+ numberFirst(): boolean;
50
+ /** The act-type key of the leaf class ("regulation" / "directive" /
51
+ * "decision" / the base's "identifier"). */
52
+ euTypeKey(): string;
53
+ /** The act-type title of the leaf class ("" on the base). */
54
+ euTitle(): string;
55
+ /** The CELEX type letter of the leaf class (R / L / D). */
56
+ euCelexLetter(): string | null;
57
+ /** The ELI segment of the leaf class (reg / dir / dec). */
58
+ euEliSegment(): string | null;
59
+ render(): string;
60
+ /** to_s(with_publisher: false): the Official-Journal-citable form
61
+ * without the leading "EU " (the PublisherStripper cut). */
62
+ toHuman(opts?: {
63
+ withPublisher?: boolean;
64
+ }): string;
65
+ /** The CELEX number: sector 3, year, type letter, number padded to 4.
66
+ * (The base class carries no type letter — the segments join empty.) */
67
+ toCelex(): string;
68
+ /** The ELI of the act as published in the Official Journal. */
69
+ toEli(): string;
70
+ /** The EU has no registered URN namespace (the ELI is its official
71
+ * identifier). This pubid-invented form keeps the act type, so a
72
+ * regulation and a decision with the same numbers do not collide. */
73
+ toUrn(): string;
74
+ /** The two numbers in print order, or the bare number without a year.
75
+ * When the plain pair would read back as ANOTHER act (two acts share
76
+ * one plain print — 32015R2014 and 32014R2015 both give "2015/2014"),
77
+ * "No" marks <number>/<year>, which always reads back as this act. */
78
+ protected numbers(): string;
79
+ }
80
+ export declare class EuRegulation extends EuIdentifier {
81
+ static polymorphicName: string;
82
+ static euType: EuType;
83
+ }
84
+ export declare class EuDirective extends EuIdentifier {
85
+ static polymorphicName: string;
86
+ static euType: EuType;
87
+ }
88
+ export declare class EuDecision extends EuIdentifier {
89
+ static polymorphicName: string;
90
+ static euType: EuType;
91
+ }
92
+ export type EuLeafClass = typeof EuRegulation | typeof EuDirective | typeof EuDecision;
93
+ /**
94
+ * Which of two printed numbers is the year, when the input has no "No".
95
+ * Only one plausible year: that one. Both: a first value from 2015 is
96
+ * the year (every act prints <year>/<number> from 2015); otherwise a
97
+ * regulation prints <number>/<year> and other acts <year>/<number>.
98
+ * Neither plausible: undefined. The renderer calls this too, to know
99
+ * when a plain print would read back as another act. `klass` is the
100
+ * Ruby class-name fragment the gem's Builder.number_first_for compares
101
+ * ("Regulation" — the leaf title — or "Identifier" on the base).
102
+ */
103
+ export declare function numberFirstFor(klass: string, first: string, second: string): boolean | undefined;
104
+ /** The first Official Journal acts (ECSC) date from 1952. */
105
+ export declare function isPlausibleYear(value: string): boolean;
106
+ /** "0079" -> "79", "0" stays "0" (Ruby strip_zeros: leading zeros only,
107
+ * never the bare zero). */
108
+ export declare function stripLeadingZeros(number: string): string;
@@ -0,0 +1,155 @@
1
+ import { BaseIdentifier, registerType } from "../../model/identifier.js";
2
+ import { extendAttributes, keyValue } from "../../model/attribute.js";
3
+ /** From 2015 every act prints <year>/<number>; before it, a regulation
4
+ * printed <number>/<year> (the first Official Journal acts, ECSC,
5
+ * date from 1952). */
6
+ export const FIXED_ORDER_YEAR = 2015;
7
+ /** The canonical reference's publisher — the PUBLISHER constant, not an
8
+ * attribute: every surface prints it, so nothing stores it. */
9
+ export const EU_PUBLISHER = "EU";
10
+ export class EuIdentifier extends BaseIdentifier {
11
+ static polymorphicName = "pubid:eu:identifier";
12
+ static attributes = extendAttributes(BaseIdentifier, {
13
+ family: { type: "string" },
14
+ number_first: { type: "boolean", equalityIgnored: true },
15
+ });
16
+ // The gem's key_value whitelist: only the four slots hit the wire; the
17
+ // base's part/edition/publisher/... slots stay off it.
18
+ static mappings = keyValue({ wire: "number", to: "number" }, { wire: "date", to: "date" }, { wire: "family", to: "family" }, { wire: "number_first", to: "number_first" });
19
+ static euType = { key: "identifier", title: "", celex: null, eli: null };
20
+ /** The year, read off the date component (Ruby #year). */
21
+ get year() {
22
+ return this.date?.year;
23
+ }
24
+ /** A regulation with a year before 2015 prints <number>/<year>;
25
+ * everything else prints <year>/<number>. (Ruby year.to_i: a missing
26
+ * year reads as 0, so a year-less regulation defaults number-first.) */
27
+ defaultNumberFirst() {
28
+ return this.euTypeKey() === "regulation" &&
29
+ (Number(this.year) || 0) < FIXED_ORDER_YEAR;
30
+ }
31
+ /** The effective print order: the stored flag when the builder set it
32
+ * (the input used the non-default order), else the default rule. */
33
+ numberFirst() {
34
+ return this.number_first ?? this.defaultNumberFirst();
35
+ }
36
+ /** The act-type key of the leaf class ("regulation" / "directive" /
37
+ * "decision" / the base's "identifier"). */
38
+ euTypeKey() {
39
+ return this.constructor.euType.key;
40
+ }
41
+ /** The act-type title of the leaf class ("" on the base). */
42
+ euTitle() {
43
+ return this.constructor.euType.title;
44
+ }
45
+ /** The CELEX type letter of the leaf class (R / L / D). */
46
+ euCelexLetter() {
47
+ return this.constructor.euType.celex;
48
+ }
49
+ /** The ELI segment of the leaf class (reg / dir / dec). */
50
+ euEliSegment() {
51
+ return this.constructor.euType.eli;
52
+ }
53
+ render() {
54
+ return `${EU_PUBLISHER} ${this.euTitle()} ${this.numbers()}`;
55
+ }
56
+ /** to_s(with_publisher: false): the Official-Journal-citable form
57
+ * without the leading "EU " (the PublisherStripper cut). */
58
+ toHuman(opts) {
59
+ const human = super.toHuman();
60
+ if (opts?.withPublisher !== false)
61
+ return human;
62
+ return human.replace(/^EU(?<sep>\s+)/, "");
63
+ }
64
+ /** The CELEX number: sector 3, year, type letter, number padded to 4.
65
+ * (The base class carries no type letter — the segments join empty.) */
66
+ toCelex() {
67
+ return `3${this.year ?? ""}${this.euCelexLetter() ?? ""}${(this.number ?? "").padStart(4, "0")}`;
68
+ }
69
+ /** The ELI of the act as published in the Official Journal. */
70
+ toEli() {
71
+ return `http://data.europa.eu/eli/${this.euEliSegment() ?? ""}/${this.year ?? ""}/${this.number ?? ""}/oj`;
72
+ }
73
+ /** The EU has no registered URN namespace (the ELI is its official
74
+ * identifier). This pubid-invented form keeps the act type, so a
75
+ * regulation and a decision with the same numbers do not collide. */
76
+ toUrn() {
77
+ return `urn:eu:${this.euEliSegment() ?? ""}:${this.year ?? ""}:${this.number ?? ""}`;
78
+ }
79
+ /** The two numbers in print order, or the bare number without a year.
80
+ * When the plain pair would read back as ANOTHER act (two acts share
81
+ * one plain print — 32015R2014 and 32014R2015 both give "2015/2014"),
82
+ * "No" marks <number>/<year>, which always reads back as this act. */
83
+ numbers() {
84
+ const number = this.number;
85
+ const year = this.year;
86
+ if (number === undefined)
87
+ return "";
88
+ if (year === undefined || year === null)
89
+ return String(number);
90
+ const numberFirst = this.numberFirst();
91
+ const pair = numberFirst ? [number, year] : [year, number];
92
+ const klass = this.euTypeKey() === "identifier"
93
+ ? "Identifier"
94
+ : this.euTitle();
95
+ if (numberFirstFor(klass, String(pair[0]), String(pair[1])) === numberFirst) {
96
+ return pair.join("/");
97
+ }
98
+ return `No ${number}/${year}`;
99
+ }
100
+ }
101
+ export class EuRegulation extends EuIdentifier {
102
+ static polymorphicName = "pubid:eu:regulation";
103
+ static euType = {
104
+ key: "regulation", title: "Regulation", celex: "R", eli: "reg",
105
+ };
106
+ }
107
+ export class EuDirective extends EuIdentifier {
108
+ static polymorphicName = "pubid:eu:directive";
109
+ static euType = {
110
+ key: "directive", title: "Directive", celex: "L", eli: "dir",
111
+ };
112
+ }
113
+ export class EuDecision extends EuIdentifier {
114
+ static polymorphicName = "pubid:eu:decision";
115
+ static euType = {
116
+ key: "decision", title: "Decision", celex: "D", eli: "dec",
117
+ };
118
+ }
119
+ /**
120
+ * Which of two printed numbers is the year, when the input has no "No".
121
+ * Only one plausible year: that one. Both: a first value from 2015 is
122
+ * the year (every act prints <year>/<number> from 2015); otherwise a
123
+ * regulation prints <number>/<year> and other acts <year>/<number>.
124
+ * Neither plausible: undefined. The renderer calls this too, to know
125
+ * when a plain print would read back as another act. `klass` is the
126
+ * Ruby class-name fragment the gem's Builder.number_first_for compares
127
+ * ("Regulation" — the leaf title — or "Identifier" on the base).
128
+ */
129
+ export function numberFirstFor(klass, first, second) {
130
+ const firstYear = isPlausibleYear(first);
131
+ const secondYear = isPlausibleYear(second);
132
+ if (!firstYear && !secondYear)
133
+ return undefined;
134
+ if (firstYear !== secondYear)
135
+ return !firstYear;
136
+ if (Number(first) >= FIXED_ORDER_YEAR)
137
+ return false;
138
+ return klass === "Regulation";
139
+ }
140
+ /** The first Official Journal acts (ECSC) date from 1952. */
141
+ export function isPlausibleYear(value) {
142
+ return value.length === 4 && Number(value) >= 1952 && Number(value) <= 2099;
143
+ }
144
+ /** "0079" -> "79", "0" stays "0" (Ruby strip_zeros: leading zeros only,
145
+ * never the bare zero). */
146
+ export function stripLeadingZeros(number) {
147
+ return number.replace(/^0+(?=\d)/, "");
148
+ }
149
+ // The *_TYPE_MAP role: polymorphic dispatch for the three leaves plus
150
+ // the base (a hand-built hash without a leaf class serializes as
151
+ // pubid:eu:identifier — the gem's Identifier class itself).
152
+ registerType(EuIdentifier);
153
+ registerType(EuRegulation);
154
+ registerType(EuDirective);
155
+ registerType(EuDecision);
@@ -21,10 +21,10 @@ const NUMBER_YEAR_SPLIT = /^(.+?)\s*[-—–]\s*(\d{2}|\d{4})$/;
21
21
  // flavor's identifier — that is what routes the joint "ISO/IEC …"
22
22
  // adoptions to the ISO flavor ahead of IEC.
23
23
  const FOREIGN_FALLBACK_ORDER = [
24
- "tgpp", "ansi", "astm", "bipm",
24
+ "tgpp", "ansi", "arxiv", "astm", "bipm",
25
25
  "bsi",
26
26
  "calconnect", "ccsds", "cen_cenelec", "cie", "doi", "easc", "ecma",
27
- "etsi", "gb", "iala", "iana", "idf", "iec", "ieee", "ietf", "iho",
27
+ "etsi", "eu", "gb", "iala", "iana", "idf", "iec", "ieee", "ietf", "iho",
28
28
  "isbn", "iso", "itu", "jcgm", "jis", "nist", "oasis", "ogc", "oiml",
29
29
  "omg", "plateau", "un", "w3c", "xsf",
30
30
  ];
@@ -45,6 +45,8 @@ import { apiGrammarImplementation } from "./api/index.js";
45
45
  import { ituGrammarImplementation } from "./itu/index.js";
46
46
  import { ietfGrammarImplementation } from "./ietf/index.js";
47
47
  import { ialaGrammarImplementation } from "./iala/index.js";
48
+ import { euGrammarImplementation } from "./eu/index.js";
49
+ import { arxivGrammarImplementation } from "./arxiv/index.js";
48
50
  /**
49
51
  * The grammar-wave registry: flavors whose Ruby grammar has been ported
50
52
  * to a TypeScript implementation. A flavor listed here parses
@@ -172,6 +174,10 @@ function grammarImplementationSwitch(flavor) {
172
174
  return ietfGrammarImplementation();
173
175
  case "iala":
174
176
  return ialaGrammarImplementation();
177
+ case "eu":
178
+ return euGrammarImplementation();
179
+ case "arxiv":
180
+ return arxivGrammarImplementation();
175
181
  default:
176
182
  return undefined;
177
183
  }