affinirum 1.5.0 → 1.5.2

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.
Files changed (54) hide show
  1. package/README.md +30 -14
  2. package/dst/Affinirum.d.ts +33 -26
  3. package/dst/Affinirum.js +58 -44
  4. package/dst/ParserState.js +23 -79
  5. package/dst/Scope.d.ts +16 -0
  6. package/dst/Scope.js +49 -0
  7. package/dst/atom/ArrayAtom.js +4 -1
  8. package/dst/cjs/Affinirum.js +57 -43
  9. package/dst/cjs/ParserState.js +22 -78
  10. package/dst/cjs/Scope.js +53 -0
  11. package/dst/cjs/atom/ArrayAtom.js +4 -1
  12. package/dst/cjs/native/Aggregable.js +1 -1
  13. package/dst/cjs/native/Boolean.js +3 -1
  14. package/dst/cjs/native/Buffer.js +32 -3
  15. package/dst/cjs/native/Enumerable.js +5 -3
  16. package/dst/cjs/native/Float.js +17 -5
  17. package/dst/cjs/native/Integer.js +41 -13
  18. package/dst/cjs/native/Iterable.js +6 -4
  19. package/dst/cjs/native/Number.js +45 -1
  20. package/dst/cjs/native/String.js +68 -22
  21. package/dst/cjs/native/Timestamp.js +74 -17
  22. package/dst/cjs/native/Unknown.js +1 -1
  23. package/dst/cjs/native/notation/AN.js +152 -11
  24. package/dst/cjs/native/notation/IP.js +1 -1
  25. package/dst/cjs/native/notation/JSON.js +1 -1
  26. package/dst/cjs/node/BlockNode.js +5 -4
  27. package/dst/cjs/node/CallNode.js +7 -5
  28. package/dst/cjs/node/SwitchNode.js +7 -9
  29. package/dst/native/Aggregable.js +1 -1
  30. package/dst/native/Boolean.js +3 -1
  31. package/dst/native/Buffer.d.ts +2 -0
  32. package/dst/native/Buffer.js +29 -2
  33. package/dst/native/Enumerable.js +5 -3
  34. package/dst/native/Float.js +17 -5
  35. package/dst/native/Integer.js +41 -13
  36. package/dst/native/Iterable.js +6 -4
  37. package/dst/native/Number.d.ts +4 -0
  38. package/dst/native/Number.js +40 -0
  39. package/dst/native/String.d.ts +2 -7
  40. package/dst/native/String.js +65 -14
  41. package/dst/native/Timestamp.d.ts +4 -2
  42. package/dst/native/Timestamp.js +71 -16
  43. package/dst/native/Unknown.js +1 -1
  44. package/dst/native/notation/AN.d.ts +1 -0
  45. package/dst/native/notation/AN.js +152 -12
  46. package/dst/native/notation/IP.js +1 -1
  47. package/dst/native/notation/JSON.js +1 -1
  48. package/dst/node/BlockNode.js +5 -4
  49. package/dst/node/CallNode.js +7 -5
  50. package/dst/node/SwitchNode.js +7 -9
  51. package/package.json +1 -1
  52. package/dst/StaticScope.d.ts +0 -13
  53. package/dst/StaticScope.js +0 -44
  54. package/dst/cjs/StaticScope.js +0 -48
@@ -4,6 +4,33 @@ export const EmptyBuffer = new ArrayBuffer(0);
4
4
  const ZERO = "0".charCodeAt(0), NINE = "9".charCodeAt(0);
5
5
  const UPPER_A = "A".charCodeAt(0), UPPER_F = "F".charCodeAt(0);
6
6
  const LOWER_A = "a".charCodeAt(0), LOWER_F = "f".charCodeAt(0);
