@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/LICENSE +197 -15
- package/NOTICE +6 -0
- package/README.md +162 -23
- package/dist/index.d.ts +125 -76
- package/dist/index.js +582 -241
- package/dist/index.js.map +1 -1
- package/package.json +18 -10
package/dist/index.js
CHANGED
|
@@ -1,12 +1,66 @@
|
|
|
1
|
-
|
|
1
|
+
// src/ascii.ts
|
|
2
|
+
/**
|
|
3
|
+
* Uppercases ASCII letters only. `String.prototype.toUpperCase` also maps some non-ASCII letters
|
|
4
|
+
* to ASCII ones ('ſ' U+017F to 'S', 'ı' U+0131 to 'I'), which would let the ASCII-only patterns
|
|
5
|
+
* accept them; every factory and parser normalizes through this function instead, like the
|
|
6
|
+
* other ports.
|
|
7
|
+
*/
|
|
8
|
+
function asciiUpperCase(value) {
|
|
9
|
+
return value.replace(/[a-z]+/g, (letters) => letters.toUpperCase());
|
|
10
|
+
}
|
|
11
|
+
// src/parse-result.ts
|
|
12
|
+
/**
|
|
13
|
+
* Thrown by the strict factories and parsers when the input is not a valid identifier. It extends
|
|
14
|
+
* `TypeError` for compatibility with callers that catch `TypeError`; `attempt` captures exactly
|
|
15
|
+
* this class so that a genuine programming `TypeError` (`undefined is not a function`) is never
|
|
16
|
+
* mistaken for an invalid input. `reason` is the code `tryParse` reports.
|
|
17
|
+
*/
|
|
18
|
+
var ValidationError = class extends TypeError {
|
|
19
|
+
reason;
|
|
20
|
+
constructor(message, reason = "format") {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = "ValidationError";
|
|
23
|
+
this.reason = reason;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
function success(value) {
|
|
27
|
+
return Object.freeze({
|
|
28
|
+
ok: true,
|
|
29
|
+
value
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function failure(error, reason) {
|
|
33
|
+
return Object.freeze({
|
|
34
|
+
ok: false,
|
|
35
|
+
error,
|
|
36
|
+
reason
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/** Collapses a `ParseResult` to the tolerant contract: the value, or `null` on failure. */
|
|
40
|
+
function valueOrNull(result) {
|
|
41
|
+
return result.ok ? result.value : null;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Runs a strict parser and captures its `ValidationError` as a failure. Any other error, a plain
|
|
45
|
+
* `TypeError` included, is a bug rather than an invalid input and keeps propagating.
|
|
46
|
+
*/
|
|
47
|
+
function attempt(parse) {
|
|
48
|
+
try {
|
|
49
|
+
return success(parse());
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof ValidationError) return failure(error.message, error.reason);
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// src/check-digit-din.ts
|
|
2
56
|
const values = /* @__PURE__ */ new Map();
|
|
3
57
|
for (let index = 0; index <= 9; index += 1) values.set(String(index), index);
|
|
4
58
|
for (let index = 0; index < 26; index += 1) values.set(String.fromCharCode("A".charCodeAt(0) + index), 10 + index);
|
|
5
59
|
function checkDigitDin(code) {
|
|
6
|
-
const normalized = code
|
|
60
|
+
const normalized = asciiUpperCase(code);
|
|
7
61
|
const lookup = Array.from(normalized, (char) => {
|
|
8
62
|
const value = values.get(char);
|
|
9
|
-
if (value === void 0) throw new
|
|
63
|
+
if (value === void 0) throw new ValidationError(`invalid character '${char}' in code '${code}'; must consist of uppercase ASCII letters and digits`);
|
|
10
64
|
return value;
|
|
11
65
|
});
|
|
12
66
|
let sum = 0;
|
|
@@ -24,8 +78,7 @@ function checkDigitDin(code) {
|
|
|
24
78
|
if (mod >= 10) return "X";
|
|
25
79
|
return String(mod);
|
|
26
80
|
}
|
|
27
|
-
|
|
28
|
-
//#region src/check-digit-iso.ts
|
|
81
|
+
// src/check-digit-iso.ts
|
|
29
82
|
const checkDigitInputRegex = /^[A-Z0-9]+$/;
|
|
30
83
|
const matrixKey = (m) => `${m.m11},${m.m12},${m.m21},${m.m22}`;
|
|
31
84
|
const multiplyMatrix = (a, b) => ({
|
|
@@ -122,9 +175,9 @@ const negP2minus15 = {
|
|
|
122
175
|
m22: 1
|
|
123
176
|
};
|
|
124
177
|
function checkDigitIso(code) {
|
|
125
|
-
const normalized = code
|
|
126
|
-
if (normalized.length !== p1s.length || normalized.length !== p2s.length) throw new
|
|
127
|
-
if (!checkDigitInputRegex.test(normalized)) throw new
|
|
178
|
+
const normalized = asciiUpperCase(code);
|
|
179
|
+
if (normalized.length !== p1s.length || normalized.length !== p2s.length) throw new ValidationError(`Code must have a length of ${p1s.length}`);
|
|
180
|
+
if (!checkDigitInputRegex.test(normalized)) throw new ValidationError("Code must consist of uppercase ASCII letters and digits");
|
|
128
181
|
const sumEq = (matrices, toVec) => {
|
|
129
182
|
let value = {
|
|
130
183
|
v1: 0,
|
|
@@ -133,9 +186,9 @@ function checkDigitIso(code) {
|
|
|
133
186
|
for (let index = 0; index < matrices.length; index += 1) {
|
|
134
187
|
const character = normalized.charAt(index);
|
|
135
188
|
const matrix = encoding.get(character);
|
|
136
|
-
if (!matrix) throw new
|
|
189
|
+
if (!matrix) throw new ValidationError(`Invalid character: ${character}.`);
|
|
137
190
|
const positionMatrix = matrices[index];
|
|
138
|
-
if (!positionMatrix) throw new
|
|
191
|
+
if (!positionMatrix) throw new Error(`Missing matrix at index: ${index}.`);
|
|
139
192
|
const qr = toVec(matrix);
|
|
140
193
|
value = addVec(value, multiplyVec(qr, positionMatrix));
|
|
141
194
|
}
|
|
@@ -156,79 +209,316 @@ function checkDigitIso(code) {
|
|
|
156
209
|
m22: t2.v2 % 3
|
|
157
210
|
};
|
|
158
211
|
const decoded = decoding.get(matrixKey(m15));
|
|
159
|
-
if (!decoded) throw new
|
|
212
|
+
if (!decoded) throw new Error(`Undecodable matrix: ${matrixKey(m15)}.`);
|
|
160
213
|
return decoded;
|
|
161
214
|
}
|
|
162
|
-
|
|
163
|
-
//#region src/contract-id-standard.ts
|
|
215
|
+
// src/contract-id-standard.ts
|
|
164
216
|
const ContractIdStandards = {
|
|
165
217
|
ISO: "ISO",
|
|
166
218
|
EMI3: "EMI3",
|
|
167
219
|
DIN: "DIN"
|
|
168
220
|
};
|
|
169
|
-
|
|
170
|
-
|
|
221
|
+
// src/iso3166-alpha2.ts
|
|
222
|
+
/**
|
|
223
|
+
* The 249 ISO 3166-1 alpha-2 codes returned by the JDK's `Locale.getISOCountries()`, the
|
|
224
|
+
* reference the Scala library validates country codes against. Sorted.
|
|
225
|
+
*/
|
|
226
|
+
const ISO_3166_ALPHA2 = [
|
|
227
|
+
"AD",
|
|
228
|
+
"AE",
|
|
229
|
+
"AF",
|
|
230
|
+
"AG",
|
|
231
|
+
"AI",
|
|
232
|
+
"AL",
|
|
233
|
+
"AM",
|
|
234
|
+
"AO",
|
|
235
|
+
"AQ",
|
|
236
|
+
"AR",
|
|
237
|
+
"AS",
|
|
238
|
+
"AT",
|
|
239
|
+
"AU",
|
|
240
|
+
"AW",
|
|
241
|
+
"AX",
|
|
242
|
+
"AZ",
|
|
243
|
+
"BA",
|
|
244
|
+
"BB",
|
|
245
|
+
"BD",
|
|
246
|
+
"BE",
|
|
247
|
+
"BF",
|
|
248
|
+
"BG",
|
|
249
|
+
"BH",
|
|
250
|
+
"BI",
|
|
251
|
+
"BJ",
|
|
252
|
+
"BL",
|
|
253
|
+
"BM",
|
|
254
|
+
"BN",
|
|
255
|
+
"BO",
|
|
256
|
+
"BQ",
|
|
257
|
+
"BR",
|
|
258
|
+
"BS",
|
|
259
|
+
"BT",
|
|
260
|
+
"BV",
|
|
261
|
+
"BW",
|
|
262
|
+
"BY",
|
|
263
|
+
"BZ",
|
|
264
|
+
"CA",
|
|
265
|
+
"CC",
|
|
266
|
+
"CD",
|
|
267
|
+
"CF",
|
|
268
|
+
"CG",
|
|
269
|
+
"CH",
|
|
270
|
+
"CI",
|
|
271
|
+
"CK",
|
|
272
|
+
"CL",
|
|
273
|
+
"CM",
|
|
274
|
+
"CN",
|
|
275
|
+
"CO",
|
|
276
|
+
"CR",
|
|
277
|
+
"CU",
|
|
278
|
+
"CV",
|
|
279
|
+
"CW",
|
|
280
|
+
"CX",
|
|
281
|
+
"CY",
|
|
282
|
+
"CZ",
|
|
283
|
+
"DE",
|
|
284
|
+
"DJ",
|
|
285
|
+
"DK",
|
|
286
|
+
"DM",
|
|
287
|
+
"DO",
|
|
288
|
+
"DZ",
|
|
289
|
+
"EC",
|
|
290
|
+
"EE",
|
|
291
|
+
"EG",
|
|
292
|
+
"EH",
|
|
293
|
+
"ER",
|
|
294
|
+
"ES",
|
|
295
|
+
"ET",
|
|
296
|
+
"FI",
|
|
297
|
+
"FJ",
|
|
298
|
+
"FK",
|
|
299
|
+
"FM",
|
|
300
|
+
"FO",
|
|
301
|
+
"FR",
|
|
302
|
+
"GA",
|
|
303
|
+
"GB",
|
|
304
|
+
"GD",
|
|
305
|
+
"GE",
|
|
306
|
+
"GF",
|
|
307
|
+
"GG",
|
|
308
|
+
"GH",
|
|
309
|
+
"GI",
|
|
310
|
+
"GL",
|
|
311
|
+
"GM",
|
|
312
|
+
"GN",
|
|
313
|
+
"GP",
|
|
314
|
+
"GQ",
|
|
315
|
+
"GR",
|
|
316
|
+
"GS",
|
|
317
|
+
"GT",
|
|
318
|
+
"GU",
|
|
319
|
+
"GW",
|
|
320
|
+
"GY",
|
|
321
|
+
"HK",
|
|
322
|
+
"HM",
|
|
323
|
+
"HN",
|
|
324
|
+
"HR",
|
|
325
|
+
"HT",
|
|
326
|
+
"HU",
|
|
327
|
+
"ID",
|
|
328
|
+
"IE",
|
|
329
|
+
"IL",
|
|
330
|
+
"IM",
|
|
331
|
+
"IN",
|
|
332
|
+
"IO",
|
|
333
|
+
"IQ",
|
|
334
|
+
"IR",
|
|
335
|
+
"IS",
|
|
336
|
+
"IT",
|
|
337
|
+
"JE",
|
|
338
|
+
"JM",
|
|
339
|
+
"JO",
|
|
340
|
+
"JP",
|
|
341
|
+
"KE",
|
|
342
|
+
"KG",
|
|
343
|
+
"KH",
|
|
344
|
+
"KI",
|
|
345
|
+
"KM",
|
|
346
|
+
"KN",
|
|
347
|
+
"KP",
|
|
348
|
+
"KR",
|
|
349
|
+
"KW",
|
|
350
|
+
"KY",
|
|
351
|
+
"KZ",
|
|
352
|
+
"LA",
|
|
353
|
+
"LB",
|
|
354
|
+
"LC",
|
|
355
|
+
"LI",
|
|
356
|
+
"LK",
|
|
357
|
+
"LR",
|
|
358
|
+
"LS",
|
|
359
|
+
"LT",
|
|
360
|
+
"LU",
|
|
361
|
+
"LV",
|
|
362
|
+
"LY",
|
|
363
|
+
"MA",
|
|
364
|
+
"MC",
|
|
365
|
+
"MD",
|
|
366
|
+
"ME",
|
|
367
|
+
"MF",
|
|
368
|
+
"MG",
|
|
369
|
+
"MH",
|
|
370
|
+
"MK",
|
|
371
|
+
"ML",
|
|
372
|
+
"MM",
|
|
373
|
+
"MN",
|
|
374
|
+
"MO",
|
|
375
|
+
"MP",
|
|
376
|
+
"MQ",
|
|
377
|
+
"MR",
|
|
378
|
+
"MS",
|
|
379
|
+
"MT",
|
|
380
|
+
"MU",
|
|
381
|
+
"MV",
|
|
382
|
+
"MW",
|
|
383
|
+
"MX",
|
|
384
|
+
"MY",
|
|
385
|
+
"MZ",
|
|
386
|
+
"NA",
|
|
387
|
+
"NC",
|
|
388
|
+
"NE",
|
|
389
|
+
"NF",
|
|
390
|
+
"NG",
|
|
391
|
+
"NI",
|
|
392
|
+
"NL",
|
|
393
|
+
"NO",
|
|
394
|
+
"NP",
|
|
395
|
+
"NR",
|
|
396
|
+
"NU",
|
|
397
|
+
"NZ",
|
|
398
|
+
"OM",
|
|
399
|
+
"PA",
|
|
400
|
+
"PE",
|
|
401
|
+
"PF",
|
|
402
|
+
"PG",
|
|
403
|
+
"PH",
|
|
404
|
+
"PK",
|
|
405
|
+
"PL",
|
|
406
|
+
"PM",
|
|
407
|
+
"PN",
|
|
408
|
+
"PR",
|
|
409
|
+
"PS",
|
|
410
|
+
"PT",
|
|
411
|
+
"PW",
|
|
412
|
+
"PY",
|
|
413
|
+
"QA",
|
|
414
|
+
"RE",
|
|
415
|
+
"RO",
|
|
416
|
+
"RS",
|
|
417
|
+
"RU",
|
|
418
|
+
"RW",
|
|
419
|
+
"SA",
|
|
420
|
+
"SB",
|
|
421
|
+
"SC",
|
|
422
|
+
"SD",
|
|
423
|
+
"SE",
|
|
424
|
+
"SG",
|
|
425
|
+
"SH",
|
|
426
|
+
"SI",
|
|
427
|
+
"SJ",
|
|
428
|
+
"SK",
|
|
429
|
+
"SL",
|
|
430
|
+
"SM",
|
|
431
|
+
"SN",
|
|
432
|
+
"SO",
|
|
433
|
+
"SR",
|
|
434
|
+
"SS",
|
|
435
|
+
"ST",
|
|
436
|
+
"SV",
|
|
437
|
+
"SX",
|
|
438
|
+
"SY",
|
|
439
|
+
"SZ",
|
|
440
|
+
"TC",
|
|
441
|
+
"TD",
|
|
442
|
+
"TF",
|
|
443
|
+
"TG",
|
|
444
|
+
"TH",
|
|
445
|
+
"TJ",
|
|
446
|
+
"TK",
|
|
447
|
+
"TL",
|
|
448
|
+
"TM",
|
|
449
|
+
"TN",
|
|
450
|
+
"TO",
|
|
451
|
+
"TR",
|
|
452
|
+
"TT",
|
|
453
|
+
"TV",
|
|
454
|
+
"TW",
|
|
455
|
+
"TZ",
|
|
456
|
+
"UA",
|
|
457
|
+
"UG",
|
|
458
|
+
"UM",
|
|
459
|
+
"US",
|
|
460
|
+
"UY",
|
|
461
|
+
"UZ",
|
|
462
|
+
"VA",
|
|
463
|
+
"VC",
|
|
464
|
+
"VE",
|
|
465
|
+
"VG",
|
|
466
|
+
"VI",
|
|
467
|
+
"VN",
|
|
468
|
+
"VU",
|
|
469
|
+
"WF",
|
|
470
|
+
"WS",
|
|
471
|
+
"YE",
|
|
472
|
+
"YT",
|
|
473
|
+
"ZA",
|
|
474
|
+
"ZM",
|
|
475
|
+
"ZW"
|
|
476
|
+
];
|
|
477
|
+
// src/string-id.ts
|
|
478
|
+
/**
|
|
479
|
+
* Builds the companion of a string identifier (branded or literal union). Valid input is
|
|
480
|
+
* normalized to uppercase (a no-op for the digit-only identifiers); `isValid` stays a boolean
|
|
481
|
+
* rather than a type guard because it accepts the non-normalized input that `from` still has to
|
|
482
|
+
* uppercase.
|
|
483
|
+
*/
|
|
484
|
+
function defineStringId(spec) {
|
|
485
|
+
const from = (raw) => {
|
|
486
|
+
if (!spec.isValid(raw)) throw new ValidationError(spec.message(raw), spec.reason);
|
|
487
|
+
return asciiUpperCase(raw);
|
|
488
|
+
};
|
|
489
|
+
const tryParse = (raw) => attempt(() => from(raw));
|
|
490
|
+
const parse = (raw) => valueOrNull(tryParse(raw));
|
|
491
|
+
return Object.freeze({
|
|
492
|
+
isValid: spec.isValid,
|
|
493
|
+
from,
|
|
494
|
+
tryParse,
|
|
495
|
+
parse
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
// src/country-code.ts
|
|
171
499
|
const COUNTRY_CODE_REGEX = /^[A-Za-z]{2}$/;
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
static isValid(raw) {
|
|
180
|
-
if (!COUNTRY_CODE_REGEX.test(raw)) return false;
|
|
181
|
-
const normalized = raw.toUpperCase();
|
|
182
|
-
const display = regionNames.of(normalized);
|
|
183
|
-
return display !== void 0 && display !== normalized && display !== "Unknown Region";
|
|
184
|
-
}
|
|
185
|
-
static from(raw) {
|
|
186
|
-
if (!CountryCode.isValid(raw)) throw new TypeError("Country Code must be valid according to ISO 3166-1 alpha-2");
|
|
187
|
-
return new CountryCode(raw.toUpperCase());
|
|
188
|
-
}
|
|
189
|
-
static parse(raw) {
|
|
190
|
-
try {
|
|
191
|
-
return CountryCode.from(raw);
|
|
192
|
-
} catch {
|
|
193
|
-
return null;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
toString() {
|
|
197
|
-
return this.value;
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
//#endregion
|
|
201
|
-
//#region src/provider-id.ts
|
|
500
|
+
const KNOWN_CODES = new Set(ISO_3166_ALPHA2);
|
|
501
|
+
const CountryCode = defineStringId({
|
|
502
|
+
isValid: (raw) => COUNTRY_CODE_REGEX.test(raw) && KNOWN_CODES.has(asciiUpperCase(raw)),
|
|
503
|
+
message: () => "Country Code must be valid according to ISO 3166-1 alpha-2",
|
|
504
|
+
reason: "country-code"
|
|
505
|
+
});
|
|
506
|
+
// src/provider-id.ts
|
|
202
507
|
const PARTY_CODE_REGEX = /^[A-Za-z0-9]{3}$/;
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
static from(raw) {
|
|
216
|
-
if (!ProviderId.isValid(raw)) throw new TypeError("OperatorId must have a length of 3 and be ASCII letters or digits");
|
|
217
|
-
return new ProviderId(raw.toUpperCase());
|
|
218
|
-
}
|
|
219
|
-
static parse(raw) {
|
|
220
|
-
try {
|
|
221
|
-
return ProviderId.from(raw);
|
|
222
|
-
} catch {
|
|
223
|
-
return null;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
toString() {
|
|
227
|
-
return this.value;
|
|
228
|
-
}
|
|
508
|
+
/**
|
|
509
|
+
* Shared by `ProviderId` and `OperatorIdIso`: both are the 3-character party code of a party id.
|
|
510
|
+
* The message names `OperatorId` for both on purpose: it is the wording of the Scala reference
|
|
511
|
+
* (`PartyCode` in `basicIdentifiers.scala`, also shared by its two identifiers) and of the Java
|
|
512
|
+
* port, kept for cross-port parity of the error contract. The reason is `provider-id`;
|
|
513
|
+
* `OperatorIdIso` overrides it with `operator-id`.
|
|
514
|
+
*/
|
|
515
|
+
const partyCodeSpec = {
|
|
516
|
+
isValid: (raw) => PARTY_CODE_REGEX.test(raw),
|
|
517
|
+
message: () => "OperatorId must have a length of 3 and be ASCII letters or digits",
|
|
518
|
+
reason: "provider-id"
|
|
229
519
|
};
|
|
230
|
-
|
|
231
|
-
|
|
520
|
+
const ProviderId = defineStringId(partyCodeSpec);
|
|
521
|
+
// src/party-id.ts
|
|
232
522
|
const PARTY_ID_REGEX = /^([A-Za-z]{2})[-*]?([A-Za-z0-9]{3})$/;
|
|
233
523
|
var PartyId = class PartyId {
|
|
234
524
|
countryCode;
|
|
@@ -238,40 +528,66 @@ var PartyId = class PartyId {
|
|
|
238
528
|
this.partyCode = partyCode;
|
|
239
529
|
Object.freeze(this);
|
|
240
530
|
}
|
|
531
|
+
static tryParse(raw) {
|
|
532
|
+
return attempt(() => PartyId.parseStrict(raw));
|
|
533
|
+
}
|
|
241
534
|
static parse(raw) {
|
|
242
|
-
|
|
243
|
-
if (!match) return null;
|
|
244
|
-
try {
|
|
245
|
-
const country = match[1];
|
|
246
|
-
const party = match[2];
|
|
247
|
-
if (country === void 0 || party === void 0) return null;
|
|
248
|
-
const countryCode = CountryCode.from(country);
|
|
249
|
-
const partyCode = ProviderId.from(party).value;
|
|
250
|
-
return new PartyId(countryCode, partyCode);
|
|
251
|
-
} catch {
|
|
252
|
-
return null;
|
|
253
|
-
}
|
|
535
|
+
return valueOrNull(PartyId.tryParse(raw));
|
|
254
536
|
}
|
|
255
537
|
static parseStrict(raw) {
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
538
|
+
const match = PARTY_ID_REGEX.exec(raw);
|
|
539
|
+
const country = match?.[1];
|
|
540
|
+
const party = match?.[2];
|
|
541
|
+
if (country === void 0 || party === void 0) throw new ValidationError(`Invalid party ID: ${raw}`);
|
|
542
|
+
return new PartyId(CountryCode.from(country), ProviderId.from(party));
|
|
259
543
|
}
|
|
260
544
|
static fromCountryAndProvider(countryCode, providerId) {
|
|
261
|
-
return new PartyId(countryCode, providerId
|
|
545
|
+
return new PartyId(countryCode, providerId);
|
|
262
546
|
}
|
|
263
547
|
static fromCountryAndOperator(countryCode, operatorId) {
|
|
264
|
-
return new PartyId(countryCode, operatorId
|
|
548
|
+
return new PartyId(countryCode, operatorId);
|
|
265
549
|
}
|
|
266
550
|
toCompactString() {
|
|
267
|
-
return `${this.countryCode
|
|
551
|
+
return `${this.countryCode}${this.partyCode}`;
|
|
268
552
|
}
|
|
269
553
|
toString() {
|
|
270
|
-
return `${this.countryCode
|
|
554
|
+
return `${this.countryCode}-${this.partyCode}`;
|
|
271
555
|
}
|
|
272
556
|
};
|
|
273
|
-
|
|
274
|
-
|
|
557
|
+
// src/separator-policy.ts
|
|
558
|
+
/**
|
|
559
|
+
* How strictly `ContractId` parsing treats the optional separators between country code,
|
|
560
|
+
* provider id, instance and check digit. A rejected input fails with the `format` reason.
|
|
561
|
+
*
|
|
562
|
+
* - `LENIENT`: any combination of separators the standard's format allows.
|
|
563
|
+
* - `CONSISTENT`: no separator at all, or the same separator at every position (the default).
|
|
564
|
+
* - `COMPACT`: no separator at all.
|
|
565
|
+
*/
|
|
566
|
+
const SeparatorPolicies = {
|
|
567
|
+
LENIENT: "LENIENT",
|
|
568
|
+
CONSISTENT: "CONSISTENT",
|
|
569
|
+
COMPACT: "COMPACT"
|
|
570
|
+
};
|
|
571
|
+
const separatorsOf = (raw) => raw.replace(/[A-Za-z0-9]/g, "");
|
|
572
|
+
const rules = {
|
|
573
|
+
LENIENT: () => true,
|
|
574
|
+
CONSISTENT: (raw, positions) => {
|
|
575
|
+
const separators = separatorsOf(raw);
|
|
576
|
+
return separators.length === 0 || separators === separators.charAt(0).repeat(positions);
|
|
577
|
+
},
|
|
578
|
+
COMPACT: (raw) => separatorsOf(raw).length === 0
|
|
579
|
+
};
|
|
580
|
+
/**
|
|
581
|
+
* The rule of `policy`; an unknown policy is a programming error and throws a plain `TypeError`
|
|
582
|
+
* (not a `ValidationError`), so callers resolve it before parsing anything.
|
|
583
|
+
*
|
|
584
|
+
* @internal
|
|
585
|
+
*/
|
|
586
|
+
function separatorRule(policy) {
|
|
587
|
+
if (!Object.hasOwn(rules, policy)) throw new TypeError(`Unknown separator policy ${String(policy)}`);
|
|
588
|
+
return rules[policy];
|
|
589
|
+
}
|
|
590
|
+
// src/contract-id.ts
|
|
275
591
|
const parsers = {
|
|
276
592
|
ISO: {
|
|
277
593
|
instanceRegex: /^([A-Za-z0-9]{9})$/,
|
|
@@ -308,33 +624,39 @@ var ContractId = class ContractId {
|
|
|
308
624
|
}
|
|
309
625
|
static fromParts(standard, countryCode, providerId, instanceValue, checkDigit) {
|
|
310
626
|
const parser = parsers[standard];
|
|
311
|
-
const cc =
|
|
312
|
-
const provider =
|
|
313
|
-
const normalizedInstance = instanceValue
|
|
314
|
-
if (!parser.instanceRegex.test(normalizedInstance)) throw new
|
|
315
|
-
const computed = parser.computeCheckDigit(`${cc
|
|
316
|
-
if (checkDigit !== void 0 && checkDigit
|
|
627
|
+
const cc = CountryCode.from(countryCode);
|
|
628
|
+
const provider = ProviderId.from(providerId);
|
|
629
|
+
const normalizedInstance = asciiUpperCase(instanceValue);
|
|
630
|
+
if (!parser.instanceRegex.test(normalizedInstance)) throw new ValidationError(`${instanceValue} is not a valid instance value for ${parser.name} format`, "instance");
|
|
631
|
+
const computed = parser.computeCheckDigit(`${cc}${provider}${normalizedInstance}`);
|
|
632
|
+
if (checkDigit !== void 0 && asciiUpperCase(checkDigit) !== computed) throw new ValidationError(`Given check digit '${checkDigit}' is not equal to computed '${computed}'`, "check-digit");
|
|
317
633
|
return new ContractId(standard, cc, provider, normalizedInstance, computed);
|
|
318
634
|
}
|
|
319
|
-
static
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
}
|
|
635
|
+
static tryParse(standard, raw, policy = SeparatorPolicies.CONSISTENT) {
|
|
636
|
+
return attempt(() => ContractId.parseStrict(standard, raw, policy));
|
|
637
|
+
}
|
|
638
|
+
static parse(standard, raw, policy = SeparatorPolicies.CONSISTENT) {
|
|
639
|
+
return valueOrNull(ContractId.tryParse(standard, raw, policy));
|
|
325
640
|
}
|
|
326
|
-
|
|
641
|
+
/**
|
|
642
|
+
* Parses `raw` under `standard`, then rejects its separators with the `format` reason when they
|
|
643
|
+
* break `policy` (default `CONSISTENT`). An unknown policy throws a plain `TypeError` before any
|
|
644
|
+
* parsing.
|
|
645
|
+
*/
|
|
646
|
+
static parseStrict(standard, raw, policy = SeparatorPolicies.CONSISTENT) {
|
|
647
|
+
const followsPolicy = separatorRule(policy);
|
|
327
648
|
const parser = parsers[standard];
|
|
328
649
|
const match = parser.fullRegex.exec(raw);
|
|
329
|
-
if (!match || match[1] === void 0 || match[2] === void 0 || match[3] === void 0) throw new
|
|
650
|
+
if (!match || match[1] === void 0 || match[2] === void 0 || match[3] === void 0) throw new ValidationError(`${raw} is not a valid Contract Id for ${parser.name}`);
|
|
651
|
+
if (!followsPolicy(raw, match[4] === void 0 ? 2 : 3)) throw new ValidationError(`${raw} does not follow the ${policy} separator policy for ${parser.name}`);
|
|
330
652
|
const [, country, provider, instance, check] = match;
|
|
331
653
|
return ContractId.fromParts(standard, country, provider, instance, check);
|
|
332
654
|
}
|
|
333
655
|
toCompactString() {
|
|
334
|
-
return `${this.countryCode
|
|
656
|
+
return `${this.countryCode}${this.providerId}${this.instanceValue}${this.checkDigit}`;
|
|
335
657
|
}
|
|
336
658
|
toCompactStringWithoutCheckDigit() {
|
|
337
|
-
return `${this.countryCode
|
|
659
|
+
return `${this.countryCode}${this.providerId}${this.instanceValue}`;
|
|
338
660
|
}
|
|
339
661
|
get partyId() {
|
|
340
662
|
return PartyId.fromCountryAndProvider(this.countryCode, this.providerId);
|
|
@@ -343,113 +665,59 @@ var ContractId = class ContractId {
|
|
|
343
665
|
if (targetStandard === this.standard) return this;
|
|
344
666
|
if (this.standard === ContractIdStandards.DIN && targetStandard === ContractIdStandards.EMI3) return ContractId.fromParts(ContractIdStandards.EMI3, this.countryCode, this.providerId, `C0${this.instanceValue}${this.checkDigit}`);
|
|
345
667
|
if (this.standard === ContractIdStandards.EMI3 && targetStandard === ContractIdStandards.DIN) {
|
|
346
|
-
if (!this.instanceValue.startsWith("C0")) throw new
|
|
668
|
+
if (!this.instanceValue.startsWith("C0")) throw new ValidationError(`${this.toString()} cannot be converted to ${parsers.DIN.name} format`, "unconvertible");
|
|
347
669
|
const dinInstance = this.instanceValue.slice(2, 8);
|
|
348
670
|
const dinCheck = this.instanceValue.slice(8, 9);
|
|
349
671
|
return ContractId.fromParts(ContractIdStandards.DIN, this.countryCode, this.providerId, dinInstance, dinCheck);
|
|
350
672
|
}
|
|
351
673
|
if (this.standard === ContractIdStandards.ISO && targetStandard === ContractIdStandards.DIN) {
|
|
352
|
-
if (!this.instanceValue.startsWith("00")) throw new
|
|
674
|
+
if (!this.instanceValue.startsWith("00")) throw new ValidationError(`${this.toString()} cannot be converted to ${parsers.DIN.name} format`, "unconvertible");
|
|
353
675
|
const dinInstance = this.instanceValue.slice(2, 8);
|
|
354
676
|
const dinCheck = this.instanceValue.slice(8, 9);
|
|
355
677
|
return ContractId.fromParts(ContractIdStandards.DIN, this.countryCode, this.providerId, dinInstance, dinCheck);
|
|
356
678
|
}
|
|
357
679
|
if (this.standard === ContractIdStandards.DIN && targetStandard === ContractIdStandards.ISO) return ContractId.fromParts(ContractIdStandards.ISO, this.countryCode, this.providerId, `00${this.instanceValue}${this.checkDigit}`);
|
|
358
680
|
if (this.standard === ContractIdStandards.EMI3 && targetStandard === ContractIdStandards.ISO) return ContractId.fromParts(ContractIdStandards.ISO, this.countryCode, this.providerId, this.instanceValue, this.checkDigit);
|
|
359
|
-
if (this.standard === ContractIdStandards.ISO && targetStandard === ContractIdStandards.EMI3)
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
toString() {
|
|
363
|
-
return `${this.countryCode.toString()}-${this.providerId.toString()}-${this.instanceValue}-${this.checkDigit}`;
|
|
364
|
-
}
|
|
365
|
-
};
|
|
366
|
-
//#endregion
|
|
367
|
-
//#region src/operator-id.ts
|
|
368
|
-
const OPERATOR_DIN_REGEX = /^[0-9]{3,6}$/;
|
|
369
|
-
var OperatorIdIso = class OperatorIdIso {
|
|
370
|
-
value;
|
|
371
|
-
constructor(value) {
|
|
372
|
-
this.value = value;
|
|
373
|
-
Object.freeze(this);
|
|
374
|
-
}
|
|
375
|
-
static isValid(raw) {
|
|
376
|
-
return isValidPartyCode(raw);
|
|
377
|
-
}
|
|
378
|
-
static from(raw) {
|
|
379
|
-
if (!OperatorIdIso.isValid(raw)) throw new TypeError("OperatorId must have a length of 3 and be ASCII letters or digits");
|
|
380
|
-
return new OperatorIdIso(raw.toUpperCase());
|
|
381
|
-
}
|
|
382
|
-
static parse(raw) {
|
|
383
|
-
try {
|
|
384
|
-
return OperatorIdIso.from(raw);
|
|
385
|
-
} catch {
|
|
386
|
-
return null;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
toString() {
|
|
390
|
-
return this.value;
|
|
391
|
-
}
|
|
392
|
-
};
|
|
393
|
-
var OperatorIdDin = class OperatorIdDin {
|
|
394
|
-
value;
|
|
395
|
-
constructor(value) {
|
|
396
|
-
this.value = value;
|
|
397
|
-
Object.freeze(this);
|
|
398
|
-
}
|
|
399
|
-
static isValid(raw) {
|
|
400
|
-
return OPERATOR_DIN_REGEX.test(raw);
|
|
401
|
-
}
|
|
402
|
-
static from(raw) {
|
|
403
|
-
if (!OperatorIdDin.isValid(raw)) throw new TypeError("OperatorId must have a length of 3-6 chars and be digits");
|
|
404
|
-
return new OperatorIdDin(raw.toUpperCase());
|
|
405
|
-
}
|
|
406
|
-
static parse(raw) {
|
|
407
|
-
try {
|
|
408
|
-
return OperatorIdDin.from(raw);
|
|
409
|
-
} catch {
|
|
410
|
-
return null;
|
|
681
|
+
if (this.standard === ContractIdStandards.ISO && targetStandard === ContractIdStandards.EMI3) {
|
|
682
|
+
if (!parsers.EMI3.instanceRegex.test(this.instanceValue)) throw new ValidationError(`${this.instanceValue} is not a valid instance value for ${parsers.EMI3.name} format`, "unconvertible");
|
|
683
|
+
return ContractId.fromParts(ContractIdStandards.EMI3, this.countryCode, this.providerId, this.instanceValue, this.checkDigit);
|
|
411
684
|
}
|
|
685
|
+
throw new ValidationError(`It is not possible to convert ${this.standard} to ${targetStandard}`, "unconvertible");
|
|
412
686
|
}
|
|
413
687
|
toString() {
|
|
414
|
-
return this.
|
|
688
|
+
return `${this.countryCode}-${this.providerId}-${this.instanceValue}-${this.checkDigit}`;
|
|
415
689
|
}
|
|
416
690
|
};
|
|
417
|
-
|
|
418
|
-
|
|
691
|
+
// src/operator-id.ts
|
|
692
|
+
const OPERATOR_DIN_REGEX = /^[0-9]{3,6}$/;
|
|
693
|
+
const OperatorIdIso = defineStringId({
|
|
694
|
+
...partyCodeSpec,
|
|
695
|
+
reason: "operator-id"
|
|
696
|
+
});
|
|
697
|
+
const OperatorIdDin = defineStringId({
|
|
698
|
+
isValid: (raw) => OPERATOR_DIN_REGEX.test(raw),
|
|
699
|
+
message: () => "OperatorId must have a length of 3-6 chars and be digits",
|
|
700
|
+
reason: "operator-id"
|
|
701
|
+
});
|
|
702
|
+
// src/phone-country-code.ts
|
|
419
703
|
const PHONE_COUNTRY_CODE_REGEX = /^\+?[0-9]{1,3}$/;
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
static isValid(raw) {
|
|
427
|
-
return PHONE_COUNTRY_CODE_REGEX.test(raw);
|
|
428
|
-
}
|
|
429
|
-
static from(raw) {
|
|
430
|
-
if (!PhoneCountryCode.isValid(raw)) throw new TypeError(`phone Country Code must start with a '+' sign and be followed by 1-3 digits. (Was: ${raw})`);
|
|
431
|
-
return new PhoneCountryCode(raw);
|
|
432
|
-
}
|
|
433
|
-
static parse(raw) {
|
|
434
|
-
try {
|
|
435
|
-
return PhoneCountryCode.from(raw);
|
|
436
|
-
} catch {
|
|
437
|
-
return null;
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
toString() {
|
|
441
|
-
return this.value;
|
|
442
|
-
}
|
|
443
|
-
};
|
|
444
|
-
//#endregion
|
|
445
|
-
//#region src/evse-id.ts
|
|
704
|
+
const PhoneCountryCode = defineStringId({
|
|
705
|
+
isValid: (raw) => PHONE_COUNTRY_CODE_REGEX.test(raw),
|
|
706
|
+
message: (raw) => `phone Country Code must start with a '+' sign and be followed by 1-3 digits. (Was: ${raw})`,
|
|
707
|
+
reason: "phone-country-code"
|
|
708
|
+
});
|
|
709
|
+
// src/evse-id.ts
|
|
446
710
|
const ISO_COUNTRY_REGEX = /^([A-Za-z]{2})$/;
|
|
447
711
|
const ISO_OPERATOR_REGEX = /^([A-Za-z0-9]{3})$/;
|
|
448
712
|
const ISO_POWER_OUTLET_REGEX = /^([A-Za-z0-9*]{1,31})$/;
|
|
449
|
-
const
|
|
713
|
+
const ISO_COUNTRY_AND_OPERATOR_REGEX = /^[A-Za-z]{2}[A-Za-z0-9]{3}/;
|
|
714
|
+
const ISO_OPERATOR_AND_ID_TYPE_REGEX = /^[A-Za-z0-9]{3}[Ee]/;
|
|
715
|
+
const ISO_EVSE_REGEX = /^([A-Za-z]{2})\*?([A-Za-z0-9]{3})\*?[Ee]([A-Za-z0-9*]{1,31})$/;
|
|
450
716
|
const DIN_COUNTRY_REGEX = /^(\+?[0-9]{1,3})$/;
|
|
451
717
|
const DIN_OPERATOR_REGEX = /^([0-9]{3,6})$/;
|
|
452
718
|
const DIN_POWER_OUTLET_REGEX = /^([0-9*]{1,32})$/;
|
|
719
|
+
const DIN_COUNTRY_AND_OPERATOR_REGEX = /^\+?[0-9]{4,}$/;
|
|
720
|
+
const DIN_OVERLONG_OPERATOR_REGEX = /^[0-9]{7,}$/;
|
|
453
721
|
const DIN_EVSE_REGEX = /^(\+?[0-9]{1,3})\*([0-9]{3,6})\*([0-9*]{1,32})$/;
|
|
454
722
|
var EvseIdBase = class {};
|
|
455
723
|
var EvseIdIso = class EvseIdIso extends EvseIdBase {
|
|
@@ -464,21 +732,18 @@ var EvseIdIso = class EvseIdIso extends EvseIdBase {
|
|
|
464
732
|
Object.freeze(this);
|
|
465
733
|
}
|
|
466
734
|
static fromParts(countryCode, operatorId, powerOutletId) {
|
|
467
|
-
return createIso(countryCode
|
|
735
|
+
return createIso(asciiUpperCase(countryCode), asciiUpperCase(operatorId), asciiUpperCase(powerOutletId));
|
|
736
|
+
}
|
|
737
|
+
static tryParse(raw) {
|
|
738
|
+
return attempt(() => EvseIdIso.parseStrict(raw));
|
|
468
739
|
}
|
|
469
740
|
static parse(raw) {
|
|
470
|
-
|
|
471
|
-
if (!match || match[1] === void 0 || match[2] === void 0 || match[3] === void 0) return null;
|
|
472
|
-
try {
|
|
473
|
-
return createIso(match[1].toUpperCase(), match[2].toUpperCase(), match[3].toUpperCase());
|
|
474
|
-
} catch {
|
|
475
|
-
return null;
|
|
476
|
-
}
|
|
741
|
+
return valueOrNull(EvseIdIso.tryParse(raw));
|
|
477
742
|
}
|
|
478
743
|
static parseStrict(raw) {
|
|
479
|
-
const
|
|
480
|
-
if (
|
|
481
|
-
return
|
|
744
|
+
const match = ISO_EVSE_REGEX.exec(raw);
|
|
745
|
+
if (!match || match[1] === void 0 || match[2] === void 0 || match[3] === void 0) throw new ValidationError(`Invalid ISO EVSE ID: ${raw}`, diagnoseIso(raw));
|
|
746
|
+
return createIso(asciiUpperCase(match[1]), asciiUpperCase(match[2]), asciiUpperCase(match[3]));
|
|
482
747
|
}
|
|
483
748
|
get partyId() {
|
|
484
749
|
return PartyId.fromCountryAndOperator(this.countryCode, this.operatorId);
|
|
@@ -487,7 +752,7 @@ var EvseIdIso = class EvseIdIso extends EvseIdBase {
|
|
|
487
752
|
return this.toString().replaceAll("*", "");
|
|
488
753
|
}
|
|
489
754
|
toString() {
|
|
490
|
-
return `${this.countryCode
|
|
755
|
+
return `${this.countryCode}*${this.operatorId}*E${this.powerOutletId}`;
|
|
491
756
|
}
|
|
492
757
|
};
|
|
493
758
|
var EvseIdDin = class EvseIdDin extends EvseIdBase {
|
|
@@ -502,88 +767,129 @@ var EvseIdDin = class EvseIdDin extends EvseIdBase {
|
|
|
502
767
|
Object.freeze(this);
|
|
503
768
|
}
|
|
504
769
|
static fromParts(countryCode, operatorId, powerOutletId) {
|
|
505
|
-
return createDin(countryCode
|
|
770
|
+
return createDin(asciiUpperCase(countryCode), asciiUpperCase(operatorId), asciiUpperCase(powerOutletId));
|
|
771
|
+
}
|
|
772
|
+
static tryParse(raw) {
|
|
773
|
+
return attempt(() => EvseIdDin.parseStrict(raw));
|
|
506
774
|
}
|
|
507
775
|
static parse(raw) {
|
|
508
|
-
|
|
509
|
-
if (!match || match[1] === void 0 || match[2] === void 0 || match[3] === void 0) return null;
|
|
510
|
-
try {
|
|
511
|
-
return createDin(match[1].toUpperCase(), match[2].toUpperCase(), match[3].toUpperCase());
|
|
512
|
-
} catch {
|
|
513
|
-
return null;
|
|
514
|
-
}
|
|
776
|
+
return valueOrNull(EvseIdDin.tryParse(raw));
|
|
515
777
|
}
|
|
516
778
|
static parseStrict(raw) {
|
|
517
|
-
const
|
|
518
|
-
if (
|
|
519
|
-
return
|
|
779
|
+
const match = DIN_EVSE_REGEX.exec(raw);
|
|
780
|
+
if (!match || match[1] === void 0 || match[2] === void 0 || match[3] === void 0) throw new ValidationError(`Invalid DIN EVSE ID: ${raw}`, diagnoseDin(raw));
|
|
781
|
+
return createDin(asciiUpperCase(match[1]), asciiUpperCase(match[2]), asciiUpperCase(match[3]));
|
|
520
782
|
}
|
|
521
783
|
toString() {
|
|
522
|
-
return `${this.countryCode
|
|
784
|
+
return `${this.countryCode}*${this.operatorId}*${this.powerOutletId}`;
|
|
523
785
|
}
|
|
524
786
|
};
|
|
525
787
|
function validateIso(countryCode, operatorId, powerOutletId) {
|
|
526
788
|
if (!ISO_COUNTRY_REGEX.test(countryCode)) return {
|
|
527
789
|
priority: 1,
|
|
528
|
-
description: "Invalid countryCode for ISO or DIN format"
|
|
790
|
+
description: "Invalid countryCode for ISO or DIN format",
|
|
791
|
+
reason: "country-code"
|
|
529
792
|
};
|
|
530
793
|
if (!ISO_OPERATOR_REGEX.test(operatorId)) return {
|
|
531
794
|
priority: 2,
|
|
532
|
-
description: "Invalid operatorId for ISO format"
|
|
795
|
+
description: "Invalid operatorId for ISO format",
|
|
796
|
+
reason: "operator-id"
|
|
533
797
|
};
|
|
534
798
|
if (!ISO_POWER_OUTLET_REGEX.test(powerOutletId)) return {
|
|
535
799
|
priority: 3,
|
|
536
|
-
description: "Invalid powerOutletId for ISO format"
|
|
800
|
+
description: "Invalid powerOutletId for ISO format",
|
|
801
|
+
reason: "power-outlet-id"
|
|
537
802
|
};
|
|
538
803
|
return null;
|
|
539
804
|
}
|
|
540
805
|
function validateDin(countryCode, operatorId, powerOutletId) {
|
|
541
806
|
if (!DIN_COUNTRY_REGEX.test(countryCode)) return {
|
|
542
807
|
priority: 1,
|
|
543
|
-
description: "Invalid countryCode for ISO or DIN format"
|
|
808
|
+
description: "Invalid countryCode for ISO or DIN format",
|
|
809
|
+
reason: "phone-country-code"
|
|
544
810
|
};
|
|
545
811
|
if (!DIN_OPERATOR_REGEX.test(operatorId)) return {
|
|
546
812
|
priority: 2,
|
|
547
|
-
description: "Invalid operatorId for DIN format"
|
|
813
|
+
description: "Invalid operatorId for DIN format",
|
|
814
|
+
reason: "operator-id"
|
|
548
815
|
};
|
|
549
816
|
if (!DIN_POWER_OUTLET_REGEX.test(powerOutletId)) return {
|
|
550
817
|
priority: 3,
|
|
551
|
-
description: "Invalid powerOutletId for DIN format"
|
|
818
|
+
description: "Invalid powerOutletId for DIN format",
|
|
819
|
+
reason: "power-outlet-id"
|
|
552
820
|
};
|
|
553
821
|
return null;
|
|
554
822
|
}
|
|
823
|
+
/**
|
|
824
|
+
* Names the failing segment of an ISO EVSE id the full pattern rejected. Only the starred form
|
|
825
|
+
* `country*operator*Eoutlet` splits reliably; compact or mixed forms are `format`.
|
|
826
|
+
*/
|
|
827
|
+
function diagnoseIso(raw) {
|
|
828
|
+
const [country, operator, ...rest] = raw.split("*");
|
|
829
|
+
if (operator === void 0 || rest.length === 0) return "format";
|
|
830
|
+
if (ISO_COUNTRY_AND_OPERATOR_REGEX.test(country ?? "")) return "format";
|
|
831
|
+
if (operator.length > 3 && ISO_OPERATOR_AND_ID_TYPE_REGEX.test(operator)) return "format";
|
|
832
|
+
if (!CountryCode.isValid(country ?? "")) return "country-code";
|
|
833
|
+
if (!ISO_OPERATOR_REGEX.test(operator)) return "operator-id";
|
|
834
|
+
const typedOutlet = rest.join("*");
|
|
835
|
+
if (!typedOutlet.startsWith("E") && !typedOutlet.startsWith("e")) return "id-type";
|
|
836
|
+
if (!ISO_POWER_OUTLET_REGEX.test(typedOutlet.slice(1))) return "power-outlet-id";
|
|
837
|
+
return "format";
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
* Names the failing segment of a DIN EVSE id, whose separators are mandatory. A digit run too
|
|
841
|
+
* long for its segment may be a missing separator (`+49810*000*1`), so it is `format`.
|
|
842
|
+
*/
|
|
843
|
+
function diagnoseDin(raw) {
|
|
844
|
+
const [country, operator, ...rest] = raw.split("*");
|
|
845
|
+
if (operator === void 0 || rest.length === 0) return "format";
|
|
846
|
+
if (DIN_COUNTRY_AND_OPERATOR_REGEX.test(country ?? "") || DIN_OVERLONG_OPERATOR_REGEX.test(operator)) return "format";
|
|
847
|
+
const error = validateDin(country ?? "", operator, rest.join("*"));
|
|
848
|
+
return error === null ? "format" : error.reason;
|
|
849
|
+
}
|
|
555
850
|
function createIso(countryCode, operatorId, powerOutletId) {
|
|
556
|
-
|
|
851
|
+
const country = CountryCode.from(countryCode);
|
|
852
|
+
const operator = OperatorIdIso.from(operatorId);
|
|
853
|
+
if (!ISO_POWER_OUTLET_REGEX.test(powerOutletId)) throw new ValidationError("Invalid powerOutletId for ISO format", "power-outlet-id");
|
|
854
|
+
return new EvseIdIso(country, operator, powerOutletId);
|
|
557
855
|
}
|
|
558
856
|
function createDin(countryCode, operatorId, powerOutletId) {
|
|
559
857
|
const countryWithPlus = countryCode.startsWith("+") ? countryCode : `+${countryCode}`;
|
|
560
|
-
|
|
858
|
+
const country = PhoneCountryCode.from(countryWithPlus);
|
|
859
|
+
const operator = OperatorIdDin.from(operatorId);
|
|
860
|
+
if (!DIN_POWER_OUTLET_REGEX.test(powerOutletId)) throw new ValidationError("Invalid powerOutletId for DIN format", "power-outlet-id");
|
|
861
|
+
return new EvseIdDin(country, operator, powerOutletId);
|
|
561
862
|
}
|
|
562
863
|
const EvseId = {
|
|
563
864
|
fromParts(countryCode, operatorId, powerOutletId) {
|
|
564
|
-
const normalizedCountry = countryCode
|
|
565
|
-
const normalizedOperator = operatorId
|
|
566
|
-
const normalizedPowerOutlet = powerOutletId
|
|
865
|
+
const normalizedCountry = asciiUpperCase(countryCode);
|
|
866
|
+
const normalizedOperator = asciiUpperCase(operatorId);
|
|
867
|
+
const normalizedPowerOutlet = asciiUpperCase(powerOutletId);
|
|
567
868
|
const isoError = validateIso(normalizedCountry, normalizedOperator, normalizedPowerOutlet);
|
|
568
869
|
if (isoError === null) return createIso(normalizedCountry, normalizedOperator, normalizedPowerOutlet);
|
|
569
870
|
const dinError = validateDin(normalizedCountry, normalizedOperator, normalizedPowerOutlet);
|
|
570
871
|
if (dinError === null) return createDin(normalizedCountry, normalizedOperator, normalizedPowerOutlet);
|
|
571
|
-
|
|
572
|
-
throw new
|
|
872
|
+
const error = isoError.priority >= dinError.priority ? isoError : dinError;
|
|
873
|
+
throw new ValidationError(error.description, error.reason);
|
|
874
|
+
},
|
|
875
|
+
tryParse(raw) {
|
|
876
|
+
const iso = EvseIdIso.tryParse(raw);
|
|
877
|
+
if (iso.ok) return iso;
|
|
878
|
+
const din = EvseIdDin.tryParse(raw);
|
|
879
|
+
if (din.ok) return din;
|
|
880
|
+
const reason = /^[A-Za-z]/.test(raw) ? iso.reason : /^[+0-9]/.test(raw) ? din.reason : "format";
|
|
881
|
+
return failure(`Invalid EVSE ID: ${raw} (ISO: ${iso.error}; DIN: ${din.error})`, reason);
|
|
573
882
|
},
|
|
574
883
|
parse(raw) {
|
|
575
|
-
|
|
576
|
-
if (iso !== null) return iso;
|
|
577
|
-
return EvseIdDin.parse(raw);
|
|
884
|
+
return valueOrNull(EvseId.tryParse(raw));
|
|
578
885
|
},
|
|
579
886
|
parseStrict(raw) {
|
|
580
|
-
const
|
|
581
|
-
if (
|
|
582
|
-
return
|
|
887
|
+
const result = EvseId.tryParse(raw);
|
|
888
|
+
if (!result.ok) throw new ValidationError(result.error, result.reason);
|
|
889
|
+
return result.value;
|
|
583
890
|
}
|
|
584
891
|
};
|
|
585
|
-
|
|
586
|
-
//#region src/parsers.ts
|
|
892
|
+
// src/parsers.ts
|
|
587
893
|
const MobilityIdParsers = {
|
|
588
894
|
parseCountryCode(raw) {
|
|
589
895
|
return CountryCode.parse(raw);
|
|
@@ -620,9 +926,44 @@ const MobilityIdParsers = {
|
|
|
620
926
|
},
|
|
621
927
|
parseEvseIdDin(raw) {
|
|
622
928
|
return EvseIdDin.parse(raw);
|
|
929
|
+
},
|
|
930
|
+
tryParseCountryCode(raw) {
|
|
931
|
+
return CountryCode.tryParse(raw);
|
|
932
|
+
},
|
|
933
|
+
tryParsePhoneCountryCode(raw) {
|
|
934
|
+
return PhoneCountryCode.tryParse(raw);
|
|
935
|
+
},
|
|
936
|
+
tryParseProviderId(raw) {
|
|
937
|
+
return ProviderId.tryParse(raw);
|
|
938
|
+
},
|
|
939
|
+
tryParseOperatorIdIso(raw) {
|
|
940
|
+
return OperatorIdIso.tryParse(raw);
|
|
941
|
+
},
|
|
942
|
+
tryParseOperatorIdDin(raw) {
|
|
943
|
+
return OperatorIdDin.tryParse(raw);
|
|
944
|
+
},
|
|
945
|
+
tryParsePartyId(raw) {
|
|
946
|
+
return PartyId.tryParse(raw);
|
|
947
|
+
},
|
|
948
|
+
tryParseContractIdIso(raw) {
|
|
949
|
+
return ContractId.tryParse(ContractIdStandards.ISO, raw);
|
|
950
|
+
},
|
|
951
|
+
tryParseContractIdDin(raw) {
|
|
952
|
+
return ContractId.tryParse(ContractIdStandards.DIN, raw);
|
|
953
|
+
},
|
|
954
|
+
tryParseContractIdEmi3(raw) {
|
|
955
|
+
return ContractId.tryParse(ContractIdStandards.EMI3, raw);
|
|
956
|
+
},
|
|
957
|
+
tryParseEvseId(raw) {
|
|
958
|
+
return EvseId.tryParse(raw);
|
|
959
|
+
},
|
|
960
|
+
tryParseEvseIdIso(raw) {
|
|
961
|
+
return EvseIdIso.tryParse(raw);
|
|
962
|
+
},
|
|
963
|
+
tryParseEvseIdDin(raw) {
|
|
964
|
+
return EvseIdDin.tryParse(raw);
|
|
623
965
|
}
|
|
624
966
|
};
|
|
625
|
-
|
|
626
|
-
export { ContractId, ContractIdStandards, CountryCode, EvseId, EvseIdDin, EvseIdIso, MobilityIdParsers, OperatorIdDin, OperatorIdIso, PartyId, PhoneCountryCode, ProviderId, checkDigitDin, checkDigitIso };
|
|
967
|
+
export { ContractId, ContractIdStandards, CountryCode, EvseId, EvseIdDin, EvseIdIso, ISO_3166_ALPHA2, MobilityIdParsers, OperatorIdDin, OperatorIdIso, PartyId, PhoneCountryCode, ProviderId, SeparatorPolicies, ValidationError, checkDigitDin, checkDigitIso };
|
|
627
968
|
|
|
628
969
|
//# sourceMappingURL=index.js.map
|