@juherr/mobilityid 0.0.1 → 0.2.0

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/dist/index.d.ts CHANGED
@@ -1,61 +1,91 @@
1
- //#region src/check-digit-din.d.ts
2
- declare function checkDigitDin(code: string): string;
3
- //#endregion
4
- //#region src/check-digit-iso.d.ts
5
- declare function checkDigitIso(code: string): string;
6
- //#endregion
7
- //#region src/contract-id-standard.d.ts
8
- declare const ContractIdStandards: {
1
+ export declare function checkDigitDin(code: string): string;
2
+ export declare function checkDigitIso(code: string): string;
3
+ export declare const ContractIdStandards: {
9
4
  readonly ISO: "ISO";
10
5
  readonly EMI3: "EMI3";
11
6
  readonly DIN: "DIN";
12
7
  };
13
8
  type ContractIdStandard = (typeof ContractIdStandards)[keyof typeof ContractIdStandards];
14
- //#endregion
15
- //#region src/country-code.d.ts
16
- declare class CountryCode {
17
- readonly value: string;
18
- private constructor();
19
- static isValid(raw: string): boolean;
20
- static from(raw: string): CountryCode;
21
- static parse(raw: string): CountryCode | null;
22
- toString(): string;
23
- }
24
- //#endregion
25
- //#region src/operator-id.d.ts
26
- declare class OperatorIdIso {
27
- readonly value: string;
28
- private constructor();
29
- static isValid(raw: string): boolean;
30
- static from(raw: string): OperatorIdIso;
31
- static parse(raw: string): OperatorIdIso | null;
32
- toString(): string;
33
- }
34
- declare class OperatorIdDin {
35
- readonly value: string;
36
- private constructor();
37
- static isValid(raw: string): boolean;
38
- static from(raw: string): OperatorIdDin;
39
- static parse(raw: string): OperatorIdDin | null;
40
- toString(): string;
9
+ /**
10
+ * The 249 ISO 3166-1 alpha-2 codes returned by the JDK's `Locale.getISOCountries()`, the
11
+ * reference the Scala library validates country codes against. Sorted.
12
+ */
13
+ export declare const ISO_3166_ALPHA2: readonly ["AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "ZA", "ZM", "ZW"];
14
+ /**
15
+ * Machine-readable reason of a parse failure, stable across releases: count failures or report
16
+ * a broken record on it, show `error` to humans. A component code is given only when the failing
17
+ * component can be told apart reliably; anything else is `format`. The codes check structure
18
+ * and the standards' tables (ISO 3166-1), never real-world registrations.
19
+ *
20
+ * - `format`: the overall shape is not recognized, or the failing part cannot be told apart.
21
+ * - `country-code`: ISO 3166-1 alpha-2 country code.
22
+ * - `phone-country-code`: DIN phone country code.
23
+ * - `provider-id`: provider id of a contract id, party code of a party id.
24
+ * - `operator-id`: operator id of an EVSE id (ISO or DIN).
25
+ * - `id-type`: ISO EVSE id without the `E` id type in front of the power outlet id.
26
+ * - `power-outlet-id`: power outlet id of an EVSE id.
27
+ * - `instance`: instance value of a contract id.
28
+ * - `check-digit`: check digit of a contract id differs from the computed one.
29
+ * - `unconvertible`: a contract id cannot be converted to the requested standard.
30
+ */
31
+ type ParseFailureReason = "format" | "country-code" | "phone-country-code" | "provider-id" | "operator-id" | "id-type" | "power-outlet-id" | "instance" | "check-digit" | "unconvertible";
32
+ /**
33
+ * Thrown by the strict factories and parsers when the input is not a valid identifier. It extends
34
+ * `TypeError` for compatibility with callers that catch `TypeError`; `attempt` captures exactly
35
+ * this class so that a genuine programming `TypeError` (`undefined is not a function`) is never
36
+ * mistaken for an invalid input. `reason` is the code `tryParse` reports.
37
+ */
38
+ export declare class ValidationError extends TypeError {
39
+ readonly reason: ParseFailureReason;
40
+ constructor(message: string, reason?: ParseFailureReason);
41
41
  }
42
+ /**
43
+ * Outcome of a `tryParse` call: the parsed value, or the message and reason code the strict
44
+ * parser would have thrown. Narrow on `ok`; both variants are frozen like the domain objects.
45
+ */
46
+ type ParseResult<T> = Readonly<{
47
+ ok: true;
48
+ value: T;
49
+ }> | Readonly<{
50
+ ok: false;
51
+ error: string;
52
+ reason: ParseFailureReason;
53
+ }>;
54
+ declare const brand: unique symbol;
55
+ /**
56
+ * Branded string for an open set of values: a `StringId<"ProviderId">` is a plain string at
57
+ * runtime that only the `ProviderId` factory can produce at the type level. The symbol is never
58
+ * assigned; it exists only in the type system, so a branded value costs nothing over the
59
+ * underlying string. A closed set (`CountryCode`) uses a literal union instead and only shares
60
+ * the companion below.
61
+ */
62
+ type StringId<B extends string> = string & {
63
+ readonly [brand]: B;
64
+ };
65
+ /**
66
+ * The three parsing entry points of a single-valued identifier plus its predicate. `from` is
67
+ * strict and throws `ValidationError`; `tryParse` and `parse` are derived from it.
68
+ */
69
+ type StringIdCompanion<T extends string> = Readonly<{
70
+ isValid(raw: string): boolean;
71
+ from(raw: string): T;
72
+ tryParse(raw: string): ParseResult<T>;
73
+ parse(raw: string): T | null;
74
+ }>;
75
+ export type CountryCode = (typeof ISO_3166_ALPHA2)[number];
76
+ export declare const CountryCode: StringIdCompanion<CountryCode>;
77
+ export type OperatorIdIso = StringId<"OperatorIdIso">;
78
+ export declare const OperatorIdIso: StringIdCompanion<OperatorIdIso>;
79
+ export type OperatorIdDin = StringId<"OperatorIdDin">;
80
+ export declare const OperatorIdDin: StringIdCompanion<OperatorIdDin>;
42
81
  type OperatorId = OperatorIdIso | OperatorIdDin;
43
- //#endregion
44
- //#region src/provider-id.d.ts
45
- declare class ProviderId {
46
- readonly value: string;
47
- private constructor();
48
- static isValid(raw: string): boolean;
49
- static from(raw: string): ProviderId;
50
- static parse(raw: string): ProviderId | null;
51
- toString(): string;
52
- }
53
- //#endregion
54
- //#region src/party-id.d.ts
55
- declare class PartyId {
82
+ export type ProviderId = StringId<"ProviderId">;
83
+ export declare const ProviderId: StringIdCompanion<ProviderId>;
84
+ export declare class PartyId {
56
85
  readonly countryCode: CountryCode;
57
86
  readonly partyCode: string;
58
87
  private constructor();
88
+ static tryParse(raw: string): ParseResult<PartyId>;
59
89
  static parse(raw: string): PartyId | null;
60
90
  static parseStrict(raw: string): PartyId;
61
91
  static fromCountryAndProvider(countryCode: CountryCode, providerId: ProviderId): PartyId;
@@ -63,73 +93,82 @@ declare class PartyId {
63
93
  toCompactString(): string;
64
94
  toString(): string;
65
95
  }
66
- //#endregion
67
- //#region src/contract-id.d.ts
68
- declare class ContractId {
96
+ /**
97
+ * How strictly `ContractId` parsing treats the optional separators between country code,
98
+ * provider id, instance and check digit. A rejected input fails with the `format` reason.
99
+ *
100
+ * - `LENIENT`: any combination of separators the standard's format allows.
101
+ * - `CONSISTENT`: no separator at all, or the same separator at every position (the default).
102
+ * - `COMPACT`: no separator at all.
103
+ */
104
+ export declare const SeparatorPolicies: {
105
+ readonly LENIENT: "LENIENT";
106
+ readonly CONSISTENT: "CONSISTENT";
107
+ readonly COMPACT: "COMPACT";
108
+ };
109
+ type SeparatorPolicy = (typeof SeparatorPolicies)[keyof typeof SeparatorPolicies];
110
+ export declare class ContractId {
69
111
  readonly standard: ContractIdStandard;
70
112
  readonly countryCode: CountryCode;
71
113
  readonly providerId: ProviderId;
72
114
  readonly instanceValue: string;
73
115
  readonly checkDigit: string;
74
116
  private constructor();
75
- static fromParts(standard: ContractIdStandard, countryCode: string | CountryCode, providerId: string | ProviderId, instanceValue: string, checkDigit?: string): ContractId;
76
- static parse(standard: ContractIdStandard, raw: string): ContractId | null;
77
- static parseStrict(standard: ContractIdStandard, raw: string): ContractId;
117
+ static fromParts(standard: ContractIdStandard, countryCode: string, providerId: string, instanceValue: string, checkDigit?: string): ContractId;
118
+ static tryParse(standard: ContractIdStandard, raw: string, policy?: SeparatorPolicy): ParseResult<ContractId>;
119
+ static parse(standard: ContractIdStandard, raw: string, policy?: SeparatorPolicy): ContractId | null;
120
+ /**
121
+ * Parses `raw` under `standard`, then rejects its separators with the `format` reason when they
122
+ * break `policy` (default `CONSISTENT`). An unknown policy throws a plain `TypeError` before any
123
+ * parsing.
124
+ */
125
+ static parseStrict(standard: ContractIdStandard, raw: string, policy?: SeparatorPolicy): ContractId;
78
126
  toCompactString(): string;
79
127
  toCompactStringWithoutCheckDigit(): string;
80
128
  get partyId(): PartyId;
81
129
  convertTo(targetStandard: ContractIdStandard): ContractId;
82
130
  toString(): string;
83
131
  }
84
- //#endregion
85
- //#region src/phone-country-code.d.ts
86
- declare class PhoneCountryCode {
87
- readonly value: string;
88
- private constructor();
89
- static isValid(raw: string): boolean;
90
- static from(raw: string): PhoneCountryCode;
91
- static parse(raw: string): PhoneCountryCode | null;
92
- toString(): string;
93
- }
94
- //#endregion
95
- //#region src/evse-id.d.ts
132
+ export type PhoneCountryCode = StringId<"PhoneCountryCode">;
133
+ export declare const PhoneCountryCode: StringIdCompanion<PhoneCountryCode>;
96
134
  declare abstract class EvseIdBase {
97
135
  abstract readonly countryCode: CountryCode | PhoneCountryCode;
98
136
  abstract readonly operatorId: OperatorId;
99
137
  abstract readonly powerOutletId: string;
100
138
  abstract toString(): string;
101
139
  }
102
- declare class EvseIdIso extends EvseIdBase {
140
+ export declare class EvseIdIso extends EvseIdBase {
103
141
  readonly countryCode: CountryCode;
104
142
  readonly operatorId: OperatorIdIso;
105
143
  readonly powerOutletId: string;
106
144
  constructor(countryCode: CountryCode, operatorId: OperatorIdIso, powerOutletId: string);
107
145
  static fromParts(countryCode: string, operatorId: string, powerOutletId: string): EvseIdIso;
146
+ static tryParse(raw: string): ParseResult<EvseIdIso>;
108
147
  static parse(raw: string): EvseIdIso | null;
109
148
  static parseStrict(raw: string): EvseIdIso;
110
149
  get partyId(): PartyId;
111
150
  toCompactString(): string;
112
151
  toString(): string;
113
152
  }
114
- declare class EvseIdDin extends EvseIdBase {
153
+ export declare class EvseIdDin extends EvseIdBase {
115
154
  readonly countryCode: PhoneCountryCode;
116
155
  readonly operatorId: OperatorIdDin;
117
156
  readonly powerOutletId: string;
118
157
  constructor(countryCode: PhoneCountryCode, operatorId: OperatorIdDin, powerOutletId: string);
119
158
  static fromParts(countryCode: string, operatorId: string, powerOutletId: string): EvseIdDin;
159
+ static tryParse(raw: string): ParseResult<EvseIdDin>;
120
160
  static parse(raw: string): EvseIdDin | null;
121
161
  static parseStrict(raw: string): EvseIdDin;
122
162
  toString(): string;
123
163
  }
124
- type EvseId = EvseIdIso | EvseIdDin;
125
- declare const EvseId: {
164
+ export type EvseId = EvseIdIso | EvseIdDin;
165
+ export declare const EvseId: {
126
166
  fromParts(countryCode: string, operatorId: string, powerOutletId: string): EvseId;
167
+ tryParse(raw: string): ParseResult<EvseId>;
127
168
  parse(raw: string): EvseId | null;
128
169
  parseStrict(raw: string): EvseId;
129
170
  };
130
- //#endregion
131
- //#region src/parsers.d.ts
132
- declare const MobilityIdParsers: {
171
+ export declare const MobilityIdParsers: {
133
172
  parseCountryCode(raw: string): CountryCode | null;
134
173
  parsePhoneCountryCode(raw: string): PhoneCountryCode | null;
135
174
  parseProviderId(raw: string): ProviderId | null;
@@ -142,7 +181,17 @@ declare const MobilityIdParsers: {
142
181
  parseEvseId(raw: string): EvseId | null;
143
182
  parseEvseIdIso(raw: string): EvseIdIso | null;
144
183
  parseEvseIdDin(raw: string): EvseIdDin | null;
184
+ tryParseCountryCode(raw: string): ParseResult<CountryCode>;
185
+ tryParsePhoneCountryCode(raw: string): ParseResult<PhoneCountryCode>;
186
+ tryParseProviderId(raw: string): ParseResult<ProviderId>;
187
+ tryParseOperatorIdIso(raw: string): ParseResult<OperatorIdIso>;
188
+ tryParseOperatorIdDin(raw: string): ParseResult<OperatorIdDin>;
189
+ tryParsePartyId(raw: string): ParseResult<PartyId>;
190
+ tryParseContractIdIso(raw: string): ParseResult<ContractId>;
191
+ tryParseContractIdDin(raw: string): ParseResult<ContractId>;
192
+ tryParseContractIdEmi3(raw: string): ParseResult<ContractId>;
193
+ tryParseEvseId(raw: string): ParseResult<EvseId>;
194
+ tryParseEvseIdIso(raw: string): ParseResult<EvseIdIso>;
195
+ tryParseEvseIdDin(raw: string): ParseResult<EvseIdDin>;
145
196
  };
146
- //#endregion
147
- export { ContractId, type ContractIdStandard, ContractIdStandards, CountryCode, EvseId, type EvseId as EvseIdValue, EvseIdDin, EvseIdIso, MobilityIdParsers, type OperatorId, OperatorIdDin, OperatorIdIso, PartyId, PhoneCountryCode, ProviderId, checkDigitDin, checkDigitIso };
148
- //# sourceMappingURL=index.d.ts.map
197
+ export type { ContractIdStandard, EvseId as EvseIdValue, OperatorId, ParseFailureReason, ParseResult, SeparatorPolicy };