7
+ export const formatLiteralBuffer = (value) => `\`${formatBuffer(value)}\``;
8
+ export const parseLiteralBuffer = (value, offset = 0) => {
9
+ const q = value.charCodeAt(offset);
10
+ for (let i = offset + 1, bytes = [], digits = [0, 0], ix = 0; i < value.length; ++i) {
11
+ const code = value.charCodeAt(i);
12
+ if (code === q) {
13
+ if (ix === 1) {
14
+ bytes.push(digits[0]);
15
+ }
16
+ return [new Uint8Array(bytes).buffer, i + 1];
17
+ }
18
+ if (code >= ZERO && code <= NINE) {
19
+ digits[ix++] = code - ZERO;
20
+ }
21
+ else if (code >= UPPER_A && code <= UPPER_F) {
22
+ digits[ix++] = code - UPPER_A + 10;
23
+ }
24
+ else if (code >= LOWER_A && code <= LOWER_F) {
25
+ digits[ix++] = code - LOWER_A + 10;
26
+ }
27
+ if (ix > 1) {
28
+ bytes.push(digits[0] << 4 | digits[1]);
29
+ ix = 0;
30
+ }
31
+ }
32
+ throw new Error(`missing closing buffer quotation mark ${String.fromCharCode(q)}`);
33
+ };
7
34
  export const parseBoundedInteger = (input, base, min, max) => {
8
35
  if (input.length === 0) {
9
36
  throw new Error(`Invalid IP component: ${input}`);
@@ -22,12 +49,12 @@ export const parseBoundedInteger = (input, base, min, max) => {
22
49
  digit = code - LOWER_A + 10;
23
50
  }
24
51
  else {
25
- throw new Error(`Invalid IP component: ${input}`);
52
+ throw new Error(`Invalid character: ${input.charAt(i)}`);
26
53
  }
27
54
  value = value * base + digit;
28
55
  }
29
56
  if (value < min || value > max) {
30
- throw new Error(`Invalid IP component: ${input}`);
57
+ throw new Error(`Invalid integer: ${value}`);
31
58
  }
32
59
  return value;
33
60
  };
@@ -1,13 +1,15 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
3
  import { aggregate } from "./Aggregable.js";
4
- const typeEnumerableArray = Type.arrayType([Type.Enumerable]);
4
+ const typeBufferArray = Type.arrayType([Type.Buffer]);
5
+ const typeStringArray = Type.arrayType([Type.String]);
6
+ const typeArrayArray = Type.arrayType([Type.Array]);
5
7
  export const funcSlice = new Constant((value, start = 0n, end) => value == null
6
8
  ? undefined
7
9
  : value.slice(Number(start), end == null ? undefined : Number(end)), Type.union(Type.functionType(Type.Buffer, [Type.Buffer, Type.OptionalInteger, Type.OptionalInteger]), Type.functionType(Type.String, [Type.String, Type.OptionalInteger, Type.OptionalInteger]), Type.functionType(Type.Array, [Type.Array, Type.OptionalInteger, Type.OptionalInteger])));
