typepki-strconv 0.7.0 → 0.8.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typepki-strconv",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "author": "Kenji Urushima <kenji.urushima@gmail.com>",
5
5
  "description": "TypeScript string converters and utilities sub module for TypePKI library (alpha version)",
6
6
  "homepage": "https://kjur.github.io/typepki-strconv",
@@ -67,7 +67,7 @@
67
67
  "jest": "^29.7.0",
68
68
  "run-z": "^2.0.0",
69
69
  "typedoc": "^0.25.13",
70
- "typepki-strconv": "^0.2.0",
70
+ "typepki-strconv": "^0.8.0",
71
71
  "typescript": "^5.4.5"
72
72
  }
73
73
  }
package/src/conv.mts CHANGED
@@ -360,7 +360,7 @@ export function namearraytobinstr(
360
360
  */
361
361
  export function binstrtonamearray(
362
362
  binstr: string,
363
- namedb: Record<string, number>
363
+ namedb: Record<string, number>,
364
364
  ): string[] {
365
365
  const aKey: string[] = Object.keys(namedb);
366
366
  //console.log("aKey=", aKey);
@@ -368,12 +368,14 @@ export function binstrtonamearray(
368
368
  //console.log("aVal=", aVal);
369
369
 
370
370
  const namedbrev: Record<string, string> = {};
371
- Object.keys(namedb).map((k) => { namedbrev[namedb[k]] = k; });
371
+ Object.keys(namedb).map((k) => {
372
+ namedbrev[namedb[k]] = k;
373
+ });
372
374
  //console.log("namedbrev=", JSON.stringify(namedbrev, null, 2));
373
375
 
374
- let aResult: string[] = [];
375
- binstr.split('').map((k, idx) => {
376
- if (k == '1') aResult.push(namedbrev[idx.toString()]);
376
+ const aResult: string[] = [];
377
+ binstr.split("").map((k, idx) => {
378
+ if (k === "1") aResult.push(namedbrev[idx.toString()]);
377
379
  });
378
380
 
379
381
  return aResult;
@@ -1,6 +1,11 @@
1
1
  import { describe, expect, test } from "bun:test";
2
2
 
3
- import { encodeURIComponentAll, hextouricmp, uricmptohex, hextoutf8 } from "./index.mts";
3
+ import {
4
+ encodeURIComponentAll,
5
+ hextouricmp,
6
+ hextoutf8,
7
+ uricmptohex,
8
+ } from "./index.mts";
4
9
 
5
10
  test("uricmptohex", () => {
6
11
  expect(uricmptohex("%67%68")).toBe("6768");
package/src/conv_zulu.mts CHANGED
@@ -206,3 +206,37 @@ export function timetogen(s: string): string {
206
206
  }
207
207
  return s;
208
208
  }
209
+
210
+ /**
211
+ * convert GeneralizedTime or UTCTime string to ISO8601 time string
212
+ * @param s - GeneralizedTime or UTCTime string (ex. 20170412235959Z)
213
+ * @return ISO8601 time string (ex. 2020-07-14T13:03:42Z)
214
+ * @since 0.8.0
215
+ *
216
+ * @example
217
+ * zulutoiso8601( "071231235959Z") -> "2007-12-31T23:59:59Z"
218
+ * zulutoiso8601( "971231235959Z") -> "1997-12-31T23:59:59Z"
219
+ * zulutoiso8601("20071231235959Z") -> "2007-12-31T23:59:59Z"
220
+ * zulutoiso8601( "0071231235959Z") -> raise error
221
+ * zulutoiso8601( "aaaaaaaaaa") -> raise error
222
+ */
223
+ export function zulutoiso8601(s: string): string {
224
+ const m = s.match(/^(\d{2,4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/);
225
+ if (!m) {
226
+ throw new Error(
227
+ "Invalid format: expected YYYYMMDDHHmmssZ or YYYYMMDDHHmmssZ",
228
+ );
229
+ }
230
+
231
+ const [, y, mm, dd, hh, min, ss] = m;
232
+ let y2: string = y;
233
+ if (y2.length === 2) {
234
+ if (parseInt(y2.slice(0, 1)) < 5) {
235
+ y2 = `20${y2}`;
236
+ } else {
237
+ y2 = `19${y2}`;
238
+ }
239
+ }
240
+
241
+ return `${y2}-${mm}-${dd}T${hh}:${min}:${ss}Z`;
242
+ }
@@ -4,6 +4,7 @@ import {
4
4
  datetozulu,
5
5
  msectozulu,
6
6
  zulutodate,
7
+ zulutoiso8601,
7
8
  zulutomsec,
8
9
  zulutosec,
9
10
  } from "./index.mts";
@@ -47,3 +48,17 @@ test("datetozulu", () => {
47
48
  expect(datetozulu(d, true)).toBe("170520235959Z");
48
49
  expect(datetozulu(d, false, true)).toBe("20170520235959.67Z");
49
50
  });
51
+
52
+ test("zulutoiso8601", () => {
53
+ expect(zulutoiso8601("071231235959Z")).toBe("2007-12-31T23:59:59Z");
54
+ expect(zulutoiso8601("471231235959Z")).toBe("2047-12-31T23:59:59Z");
55
+ expect(zulutoiso8601("501231235959Z")).toBe("1950-12-31T23:59:59Z");
56
+ expect(zulutoiso8601("971231235959Z")).toBe("1997-12-31T23:59:59Z");
57
+ expect(zulutoiso8601("20071231235959Z")).toBe("2007-12-31T23:59:59Z");
58
+ expect(() => {
59
+ zulutoiso8601(" 0071231235959Z");
60
+ }).toThrow(/Invalid format/);
61
+ expect(() => {
62
+ zulutoiso8601("aaaaaaa");
63
+ }).toThrow(/Invalid format/);
64
+ });
package/src/hexutil.mts CHANGED
@@ -7,5 +7,5 @@
7
7
  * hexpad("ab3c") -> "ab3c"
8
8
  */
9
9
  export function hexpad(s: string): string {
10
- return (s.length % 2 === 1) ? `0${s}` : s;
10
+ return s.length % 2 === 1 ? `0${s}` : s;
11
11
  }
package/src/index.mts CHANGED
@@ -1,4 +1,4 @@
1
- const VERSION: string = "typepki-strconv 0.7.0 kjur.github.io/typepki-strconv";
1
+ const VERSION: string = "typepki-strconv 0.8.0 kjur.github.io/typepki-strconv";
2
2
  export { VERSION };
3
3
 
4
4
  import { isBase64, isBase64URL, ishex } from "./checker.mts";
@@ -79,10 +79,19 @@ import {
79
79
  msectozulu,
80
80
  timetogen,
81
81
  zulutodate,
82
+ zulutoiso8601,
82
83
  zulutomsec,
83
84
  zulutosec,
84
85
  } from "./conv_zulu.mts";
85
- export { zulutomsec, msectozulu, zulutosec, zulutodate, datetozulu, timetogen };
86
+ export {
87
+ zulutomsec,
88
+ msectozulu,
89
+ zulutosec,
90
+ zulutodate,
91
+ datetozulu,
92
+ timetogen,
93
+ zulutoiso8601,
94
+ };
86
95
 
87
96
  import {
88
97
  encodeURIComponentAll,