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.
- package/README.md +30 -14
- package/dst/Affinirum.d.ts +33 -26
- package/dst/Affinirum.js +58 -44
- package/dst/ParserState.js +23 -79
- package/dst/Scope.d.ts +16 -0
- package/dst/Scope.js +49 -0
- package/dst/atom/ArrayAtom.js +4 -1
- package/dst/cjs/Affinirum.js +57 -43
- package/dst/cjs/ParserState.js +22 -78
- package/dst/cjs/Scope.js +53 -0
- package/dst/cjs/atom/ArrayAtom.js +4 -1
- package/dst/cjs/native/Aggregable.js +1 -1
- package/dst/cjs/native/Boolean.js +3 -1
- package/dst/cjs/native/Buffer.js +32 -3
- package/dst/cjs/native/Enumerable.js +5 -3
- package/dst/cjs/native/Float.js +17 -5
- package/dst/cjs/native/Integer.js +41 -13
- package/dst/cjs/native/Iterable.js +6 -4
- package/dst/cjs/native/Number.js +45 -1
- package/dst/cjs/native/String.js +68 -22
- package/dst/cjs/native/Timestamp.js +74 -17
- package/dst/cjs/native/Unknown.js +1 -1
- package/dst/cjs/native/notation/AN.js +152 -11
- package/dst/cjs/native/notation/IP.js +1 -1
- package/dst/cjs/native/notation/JSON.js +1 -1
- package/dst/cjs/node/BlockNode.js +5 -4
- package/dst/cjs/node/CallNode.js +7 -5
- package/dst/cjs/node/SwitchNode.js +7 -9
- package/dst/native/Aggregable.js +1 -1
- package/dst/native/Boolean.js +3 -1
- package/dst/native/Buffer.d.ts +2 -0
- package/dst/native/Buffer.js +29 -2
- package/dst/native/Enumerable.js +5 -3
- package/dst/native/Float.js +17 -5
- package/dst/native/Integer.js +41 -13
- package/dst/native/Iterable.js +6 -4
- package/dst/native/Number.d.ts +4 -0
- package/dst/native/Number.js +40 -0
- package/dst/native/String.d.ts +2 -7
- package/dst/native/String.js +65 -14
- package/dst/native/Timestamp.d.ts +4 -2
- package/dst/native/Timestamp.js +71 -16
- package/dst/native/Unknown.js +1 -1
- package/dst/native/notation/AN.d.ts +1 -0
- package/dst/native/notation/AN.js +152 -12
- package/dst/native/notation/IP.js +1 -1
- package/dst/native/notation/JSON.js +1 -1
- package/dst/node/BlockNode.js +5 -4
- package/dst/node/CallNode.js +7 -5
- package/dst/node/SwitchNode.js +7 -9
- package/package.json +1 -1
- package/dst/StaticScope.d.ts +0 -13
- package/dst/StaticScope.js +0 -44
- package/dst/cjs/StaticScope.js +0 -48
package/dst/native/Buffer.js
CHANGED
|
@@ -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
|
|
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
|
|
57
|
+
throw new Error(`Invalid integer: ${value}`);
|
|
31
58
|
}
|
|
32
59
|
return value;
|
|
33
60
|
};
|
package/dst/native/Enumerable.js
CHANGED
|
@@ -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
|
|
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,
|
|
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,
|
|
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)));
|
package/dst/native/Float.js
CHANGED
|
@@ -30,12 +30,24 @@ const decodeFloat = (value, encoding = "f64", byteOffset) => {
|
|
|
30
30
|
if (value == null) {
|
|
31
31
|
return undefined;
|
|
32
32
|
}
|
|
33
|
-
const
|
|
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
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
case "
|
|
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
|
};
|
package/dst/native/Integer.js
CHANGED
|
@@ -67,20 +67,48 @@ const decodeInteger = (value, encoding = "i64", byteOffset) => {
|
|
|
67
67
|
if (value == null) {
|
|
68
68
|
return undefined;
|
|
69
69
|
}
|
|
70
|
-
const
|
|
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
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
case "
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
case "
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
case "
|
|
82
|
-
|
|
83
|
-
|
|
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
|
};
|
package/dst/native/Iterable.js
CHANGED
|
@@ -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
|
|
28
|
-
|
|
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
|
|
39
|
-
|
|
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])));
|
package/dst/native/Number.d.ts
CHANGED
|
@@ -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;
|
package/dst/native/Number.js
CHANGED
|
@@ -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}}`);
|
package/dst/native/String.d.ts
CHANGED
|
@@ -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;
|
package/dst/native/String.js
CHANGED
|
@@ -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) ||
|
|
9
|
-
export const
|
|
10
|
-
export const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
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
|
|
270
|
+
const str = value.toLowerCase();
|
|
220
271
|
let result = "";
|
|
221
|
-
for (let i = 0; i <
|
|
222
|
-
if (isAlphanumericSymbol(
|
|
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?:
|
|
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;
|
package/dst/native/Timestamp.js
CHANGED
|
@@ -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 "
|
|
9
|
-
case "
|
|
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 "
|
|
20
|
-
case "
|
|
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)) =>
|
|
88
|
-
const
|
|
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,
|
package/dst/native/Unknown.js
CHANGED
|
@@ -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) => {
|