8
10
  export const funcSplice = new Constant((value, start, remove, inject) => value == null
9
11
  ? undefined
10
- : aggregate(inject.reduce((acc, val) => aggregate(acc, val), value.slice(0, Number(start))), value.slice(Number(start + remove))), Type.union(Type.functionType(Type.Buffer, [Type.Buffer, Type.Integer, Type.Integer, typeEnumerableArray], true), Type.functionType(Type.String, [Type.String, Type.Integer, Type.Integer, typeEnumerableArray], true), Type.functionType(Type.Array, [Type.Array, Type.Integer, Type.Integer, typeEnumerableArray], true)));
12
+ : aggregate(inject.reduce((acc, val) => aggregate(acc, val), value.slice(0, Number(start))), value.slice(Number(start + remove))), Type.union(Type.functionType(Type.Buffer, [Type.Buffer, Type.Integer, Type.Integer, typeBufferArray], true), Type.functionType(Type.String, [Type.String, Type.Integer, Type.Integer, typeStringArray], true), Type.functionType(Type.Array, [Type.Array, Type.Integer, Type.Integer, typeArrayArray], true)));
11
13
  export const funcInject = new Constant((value, start, inject) => value == null
12
14
  ? undefined
13
- : aggregate(inject.reduce((acc, val) => aggregate(acc, val), value.slice(0, Number(start))), value.slice(Number(start))), Type.union(Type.functionType(Type.Buffer, [Type.Buffer, Type.Integer, typeEnumerableArray], true), Type.functionType(Type.String, [Type.String, Type.Integer, typeEnumerableArray], true), Type.functionType(Type.Array, [Type.Array, Type.Integer, typeEnumerableArray], true)));
15
+ : aggregate(inject.reduce((acc, val) => aggregate(acc, val), value.slice(0, Number(start))), value.slice(Number(start))), Type.union(Type.functionType(Type.Buffer, [Type.Buffer, Type.Integer, typeBufferArray], true), Type.functionType(Type.String, [Type.String, Type.Integer, typeStringArray], true), Type.functionType(Type.Array, [Type.Array, Type.Integer, typeArrayArray], true)));
@@ -30,12 +30,24 @@ const decodeFloat = (value, encoding = "f64", byteOffset) => {
30
30
  if (value == null) {
31
31
  return undefined;
32
32
  }
33
- const dv = new DataView(value, byteOffset);
33
+ const offset = byteOffset ?? 0;
34
+ if (offset < 0) {
35
+ return undefined;
36
+ }
37
+ const dv = new DataView(value);
34
38
  switch (encoding) {
35
- case "f32": return dv.getFloat32(0);
36
- case "f32le": return dv.getFloat32(0, true);
37
- case "f64": return dv.getFloat64(0);
38
- case "f64le": return dv.getFloat64(0, true);
39
+ case "f32": return value.byteLength < offset + 4
40
+ ? undefined
41
+ : dv.getFloat32(offset);
42
+ case "f32le": return value.byteLength < offset + 4
43
+ ? undefined
44
+ : dv.getFloat32(offset, true);
45
+ case "f64": return value.byteLength < offset + 8
46
+ ? undefined
47
+ : dv.getFloat64(offset);
48
+ case "f64le": return value.byteLength < offset + 8
49
+ ? undefined
50
+ : dv.getFloat64(offset, true);
39
51
  default: throw new Error(`${encoding} encoding not supported`);
40
52
  }
41
53
  };
@@ -67,20 +67,48 @@ const decodeInteger = (value, encoding = "i64", byteOffset) => {
67
67
  if (value == null) {
68
68
  return undefined;
69
69
  }
70
- const dv = new DataView(value, byteOffset == null ? undefined : Number(byteOffset));
70
+ const offset = byteOffset ?? 0;
71
+ if (offset < 0) {
72
+ return undefined;
73
+ }
74
+ const dv = new DataView(value);
71
75
  switch (encoding) {
72
- case "i8": return BigInt(dv.getInt8(0));
73
- case "i16": return BigInt(dv.getInt16(0));
74
- case "i16le": return BigInt(dv.getInt16(0, true));
75
- case "i32": return BigInt(dv.getInt32(0));
76
- case "i32le": return BigInt(dv.getInt32(0, true));
77
- case "i64": return dv.getBigInt64(0);
78
- case "i64le": return dv.getBigInt64(0, true);
79
- case "n8": return BigInt(dv.getUint8(0));
80
- case "n16": return BigInt(dv.getUint16(0));
81
- case "n16le": return BigInt(dv.getUint16(0, true));
82
- case "n32": return BigInt(dv.getUint32(0));
83
- case "n32le": return BigInt(dv.getUint32(0, true));
76
+ case "i8": return value.byteLength < offset + 1
77
+ ? undefined
78
+ : BigInt(dv.getInt8(offset));
79
+ case "i16": return value.byteLength < offset + 2
80
+ ? undefined
81
+ : BigInt(dv.getInt16(offset));
82
+ case "i16le": return value.byteLength < offset + 2
83
+ ? undefined
84
+ : BigInt(dv.getInt16(offset, true));
85
+ case "i32": return value.byteLength < offset + 4
86
+ ? undefined
87
+ : BigInt(dv.getInt32(offset));
88
+ case "i32le": return value.byteLength < offset + 4
89
+ ? undefined
90
+ : BigInt(dv.getInt32(offset, true));
91
+ case "i64": return value.byteLength < offset + 8
92
+ ? undefined
93
+ : dv.getBigInt64(offset);
94
+ case "i64le": return value.byteLength < offset + 8
95
+ ? undefined
96
+ : dv.getBigInt64(offset, true);
97
+ case "n8": return value.byteLength < offset + 1
98
+ ? undefined
99
+ : BigInt(dv.getUint8(offset));
100
+ case "n16": return value.byteLength < offset + 2
101
+ ? undefined
102
+ : BigInt(dv.getUint16(offset));
103
+ case "n16le": return value.byteLength < offset + 2
104
+ ? undefined
105
+ : BigInt(dv.getUint16(offset, true));
106
+ case "n32": return value.byteLength < offset + 4
107
+ ? undefined
108
+ : BigInt(dv.getUint32(offset));
109
+ case "n32le": return value.byteLength < offset + 4
110
+ ? undefined
111
+ : BigInt(dv.getUint32(offset, true));
84
112
  default: throw new Error(`${encoding} encoding not supported`);
85
113
  }
86
114
  };
@@ -24,8 +24,9 @@ export const funcAt = new Constant((value, index) => {
24
24
  return undefined;
25
25
  }
26
26
  else if (Array.isArray(value)) {
27
- const ix = Number(index);
28
- return value[ix < 0 ? value.length + ix : ix];
27
+ const num = Number(index);
28
+ const ix = num < 0 ? value.length + num : num;
29
+ return ix >= 0 && ix < value.length ? value[ix] : undefined;
29
30
  }
30
31
  const key = String(index);
31
32
  return Object.hasOwn(value, key) ? value[key] : undefined;
@@ -35,8 +36,9 @@ export const funcHas = new Constant((value, index) => {
35
36
  return false;
36
37
  }
37
38
  else if (Array.isArray(value)) {
38
- const ix = Number(index);
39
- return ix < value.length;
39
+ const num = Number(index);
40
+ const ix = num < 0 ? value.length + num : num;
41
+ return ix >= 0 && ix < value.length;
40
42
  }
41
43
  return Object.hasOwn(value, String(index));
42
44
  }, Type.union(Type.functionType(Type.Boolean, [Type.Array, Type.Integer]), Type.functionType(Type.Boolean, [Type.Object, Type.String])));
@@ -1,4 +1,6 @@
1
1
  import { Constant } from "../Constant.js";
2
+ export declare const isSignSymbol: (c: string) => c is "+" | "-";
3
+ export declare const isNumericSymbol: (c: string) => boolean;
2
4
  export interface NumberFormatting {
3
5
  min_integer_digits?: number;
4
6
  min_fraction_digits?: number;
@@ -7,6 +9,8 @@ export interface NumberFormatting {
7
9
  max_significant_digits?: number;
8
10
  notation?: "standard" | "scientific";
9
11
  }
12
+ export declare const formatLiteralNumber: (value: bigint | number) => string;
13
+ export declare const parseLiteralNumber: (value: string, offset?: number) => readonly [number | bigint, number];
10
14
  export declare const formatNumber: (value: bigint | number, formatting?: string) => string;
11
15
  export declare const funcGreaterThan: Constant;
12
16
  export declare const funcLessThan: Constant;
@@ -1,6 +1,46 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
3
  import { boundInteger } from "./Integer.js";
4
+ export const isSignSymbol = (c) => c === "+" || c === "-";
5
+ export const isNumericSymbol = (c) => c >= "0" && c <= "9";
6
+ export const formatLiteralNumber = (value) => {
7
+ return typeof value === "number" && Number.isInteger(value)
8
+ ? `${value.toString()}.0`
9
+ : value.toString();
10
+ };
11
+ export const parseLiteralNumber = (value, offset = 0) => {
12
+ let i = offset;
13
+ let integer = true;
14
+ while (isNumericSymbol(value.charAt(i))) {
15
+ ++i;
16
+ }
17
+ if (value.charAt(i) === ".") {
18
+ ++i;
19
+ if (isNumericSymbol(value.charAt(i))) {
20
+ ++i;
21
+ while (isNumericSymbol(value.charAt(i))) {
22
+ ++i;
23
+ }
24
+ integer = false;
25
+ }
26
+ else {
27
+ --i;
28
+ }
29
+ }
30
+ if (value.charAt(i) === "e") {
31
+ ++i;
32
+ if (isNumericSymbol(value.charAt(i))
33
+ || isSignSymbol(value.charAt(i))) {
34
+ ++i;
35
+ while (isNumericSymbol(value.charAt(i))) {
36
+ ++i;
37
+ }
38
+ }
39
+ integer = false;
40
+ }
41
+ const str = value.slice(offset, i);
42
+ return [integer ? boundInteger(BigInt(str)) : parseFloat(str), i];
43
+ };
4
44
  export const formatNumber = (value, formatting) => {
5
45
  if (formatting) {
6
46
  const nformat = JSON.parse(`{${formatting}}`);
@@ -1,16 +1,11 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  export type StringEncoding = "utf8" | "sbcs" | "ucs2" | "ucs2le";
3
- export declare const isSignSymbol: (c: string) => c is "+" | "-";
4
- export declare const isNumericSymbol: (c: string) => boolean;
5
3
  export declare const isAlphanumericSymbol: (c: string) => boolean;
6
4
  export declare const isTokenStartSymbol: (c: string) => boolean;
7
5
  export declare const isTokenSymbol: (c: string) => boolean;
8
- export declare const isDateSeparatorSymbol: (c: string) => c is "-";
9
- export declare const isTimeSeparatorSymbol: (c: string) => c is ":";
10
- export declare const isDateTimeSeparatorSymbol: (c: string) => c is " " | "T" | "@";
11
- export declare const isDateSymbol: (c: string) => boolean;
12
- export declare const isTimeSymbol: (c: string) => boolean;
13
6
  export declare const isHexadecimalSymbol: (c: string) => boolean;
7
+ export declare const formatLiteralString: (value: string) => string;
8
+ export declare const parseLiteralString: (value: string, offset?: number) => readonly [string, number];
14
9
  export declare const replaceWith: (value?: string, replacement?: string, ...search: string[]) => string | undefined;
15
10
  export declare const equateStrings: (value1?: string, value2?: string, ignoreCaseSpaceEtc?: boolean) => boolean;
16
11
  export declare const containsString: (value?: string, search?: string, startPos?: number, ignoreCaseSpaceEtc?: boolean) => boolean;
@@ -1,17 +1,63 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
3
  import { createInteger } from "./Integer.js";
4
- export const isSignSymbol = (c) => c === "+" || c === "-";
5
- export const isNumericSymbol = (c) => c >= "0" && c <= "9";
6
4
  export const isAlphanumericSymbol = (c) => c >= "a" && c <= "z" || c >= "0" && c <= "9";
7
5
  export const isTokenStartSymbol = (c) => c >= "a" && c <= "z" || c >= "A" && c <= "Z" || c === "_" || c === "#" || c === "$";
8
- export const isTokenSymbol = (c) => isTokenStartSymbol(c) || isNumericSymbol(c);
9
- export const isDateSeparatorSymbol = (c) => c === "-";
10
- export const isTimeSeparatorSymbol = (c) => c === ":";
11
- export const isDateTimeSeparatorSymbol = (c) => c === "T" || c === " " || c === "@";
12
- export const isDateSymbol = (c) => isNumericSymbol(c) || isDateSeparatorSymbol(c);
13
- export const isTimeSymbol = (c) => isNumericSymbol(c) || isTimeSeparatorSymbol(c);
14
- export const isHexadecimalSymbol = (c) => isNumericSymbol(c) || c >= "a" && c <= "f" || c >= "A" && c <= "F";
6
+ export const isTokenSymbol = (c) => isTokenStartSymbol(c) || c >= "0" && c <= "9";
7
+ export const isHexadecimalSymbol = (c) => c >= "0" && c <= "9" || c >= "a" && c <= "f" || c >= "A" && c <= "F";
8
+ export const formatLiteralString = (value) => {
9
+ let str = "\"";
10
+ for (let i = 0; i < value.length; ++i) {
11
+ const c = value.charAt(i);
12
+ if (c === "\r") {
13
+ str += "\\r";
14
+ }
15
+ else if (c === "\n") {
16
+ str += "\\n";
17
+ }
18
+ else if (c === "\t") {
19
+ str += "\\t";
20
+ }
21
+ else if (c === "\\" || c === "\"") {
22
+ str += `\\${c}`;
23
+ }
24
+ else {
25
+ str += c;
26
+ }
27
+ }
28
+ return str + "\"";
29
+ };
30
+ export const parseLiteralString = (value, offset = 0) => {
31
+ const q = value.charAt(offset);
32
+ for (let i = offset + 1, str = ""; i < value.length; ++i) {
33
+ const c = value.charAt(i);
34
+ if (c === q) {
35
+ return [str, i + 1];
36
+ }
37
+ if (c === "\\") {
38
+ if (++i === value.length) {
39
+ break;
40
+ }
41
+ const nc = value.charAt(i);
42
+ if (nc === "r") {
43
+ str += "\r";
44
+ }
45
+ else if (nc === "n") {
46
+ str += "\n";
47
+ }
48
+ else if (nc === "t") {
49
+ str += "\t";
50
+ }
51
+ else {
52
+ str += nc;
53
+ }
54
+ }
55
+ else {
56
+ str += c;
57
+ }
58
+ }
59
+ throw new Error(`missing closing string quotation mark ${q}`);
60
+ };
15
61
  export const replaceWith = (value, replacement, ...search) => {
16
62
  if (value == null) {
17
63
  return undefined;
@@ -166,7 +212,10 @@ export const encodeString = (value, encoding = "utf8") => {
166
212
  return dv.buffer;
167
213
  };
168
214
  const decodeString = (value, encoding = "utf8", byteOffset, byteLength) => {
169
- if (value == null) {
215
+ if (value == null ||
216
+ byteOffset != null && byteOffset < 0 ||
217
+ byteLength != null && byteLength < 0 ||
218
+ value.byteLength < (byteOffset ?? 0) + (byteLength ?? 0)) {
170
219
  return undefined;
171
220
  }
172
221
  switch (encoding) {
@@ -187,7 +236,9 @@ const decodeString = (value, encoding = "utf8", byteOffset, byteLength) => {
187
236
  const littleEndian = encoding.endsWith("le");
188
237
  let str = "";
189
238
  for (let i = 0; i < dv.byteLength; i += 2) {
190
- str += String.fromCharCode(dv.getUint16(i, littleEndian));
239
+ if (dv.byteLength > i + 1) {
240
+ str += String.fromCharCode(dv.getUint16(i, littleEndian));
241
+ }
191
242
  }
192
243
  return str;
193
244
  };
@@ -216,10 +267,10 @@ const funcAlphanum = new Constant((value) => {
216
267
  if (!value) {
217
268
  return undefined;
218
269
  }
219
- const lowerCase = value.toLowerCase();
270
+ const str = value.toLowerCase();
220
271
  let result = "";
221
- for (let i = 0; i < lowerCase.length; ++i) {
222
- if (isAlphanumericSymbol(value[i])) {
272
+ for (let i = 0; i < str.length; ++i) {
273
+ if (isAlphanumericSymbol(str[i])) {
223
274
  result += value[i];
224
275
  }
225
276
  }
@@ -1,7 +1,9 @@
1
1
  import { Constant } from "../Constant.js";
2
- export type TimestampEncoding = "i64" | "i64le";
2
+ export type TimestampEncoding = "i32" | "i32le" | "i64" | "i64le";
3
+ export declare const formatLiteralTimestamp: (value: Date) => string;
4
+ export declare const parseLiteralTimestamp: (value: string, offset?: number) => readonly [Date, number];
3
5
  export declare const encodeTimestamp: (value: Date, encoding?: TimestampEncoding) => ArrayBuffer;
4
- export declare const decodeTimestamp: (value?: ArrayBuffer, encoding?: TimestampEncoding, byteOffset?: bigint) => Date | undefined;
6
+ export declare const decodeTimestamp: (value?: ArrayBuffer, encoding?: TimestampEncoding, byteOffset?: number) => Date | undefined;
5
7
  export declare const formatTimestamp: (value: Date, template?: string) => string;
6
8
  export declare const funcYear: Constant;
7
9
  export declare const funcMonth: Constant;
@@ -1,31 +1,83 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
3
  import { boundInteger } from "./Integer.js";
4
+ import { isNumericSymbol } from "./Number.js";
5
+ const minuteMilliseconds = 60000;
4
6
  const typeTimestampPart = Type.functionType(Type.Integer, [Type.Timestamp, Type.OptionalBoolean]);
5
7
  const typeTimestampSince = Type.functionType(Type.Float, [Type.Timestamp, Type.Timestamp]);
8
+ const isDateSeparatorSymbol = (c) => c === "-";
9
+ const isTimeSeparatorSymbol = (c) => c === ":";
10
+ const isDateTimeSeparatorSymbol = (c) => c === "T" || c === " " || c === "@";
11
+ const isDateSymbol = (c) => isNumericSymbol(c) || isDateSeparatorSymbol(c);
12
+ const isTimeSymbol = (c) => isNumericSymbol(c) || isTimeSeparatorSymbol(c);
13
+ export const formatLiteralTimestamp = (value) => `@${formatTimestamp(value)}`;
14
+ export const parseLiteralTimestamp = (value, offset = 0) => {
15
+ let i = offset + 1;
16
+ while (isDateSymbol(value.charAt(i))) {
17
+ ++i;
18
+ }
19
+ if (isDateTimeSeparatorSymbol(value.charAt(i))) {
20
+ ++i;
21
+ while (isTimeSymbol(value.charAt(i))) {
22
+ ++i;
23
+ }
24
+ if (value.charAt(i) === "."
25
+ && isNumericSymbol(value.charAt(i + 1))) {
26
+ ++i;
27
+ while (isNumericSymbol(value.charAt(i))) {
28
+ ++i;
29
+ }
30
+ if (value.charAt(i) === "Z") {
31
+ ++i;
32
+ }
33
+ }
34
+ }
35
+ return [new Date(value.slice(offset + 1, i)), i];
36
+ };
6
37
  export const encodeTimestamp = (value, encoding = "i64") => {
38
+ const littleEndian = encoding.endsWith("le");
7
39
  switch (encoding) {
8
- case "i64": break;
9
- case "i64le": break;
40
+ case "i32":
41
+ case "i32le": {
42
+ const dv = new DataView(new Int32Array(1).buffer);
43
+ const timer = value?.getTime() ?? 0;
44
+ dv.setInt32(0, (Number.isNaN(timer) ? 0 : timer) / minuteMilliseconds, littleEndian);
45
+ return dv.buffer;
46
+ }
47
+ case "i64":
48
+ case "i64le": {
49
+ const dv = new DataView(new BigInt64Array(1).buffer);
50
+ const timer = value?.getTime() ?? 0;
51
+ dv.setBigInt64(0, BigInt(Number.isNaN(timer) ? 0 : timer), littleEndian);
52
+ return dv.buffer;
53
+ }
10
54
  default: throw new Error(`${encoding} encoding not supported`);
11
55
  }
12
- const dv = new DataView(new BigInt64Array(1).buffer);
13
- const littleEndian = encoding.endsWith("le");
14
- dv.setBigInt64(0, BigInt(value?.getTime() ?? 0), littleEndian);
15
- return dv.buffer;
16
56
  };
17
57
  export const decodeTimestamp = (value, encoding = "i64", byteOffset) => {
58
+ const littleEndian = encoding.endsWith("le");
59
+ const offset = byteOffset ?? 0;
60
+ if (!value || offset < 0) {
61
+ return undefined;
62
+ }
63
+ const dv = new DataView(value);
18
64
  switch (encoding) {
19
- case "i64": break;
20
- case "i64le": break;
65
+ case "i32":
66
+ case "i32le": {
67
+ const ts = value.byteLength < offset + 4
68
+ ? undefined
69
+ : new Date(dv.getInt32(offset, littleEndian) * minuteMilliseconds);
70
+ return !ts || Number.isNaN(ts.getTime()) ? undefined : ts;
71
+ }
72
+ case "i64":
73
+ case "i64le": {
74
+ const ts = value.byteLength < offset + 8
75
+ ? undefined
76
+ : new Date(Number(dv.getBigInt64(offset, littleEndian)));
77
+ return !ts || Number.isNaN(ts.getTime()) ? undefined : ts;
78
+ }
21
79
  default: throw new Error(`${encoding} encoding not supported`);
22
80
  }
23
- const offset = byteOffset == null ? 0 : Number(byteOffset);
24
- if (!value || offset < 0 || value.byteLength < offset + 8) {
25
- return undefined;
26
- }
27
- const littleEndian = encoding.endsWith("le");
28
- return new Date(Number(new DataView(value).getBigInt64(offset, littleEndian)));
29
81
  };
30
82
  export const formatTimestamp = (value, template) => {
31
83
  if (!template || typeof template !== "string") {
@@ -84,8 +136,11 @@ export const funcDaysSince = new Constant((value1, value2) => (value1?.getTime()
84
136
  export const funcHoursSince = new Constant((value1, value2) => (value1?.getTime() - value2?.getTime()) / 3600000.0, typeTimestampSince);
85
137
  export const funcMinutesSince = new Constant((value1, value2) => (value1?.getTime() - value2?.getTime()) / 60000.0, typeTimestampSince);
86
138
  export const funcSecondsSince = new Constant((value1, value2) => (value1?.getTime() - value2?.getTime()) / 1000.0, typeTimestampSince);
87
- const funcEpochTimestamp = new Constant((value, epoch = new Date(0)) => new Date(Number(value ?? 0) + epoch?.getTime()), Type.functionType(Type.Timestamp, [Type.Number, Type.OptionalTimestamp]));
88
- const funcDecodeTimestamp = new Constant((value, encoding = "i64", byteOffset) => decodeTimestamp(value, encoding, byteOffset), Type.functionType(Type.OptionalTimestamp, [Type.OptionalBuffer, Type.OptionalString, Type.OptionalInteger]));
139
+ const funcEpochTimestamp = new Constant((value, epoch = new Date(0)) => {
140
+ const ts = new Date(Number(value ?? 0) + epoch?.getTime());
141
+ return Number.isNaN(ts.getTime()) ? undefined : ts;
142
+ }, Type.functionType(Type.Timestamp, [Type.Number, Type.OptionalTimestamp]));
143
+ const funcDecodeTimestamp = new Constant((value, encoding = "i64", byteOffset) => decodeTimestamp(value, encoding, byteOffset == null ? undefined : Number(byteOffset)), Type.functionType(Type.OptionalTimestamp, [Type.OptionalBuffer, Type.OptionalString, Type.OptionalInteger]));
89
144
  const funcParseTimestamp = new Constant((value) => parseTimestamp(value), Type.functionType(Type.OptionalTimestamp, [Type.OptionalString]));
90
145
  export const constTimestamp = Object.assign(Object.create(null), {
91
146
  Now: funcNow,
@@ -1,9 +1,9 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
+ import { formatBuffer, concatBuffers, equateBuffers, EmptyBuffer } from "./Buffer.js";
3
4
  import { encodeFloat } from "./Float.js";
4
5
  import { encodeInteger } from "./Integer.js";
5
6
  import { formatNumber } from "./Number.js";
6
- import { formatBuffer, concatBuffers, equateBuffers, EmptyBuffer } from "./Buffer.js";
7
7
  import { encodeString } from "./String.js";
8
8
  import { encodeTimestamp, formatTimestamp } from "./Timestamp.js";
9
9
  export const equate = (value1, value2) => {
@@ -1,3 +1,4 @@
1
1
  import { Value } from "../../Value.js";
2
2
  export declare const formatAN: (value: Value, whitespace?: string) => string;
3
+ export declare const parseAN: (value: string | undefined) => Value;
3
4
  export declare const constAN: any;