affinirum 1.0.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.
Files changed (121) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +301 -0
  3. package/dst/Affinirum.d.ts +73 -0
  4. package/dst/Affinirum.js +536 -0
  5. package/dst/Constant.d.ts +15 -0
  6. package/dst/Constant.js +26 -0
  7. package/dst/Constants.d.ts +2 -0
  8. package/dst/Constants.js +22 -0
  9. package/dst/Functions.d.ts +2 -0
  10. package/dst/Functions.js +85 -0
  11. package/dst/Keywords.d.ts +1 -0
  12. package/dst/Keywords.js +17 -0
  13. package/dst/Node.d.ts +13 -0
  14. package/dst/Node.js +22 -0
  15. package/dst/ParserFrame.d.ts +13 -0
  16. package/dst/ParserFrame.js +80 -0
  17. package/dst/ParserState.d.ts +54 -0
  18. package/dst/ParserState.js +562 -0
  19. package/dst/StaticScope.d.ts +13 -0
  20. package/dst/StaticScope.js +44 -0
  21. package/dst/Type.d.ts +72 -0
  22. package/dst/Type.js +196 -0
  23. package/dst/Value.d.ts +3 -0
  24. package/dst/Value.js +1 -0
  25. package/dst/Variable.d.ts +13 -0
  26. package/dst/Variable.js +25 -0
  27. package/dst/atom/ArrayAtom.d.ts +11 -0
  28. package/dst/atom/ArrayAtom.js +38 -0
  29. package/dst/atom/FunctionAtom.d.ts +17 -0
  30. package/dst/atom/FunctionAtom.js +54 -0
  31. package/dst/atom/ObjectAtom.d.ts +11 -0
  32. package/dst/atom/ObjectAtom.js +44 -0
  33. package/dst/atom/PrimitiveAtom.d.ts +8 -0
  34. package/dst/atom/PrimitiveAtom.js +26 -0
  35. package/dst/cjs/Affinirum.js +540 -0
  36. package/dst/cjs/Constant.js +30 -0
  37. package/dst/cjs/Constants.js +25 -0
  38. package/dst/cjs/Functions.js +88 -0
  39. package/dst/cjs/Keywords.js +20 -0
  40. package/dst/cjs/Node.js +26 -0
  41. package/dst/cjs/ParserFrame.js +84 -0
  42. package/dst/cjs/ParserState.js +566 -0
  43. package/dst/cjs/StaticScope.js +48 -0
  44. package/dst/cjs/Type.js +200 -0
  45. package/dst/cjs/Value.js +2 -0
  46. package/dst/cjs/Variable.js +29 -0
  47. package/dst/cjs/atom/ArrayAtom.js +42 -0
  48. package/dst/cjs/atom/FunctionAtom.js +58 -0
  49. package/dst/cjs/atom/ObjectAtom.js +48 -0
  50. package/dst/cjs/atom/PrimitiveAtom.js +30 -0
  51. package/dst/cjs/constant/Array.js +93 -0
  52. package/dst/cjs/constant/Boolean.js +25 -0
  53. package/dst/cjs/constant/Buffer.js +68 -0
  54. package/dst/cjs/constant/Enumerable.js +34 -0
  55. package/dst/cjs/constant/Float.js +85 -0
  56. package/dst/cjs/constant/Integer.js +104 -0
  57. package/dst/cjs/constant/Iterable.js +24 -0
  58. package/dst/cjs/constant/Number.js +59 -0
  59. package/dst/cjs/constant/Object.js +13 -0
  60. package/dst/cjs/constant/String.js +197 -0
  61. package/dst/cjs/constant/Timestamp.js +56 -0
  62. package/dst/cjs/constant/Unknown.js +101 -0
  63. package/dst/cjs/constant/notation/AN.js +46 -0
  64. package/dst/cjs/constant/notation/JSON.js +14 -0
  65. package/dst/cjs/index.js +19 -0
  66. package/dst/cjs/node/ArrayNode.js +34 -0
  67. package/dst/cjs/node/BlockNode.js +33 -0
  68. package/dst/cjs/node/CallNode.js +54 -0
  69. package/dst/cjs/node/ConstantNode.js +33 -0
  70. package/dst/cjs/node/LoopNode.js +34 -0
  71. package/dst/cjs/node/ObjectNode.js +42 -0
  72. package/dst/cjs/node/SwitchNode.js +45 -0
  73. package/dst/cjs/node/VariableNode.js +30 -0
  74. package/dst/cjs/package.json +3 -0
  75. package/dst/constant/Array.d.ts +22 -0
  76. package/dst/constant/Array.js +90 -0
  77. package/dst/constant/Boolean.d.ts +11 -0
  78. package/dst/constant/Boolean.js +22 -0
  79. package/dst/constant/Buffer.d.ts +10 -0
  80. package/dst/constant/Buffer.js +61 -0
  81. package/dst/constant/Enumerable.d.ts +5 -0
  82. package/dst/constant/Enumerable.js +31 -0
  83. package/dst/constant/Float.d.ts +22 -0
  84. package/dst/constant/Float.js +80 -0
  85. package/dst/constant/Integer.d.ts +10 -0
  86. package/dst/constant/Integer.js +100 -0
  87. package/dst/constant/Iterable.d.ts +3 -0
  88. package/dst/constant/Iterable.js +21 -0
  89. package/dst/constant/Number.d.ts +14 -0
  90. package/dst/constant/Number.js +56 -0
  91. package/dst/constant/Object.d.ts +7 -0
  92. package/dst/constant/Object.js +10 -0
  93. package/dst/constant/String.d.ts +34 -0
  94. package/dst/constant/String.js +179 -0
  95. package/dst/constant/Timestamp.d.ts +20 -0
  96. package/dst/constant/Timestamp.js +50 -0
  97. package/dst/constant/Unknown.d.ts +10 -0
  98. package/dst/constant/Unknown.js +95 -0
  99. package/dst/constant/notation/AN.d.ts +6 -0
  100. package/dst/constant/notation/AN.js +42 -0
  101. package/dst/constant/notation/JSON.d.ts +7 -0
  102. package/dst/constant/notation/JSON.js +10 -0
  103. package/dst/index.d.ts +3 -0
  104. package/dst/index.js +3 -0
  105. package/dst/node/ArrayNode.d.ts +12 -0
  106. package/dst/node/ArrayNode.js +30 -0
  107. package/dst/node/BlockNode.d.ts +12 -0
  108. package/dst/node/BlockNode.js +29 -0
  109. package/dst/node/CallNode.d.ts +14 -0
  110. package/dst/node/CallNode.js +50 -0
  111. package/dst/node/ConstantNode.d.ts +15 -0
  112. package/dst/node/ConstantNode.js +29 -0
  113. package/dst/node/LoopNode.d.ts +13 -0
  114. package/dst/node/LoopNode.js +30 -0
  115. package/dst/node/ObjectNode.d.ts +13 -0
  116. package/dst/node/ObjectNode.js +38 -0
  117. package/dst/node/SwitchNode.d.ts +14 -0
  118. package/dst/node/SwitchNode.js +41 -0
  119. package/dst/node/VariableNode.d.ts +14 -0
  120. package/dst/node/VariableNode.js +26 -0
  121. package/package.json +67 -0
@@ -0,0 +1,10 @@
1
+ import { Constant } from '../Constant.js';
2
+ export declare const equateBuffers: (value1: ArrayBuffer, value2: ArrayBuffer) => boolean;
3
+ export declare const concatBuffers: (value1: ArrayBuffer, value2: ArrayBuffer) => ArrayBuffer;
4
+ export declare const formatBuffer: (value?: ArrayBuffer) => string;
5
+ export declare const parseBuffer: (value?: string) => ArrayBuffer | undefined;
6
+ export declare const funcByte: Constant;
7
+ export declare const constBuffer: {
8
+ Random: Constant;
9
+ Parse: Constant;
10
+ };
@@ -0,0 +1,61 @@
1
+ import { Constant } from '../Constant.js';
2
+ import { Type } from '../Type.js';
3
+ export const equateBuffers = (value1, value2) => {
4
+ if (value1.byteLength !== value2.byteLength) {
5
+ return false;
6
+ }
7
+ const dv1 = new DataView(value1);
8
+ const dv2 = new DataView(value2);
9
+ const length = dv1.byteLength;
10
+ for (let lfi = length - 3, i = 0; i < length;) {
11
+ if (i < lfi) {
12
+ if (dv1.getUint32(i) !== dv2.getUint32(i)) {
13
+ return false;
14
+ }
15
+ i += 4;
16
+ }
17
+ else {
18
+ if (dv1.getUint8(i) !== dv2.getUint8(i)) {
19
+ return false;
20
+ }
21
+ ++i;
22
+ }
23
+ }
24
+ return true;
25
+ };
26
+ export const concatBuffers = (value1, value2) => {
27
+ const bytes = new Uint8Array(value1.byteLength + value2.byteLength);
28
+ bytes.set(new Uint8Array(value1), 0);
29
+ bytes.set(new Uint8Array(value2), value1.byteLength);
30
+ return bytes.buffer;
31
+ };
32
+ export const formatBuffer = (value) => {
33
+ if (value == null) {
34
+ return '';
35
+ }
36
+ const bytes = new Uint8Array(value);
37
+ let str = '';
38
+ for (let i = 0; i < bytes.byteLength; ++i) {
39
+ str += bytes[i].toString(16).padStart(2, '0');
40
+ }
41
+ return str;
42
+ };
43
+ export const parseBuffer = (value) => {
44
+ if (value == null) {
45
+ return undefined;
46
+ }
47
+ const bytes = new Uint8Array(Math.ceil(value.length / 2));
48
+ for (let i = 0, c = 0; c < value.length; ++i) {
49
+ bytes[i] = Number.parseInt(value.slice(c, c += 2), 16);
50
+ }
51
+ return bytes.buffer;
52
+ };
53
+ export const funcByte = new Constant((value, pos) => value == null
54
+ ? undefined
55
+ : value.slice(Number(pos), Number(pos) + 1), Type.functionType(Type.OptionalBuffer, [Type.Buffer, Type.Integer]));
56
+ const funcRandomBuffer = new Constant((value) => value == null || value < 0 ? undefined : crypto.getRandomValues(new Uint8Array(Number(value))), Type.functionType(Type.Buffer, [Type.Integer]), false);
57
+ const funcParseBuffer = new Constant((value) => parseBuffer(value), Type.functionType(Type.OptionalBuffer, [Type.String]));
58
+ export const constBuffer = {
59
+ Random: funcRandomBuffer,
60
+ Parse: funcParseBuffer,
61
+ };
@@ -0,0 +1,5 @@
1
+ import { Constant } from '../Constant.js';
2
+ export declare const funcAdd: Constant;
3
+ export declare const funcSlice: Constant;
4
+ export declare const funcSplice: Constant;
5
+ export declare const funcInject: Constant;
@@ -0,0 +1,31 @@
1
+ import { Constant } from '../Constant.js';
2
+ import { Type } from '../Type.js';
3
+ import { concatBuffers } from './Buffer.js';
4
+ const aggregate = (value1, value2) => {
5
+ if (value1 instanceof ArrayBuffer && value2 instanceof ArrayBuffer) {
6
+ return concatBuffers(value1, value2);
7
+ }
8
+ if (typeof value1 === 'string' && typeof value2 === 'string') {
9
+ return value1.concat(value2);
10
+ }
11
+ return [value1].flat().concat([value2].flat());
12
+ };
13
+ const add = (value1, value2) => {
14
+ if (typeof value1 === 'bigint' && typeof value2 === 'bigint') {
15
+ return BigInt.asIntN(64, value1 + value2);
16
+ }
17
+ if (typeof value1 === 'number' || typeof value2 === 'number') {
18
+ return Number(value1) + Number(value2);
19
+ }
20
+ return aggregate(value1, value2);
21
+ };
22
+ export const funcAdd = new Constant((value1, value2) => add(value1, value2), Type.union(Type.functionType(Type.Float, [Type.Float, Type.Integer]), Type.functionType(Type.Float, [Type.Integer, Type.Float]), Type.functionType(Type.Integer, [Type.Integer, Type.Integer]), Type.functionType(Type.Buffer, [Type.Buffer, Type.Buffer]), Type.functionType(Type.String, [Type.String, Type.String]), Type.functionType(Type.Array, [Type.Array, Type.Array])));
23
+ export const funcSlice = new Constant((value, start = 0n, end) => value == null
24
+ ? undefined
25
+ : 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])));
26
+ export const funcSplice = new Constant((value, start, remove, ...inject) => value == null
27
+ ? undefined
28
+ : 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, Type.Enumerable], true), Type.functionType(Type.String, [Type.String, Type.Integer, Type.Integer, Type.Enumerable], true), Type.functionType(Type.Array, [Type.Array, Type.Integer, Type.Integer, Type.Enumerable], true)));
29
+ export const funcInject = new Constant((value, start, ...inject) => value == null
30
+ ? undefined
31
+ : 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, Type.Enumerable], true), Type.functionType(Type.String, [Type.String, Type.Integer, Type.Enumerable], true), Type.functionType(Type.Array, [Type.Array, Type.Integer, Type.Enumerable], true)));
@@ -0,0 +1,22 @@
1
+ import { Constant } from '../Constant.js';
2
+ export declare const encodeFloat: (value: number, encoding?: "float32" | "float32le" | "float64" | "float64le") => ArrayBuffer;
3
+ export declare const formatFloat: (value: number, radix?: number) => string;
4
+ export declare const constFloat: {
5
+ NAN: Constant;
6
+ PositiveInfinity: Constant;
7
+ NegativeInfinity: Constant;
8
+ Epsilon: Constant;
9
+ Sum: Constant;
10
+ Min: Constant;
11
+ Max: Constant;
12
+ Exponent: Constant;
13
+ Logarithm: Constant;
14
+ Abs: Constant;
15
+ Ceil: Constant;
16
+ Floor: Constant;
17
+ Round: Constant;
18
+ Truncate: Constant;
19
+ Random: Constant;
20
+ Decode: Constant;
21
+ Parse: Constant;
22
+ };
@@ -0,0 +1,80 @@
1
+ import { Constant } from '../Constant.js';
2
+ import { Type } from '../Type.js';
3
+ export const encodeFloat = (value, encoding = 'float64') => {
4
+ if (value == null) {
5
+ return new Uint8Array(0).buffer;
6
+ }
7
+ let bits = '';
8
+ for (let i = 0; i < encoding.length; ++i) {
9
+ const c = encoding[i];
10
+ if (c >= '0' && c <= '9') {
11
+ bits += c;
12
+ }
13
+ }
14
+ const dv = new DataView(new Uint8Array(Number.parseInt(bits) / 8).buffer);
15
+ switch (encoding) {
16
+ case 'float32':
17
+ dv.setFloat32(0, value);
18
+ break;
19
+ case 'float32le':
20
+ dv.setFloat32(0, value, true);
21
+ break;
22
+ case 'float64':
23
+ dv.setFloat64(0, value);
24
+ break;
25
+ case 'float64le':
26
+ dv.setFloat64(0, value, true);
27
+ break;
28
+ default: throw new Error(`${encoding} encoding not supported`);
29
+ }
30
+ return dv.buffer;
31
+ };
32
+ const decodeFloat = (value, encoding = 'float64', byteOffset) => {
33
+ if (value == null) {
34
+ return undefined;
35
+ }
36
+ const dv = new DataView(value, byteOffset);
37
+ switch (encoding) {
38
+ case 'float32': return dv.getFloat32(0);
39
+ case 'float32le': return dv.getFloat32(0, true);
40
+ case 'float64': return dv.getFloat64(0);
41
+ case 'float64le': return dv.getFloat64(0, true);
42
+ default: throw new Error(`${encoding} encoding not supported`);
43
+ }
44
+ };
45
+ export const formatFloat = (value, radix) => value.toString(radix) + (Number.isInteger(value) ? '.0' : '');
46
+ const typeNumberOrArray = Type.union(Type.Float, Type.arrayType([Type.Float]));
47
+ const typeAggregator = Type.functionType(Type.Float, [typeNumberOrArray], true);
48
+ const typeNumberTransform = Type.functionType(Type.Float, [Type.Float]);
49
+ const funcSum = new Constant((...values) => values.flat().reduce((acc, val) => acc + val, 0), typeAggregator);
50
+ const funcMin = new Constant((...values) => Math.min(Number.POSITIVE_INFINITY, ...values.flat()), typeAggregator);
51
+ const funcMax = new Constant((...values) => Math.max(Number.NEGATIVE_INFINITY, ...values.flat()), typeAggregator);
52
+ const funcExponent = new Constant((value) => Math.exp(value), typeNumberTransform);
53
+ const funcLogarithm = new Constant((value) => Math.log(value), typeNumberTransform);
54
+ const funcAbs = new Constant((value) => Math.abs(value), typeNumberTransform);
55
+ const funcCeil = new Constant((value) => Math.ceil(value), typeNumberTransform);
56
+ const funcFloor = new Constant((value) => Math.floor(value), typeNumberTransform);
57
+ const funcRound = new Constant((value) => Math.round(value), typeNumberTransform);
58
+ const funcTruncate = new Constant((value) => Math.trunc(value), typeNumberTransform);
59
+ const funcRandomFloat = new Constant((value) => value == null ? undefined : Math.random() * value, Type.functionType(Type.Float, [Type.Float]), false);
60
+ const funcDecodeFloat = new Constant((value, encoding = 'float64', byteOffset) => decodeFloat(value, encoding, byteOffset == null ? undefined : Number(byteOffset)), Type.functionType(Type.OptionalFloat, [Type.Buffer, Type.OptionalString, Type.OptionalInteger]));
61
+ const funcParseFloat = new Constant((value) => value ? Number.parseFloat(value) : undefined, Type.functionType(Type.OptionalFloat, [Type.String]));
62
+ export const constFloat = {
63
+ NAN: new Constant(Number.NaN),
64
+ PositiveInfinity: new Constant(Number.POSITIVE_INFINITY),
65
+ NegativeInfinity: new Constant(Number.NEGATIVE_INFINITY),
66
+ Epsilon: new Constant(Number.EPSILON),
67
+ Sum: funcSum,
68
+ Min: funcMin,
69
+ Max: funcMax,
70
+ Exponent: funcExponent,
71
+ Logarithm: funcLogarithm,
72
+ Abs: funcAbs,
73
+ Ceil: funcCeil,
74
+ Floor: funcFloor,
75
+ Round: funcRound,
76
+ Truncate: funcTruncate,
77
+ Random: funcRandomFloat,
78
+ Decode: funcDecodeFloat,
79
+ Parse: funcParseFloat,
80
+ };
@@ -0,0 +1,10 @@
1
+ import { Constant } from '../Constant.js';
2
+ export declare const encodeInteger: (value: bigint, encoding?: "int8" | "int16" | "int16le" | "int32" | "int32le" | "uint8" | "uint16" | "uint16le" | "uint32" | "uint32le" | "int64" | "int64le" | "uint64" | "uint64le") => ArrayBuffer;
3
+ export declare const constInteger: {
4
+ Sum: Constant;
5
+ Min: Constant;
6
+ Max: Constant;
7
+ Random: Constant;
8
+ Decode: Constant;
9
+ Parse: Constant;
10
+ };
@@ -0,0 +1,100 @@
1
+ import { Constant } from '../Constant.js';
2
+ import { Type } from '../Type.js';
3
+ export const encodeInteger = (value, encoding = 'int64') => {
4
+ if (value == null) {
5
+ return new Uint8Array(0).buffer;
6
+ }
7
+ let bits = '';
8
+ for (let i = 0; i < encoding.length; ++i) {
9
+ const c = encoding[i];
10
+ if (c >= '0' && c <= '9') {
11
+ bits += c;
12
+ }
13
+ }
14
+ const dv = new DataView(new Uint8Array(Number.parseInt(bits) / 8).buffer);
15
+ switch (encoding) {
16
+ case 'int8':
17
+ dv.setInt8(0, Number(value));
18
+ break;
19
+ case 'int16':
20
+ dv.setInt16(0, Number(value));
21
+ break;
22
+ case 'int16le':
23
+ dv.setInt16(0, Number(value), true);
24
+ break;
25
+ case 'int32':
26
+ dv.setInt32(0, Number(value));
27
+ break;
28
+ case 'int32le':
29
+ dv.setInt32(0, Number(value), true);
30
+ break;
31
+ case 'uint8':
32
+ dv.setUint8(0, Number(value));
33
+ break;
34
+ case 'uint16':
35
+ dv.setUint16(0, Number(value));
36
+ break;
37
+ case 'uint16le':
38
+ dv.setUint16(0, Number(value), true);
39
+ break;
40
+ case 'uint32':
41
+ dv.setUint32(0, Number(value));
42
+ break;
43
+ case 'uint32le':
44
+ dv.setUint32(0, Number(value), true);
45
+ break;
46
+ case 'int64':
47
+ dv.setBigInt64(0, value);
48
+ break;
49
+ case 'int64le':
50
+ dv.setBigInt64(0, value, true);
51
+ break;
52
+ case 'uint64':
53
+ dv.setBigUint64(0, value);
54
+ break;
55
+ case 'uint64le':
56
+ dv.setBigUint64(0, value, true);
57
+ break;
58
+ default: throw new Error(`${encoding} encoding not supported`);
59
+ }
60
+ return dv.buffer;
61
+ };
62
+ const decodeInteger = (value, encoding = 'int64', byteOffset) => {
63
+ if (value == null) {
64
+ return undefined;
65
+ }
66
+ const dv = new DataView(value, byteOffset == null ? undefined : Number(byteOffset));
67
+ switch (encoding) {
68
+ case 'int8': return BigInt(dv.getInt8(0));
69
+ case 'int16': return BigInt(dv.getInt16(0));
70
+ case 'int16le': return BigInt(dv.getInt16(0, true));
71
+ case 'int32': return BigInt(dv.getInt32(0));
72
+ case 'int32le': return BigInt(dv.getInt32(0, true));
73
+ case 'int64': return dv.getBigInt64(0);
74
+ case 'int64le': return dv.getBigInt64(0, true);
75
+ case 'uint8': return BigInt(dv.getUint8(0));
76
+ case 'uint16': return BigInt(dv.getUint16(0));
77
+ case 'uint16le': return BigInt(dv.getUint16(0, true));
78
+ case 'uint32': return BigInt(dv.getUint32(0));
79
+ case 'uint32le': return BigInt(dv.getUint32(0, true));
80
+ case 'uint64': return dv.getBigUint64(0);
81
+ case 'uint64le': return dv.getBigUint64(0, true);
82
+ default: throw new Error(`${encoding} encoding not supported`);
83
+ }
84
+ };
85
+ const typeIntegerOrArray = Type.union(Type.Integer, Type.arrayType([Type.Integer]));
86
+ const typeAggregator = Type.functionType(Type.Integer, [typeIntegerOrArray], true);
87
+ const funcSum = new Constant((...values) => BigInt.asIntN(64, values.flat().reduce((acc, val) => acc + val, 0n)), typeAggregator);
88
+ const funcMin = new Constant((...values) => BigInt.asIntN(64, values.flat().reduce((min, val) => val < min ? val : min)), typeAggregator);
89
+ const funcMax = new Constant((...values) => BigInt.asIntN(64, values.flat().reduce((max, val) => val > max ? val : max)), typeAggregator);
90
+ const funcRandomInteger = new Constant((value) => value == null ? undefined : BigInt.asIntN(64, BigInt(Math.floor(Math.random() * Number(value)))), Type.functionType(Type.Integer, [Type.Integer]), false);
91
+ const funcDecodeInteger = new Constant((value, encoding = 'int64', byteOffset) => decodeInteger(value, encoding, byteOffset == null ? undefined : Number(byteOffset)), Type.functionType(Type.OptionalInteger, [Type.Buffer, Type.OptionalString, Type.OptionalInteger]));
92
+ const funcParseInteger = new Constant((value) => value ? BigInt.asIntN(64, BigInt(value)) : undefined, Type.functionType(Type.OptionalInteger, [Type.String]));
93
+ export const constInteger = {
94
+ Sum: funcSum,
95
+ Min: funcMin,
96
+ Max: funcMax,
97
+ Random: funcRandomInteger,
98
+ Decode: funcDecodeInteger,
99
+ Parse: funcParseInteger,
100
+ };
@@ -0,0 +1,3 @@
1
+ import { Constant } from '../Constant.js';
2
+ export declare const funcLength: Constant;
3
+ export declare const funcAt: Constant;
@@ -0,0 +1,21 @@
1
+ import { Constant } from '../Constant.js';
2
+ import { Type } from '../Type.js';
3
+ export const funcLength = new Constant((value) => BigInt.asIntN(64, BigInt(value == null
4
+ ? 0
5
+ : value instanceof ArrayBuffer
6
+ ? value.byteLength
7
+ : typeof value === 'string' || Array.isArray(value)
8
+ ? value.length
9
+ : Object.keys(value).length)), Type.functionType(Type.Integer, [Type.Iterable]));
10
+ export const funcAt = new Constant((value, index) => {
11
+ if (value == null) {
12
+ return undefined;
13
+ }
14
+ else if (Array.isArray(value)) {
15
+ const ix = Number(index);
16
+ return value[ix < 0 ? value.length + ix : ix];
17
+ }
18
+ else {
19
+ return value[String(index)];
20
+ }
21
+ }, Type.union(Type.functionType(Type.Unknown, [Type.Array, Type.Integer]), Type.functionType(Type.Unknown, [Type.Object, Type.String])));
@@ -0,0 +1,14 @@
1
+ import { Constant } from '../Constant.js';
2
+ export declare const funcGreaterThan: Constant;
3
+ export declare const funcLessThan: Constant;
4
+ export declare const funcGreaterOrEqual: Constant;
5
+ export declare const funcLessOrEqual: Constant;
6
+ export declare const funcSubtract: Constant;
7
+ export declare const funcMultiply: Constant;
8
+ export declare const funcDivide: Constant;
9
+ export declare const funcRemainder: Constant;
10
+ export declare const funcModulo: Constant;
11
+ export declare const funcPower: Constant;
12
+ export declare const funcRoot: Constant;
13
+ export declare const funcNegate: Constant;
14
+ export declare const funcCast: Constant;
@@ -0,0 +1,56 @@
1
+ import { Constant } from '../Constant.js';
2
+ import { Type } from '../Type.js';
3
+ const typeComparator = Type.functionType(Type.Boolean, [Type.Number, Type.Number]);
4
+ const typeOperator = Type.union(Type.functionType(Type.Float, [Type.Float, Type.Integer]), Type.functionType(Type.Float, [Type.Integer, Type.Float]), Type.functionType(Type.Integer, [Type.Integer, Type.Integer]));
5
+ export const funcGreaterThan = new Constant((value1, value2) => value1 > value2, typeComparator);
6
+ export const funcLessThan = new Constant((value1, value2) => value1 < value2, typeComparator);
7
+ export const funcGreaterOrEqual = new Constant((value1, value2) => value1 >= value2, typeComparator);
8
+ export const funcLessOrEqual = new Constant((value1, value2) => value1 <= value2, typeComparator);
9
+ export const funcSubtract = new Constant((value, subtrahend) => typeof value === 'bigint' && typeof subtrahend === 'bigint'
10
+ ? BigInt.asIntN(64, value - subtrahend)
11
+ : Number(value) - Number(subtrahend), typeOperator);
12
+ export const funcMultiply = new Constant((value1, value2) => typeof value1 === 'bigint' && typeof value2 === 'bigint'
13
+ ? BigInt.asIntN(64, value1 * value2)
14
+ : Number(value1) * Number(value2), typeOperator);
15
+ export const funcDivide = new Constant((value, divisor) => typeof value === 'bigint' && typeof divisor === 'bigint'
16
+ ? BigInt.asIntN(64, value / divisor)
17
+ : Number(value) / Number(divisor), typeOperator);
18
+ export const funcRemainder = new Constant((value, divisor) => typeof value === 'bigint' && typeof divisor === 'bigint'
19
+ ? BigInt.asIntN(64, value % divisor)
20
+ : Number(value) % Number(divisor), typeOperator);
21
+ export const funcModulo = new Constant((value, divisor) => typeof value === 'bigint' && typeof divisor === 'bigint'
22
+ ? BigInt.asIntN(64, (value % divisor + divisor) % divisor)
23
+ : (Number(value) % Number(divisor) + Number(divisor)) % Number(divisor), typeOperator);
24
+ export const funcPower = new Constant((value, exponent) => typeof value === 'bigint' && typeof exponent === 'bigint'
25
+ ? BigInt.asIntN(64, value ** BigInt(exponent))
26
+ : Number(value) ** Number(exponent), typeOperator);
27
+ export const funcRoot = new Constant((value, exponent) => {
28
+ if (typeof value === 'bigint' && typeof exponent === 'bigint') {
29
+ const e = BigInt(exponent);
30
+ if (value < 0n && e % 2n === 0n) {
31
+ return undefined;
32
+ }
33
+ if (value < 2n || e === 1n) {
34
+ return value;
35
+ }
36
+ let low = 0n, high = value, result = 0n;
37
+ while (low <= high) {
38
+ const mid = low + high >> 1n;
39
+ const midPow = mid ** e;
40
+ if (midPow === value) {
41
+ return mid;
42
+ }
43
+ else if (midPow < value) {
44
+ result = mid;
45
+ low = mid + 1n;
46
+ }
47
+ else {
48
+ high = mid - 1n;
49
+ }
50
+ }
51
+ return BigInt.asIntN(64, result);
52
+ }
53
+ return Math.pow(Number(value), 1 / Number(exponent));
54
+ }, typeOperator);
55
+ export const funcNegate = new Constant((value) => typeof value === 'number' ? -value : BigInt.asIntN(64, -value), Type.union(Type.functionType(Type.Float, [Type.Float]), Type.functionType(Type.Integer, [Type.Integer])));
56
+ export const funcCast = new Constant((value) => typeof value === 'number' ? BigInt.asIntN(64, BigInt(Math.trunc(value))) : Number(value), Type.union(Type.functionType(Type.Float, [Type.Integer]), Type.functionType(Type.Integer, [Type.Float])));
@@ -0,0 +1,7 @@
1
+ import { Constant } from '../Constant.js';
2
+ export declare const funcEntries: Constant;
3
+ export declare const funcKeys: Constant;
4
+ export declare const funcValues: Constant;
5
+ export declare const constObject: {
6
+ Merge: Constant;
7
+ };
@@ -0,0 +1,10 @@
1
+ import { Constant } from '../Constant.js';
2
+ import { Type } from '../Type.js';
3
+ const typeAccessor = Type.functionType(Type.Array, [Type.Object]);
4
+ export const funcEntries = new Constant((value) => Object.entries(value), typeAccessor);
5
+ export const funcKeys = new Constant((value) => Object.keys(value), Type.functionType(Type.arrayType([Type.String]), [Type.Object]));
6
+ export const funcValues = new Constant((value) => Object.values(value), typeAccessor);
7
+ const funcMerge = new Constant((...values) => values.flat().reduce((acc, val) => Object.assign(acc, val), {}), Type.functionType(Type.Object, [Type.union(Type.arrayType([Type.Object]), Type.Object)], true));
8
+ export const constObject = {
9
+ Merge: funcMerge,
10
+ };
@@ -0,0 +1,34 @@
1
+ import { Constant } from '../Constant.js';
2
+ export declare const isSign: (c: string) => c is "+" | "-";
3
+ export declare const isAlpha: (c: string) => boolean;
4
+ export declare const isNumeric: (c: string) => boolean;
5
+ export declare const isAlphanumeric: (c: string) => boolean;
6
+ export declare const isDateSymbol: (c: string) => boolean;
7
+ export declare const isTimeSymbol: (c: string) => boolean;
8
+ export declare const isDateTimeSeparator: (c: string) => c is " " | "T" | "@";
9
+ export declare const isHexadecimal: (c: string) => boolean;
10
+ export declare const isQuotation: (c: string) => c is "'" | "\"" | "`";
11
+ export declare const isCaseSpaceEtc: (c: string) => boolean;
12
+ export declare const equateStrings: (value1: string, value2: string, ignoreCaseSpaceEtc?: boolean) => boolean;
13
+ export declare const containsString: (value: string, search: string, startPos?: number, ignoreCaseSpaceEtc?: boolean) => boolean;
14
+ export declare const startsWithString: (value: string, search: string, startPos?: number, ignoreCaseSpaceEtc?: boolean) => boolean;
15
+ export declare const endsWithString: (value: string, search: string, endPos?: number, ignoreCaseSpaceEtc?: boolean) => boolean;
16
+ export declare const encodeString: (value: string, encoding?: "utf8" | "ucs2" | "ucs2le") => ArrayBufferLike;
17
+ export declare const funcLike: Constant;
18
+ export declare const funcUnlike: Constant;
19
+ export declare const funcContains: Constant;
20
+ export declare const funcStartsWith: Constant;
21
+ export declare const funcEndsWith: Constant;
22
+ export declare const funcChar: Constant;
23
+ export declare const funcCharCode: Constant;
24
+ export declare const funcTrim: Constant;
25
+ export declare const funcTrimStart: Constant;
26
+ export declare const funcTrimEnd: Constant;
27
+ export declare const funcLowerCase: Constant;
28
+ export declare const funcUpperCase: Constant;
29
+ export declare const funcSplit: Constant;
30
+ export declare const constString: {
31
+ Alphanum: Constant;
32
+ Random: Constant;
33
+ Decode: Constant;
34
+ };
@@ -0,0 +1,179 @@
1
+ import { Constant } from '../Constant.js';
2
+ import { Type } from '../Type.js';
3
+ export const isSign = (c) => c === '+' || c === '-';
4
+ export const isAlpha = (c) => c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c === '_';
5
+ export const isNumeric = (c) => c >= '0' && c <= '9';
6
+ export const isAlphanumeric = (c) => isAlpha(c) || isNumeric(c);
7
+ export const isDateSymbol = (c) => isNumeric(c) || c === '-';
8
+ export const isTimeSymbol = (c) => isNumeric(c) || c === ':';
9
+ export const isDateTimeSeparator = (c) => c === 'T' || c === ' ' || c === '@';
10
+ export const isHexadecimal = (c) => isNumeric(c) || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F';
11
+ export const isQuotation = (c) => c === '\'' || c === '"' || c === '`';
12
+ export const isCaseSpaceEtc = (c) => (c < 'a' || c > 'z') && (c < '0' || c > '9');
13
+ export const equateStrings = (value1, value2, ignoreCaseSpaceEtc) => {
14
+ if (!ignoreCaseSpaceEtc) {
15
+ return value1 === value2;
16
+ }
17
+ const str1 = value1.toLowerCase();
18
+ const str2 = value2.toLowerCase();
19
+ for (let i1 = 0, i2 = 0; i1 < str1.length && i2 < str2.length; ++i1, ++i2) {
20
+ while (isCaseSpaceEtc(str1[i1]) && i1 < str1.length) {
21
+ ++i1;
22
+ }
23
+ while (isCaseSpaceEtc(str2[i2]) && i2 < str2.length) {
24
+ ++i2;
25
+ }
26
+ if (str1[i1] != str2[i2]) {
27
+ return false;
28
+ }
29
+ }
30
+ return true;
31
+ };
32
+ export const containsString = (value, search, startPos, ignoreCaseSpaceEtc) => {
33
+ if (!ignoreCaseSpaceEtc) {
34
+ return value.includes(search, startPos);
35
+ }
36
+ const valueStr = value.toLowerCase();
37
+ const searchStr = search.toLowerCase();
38
+ if (valueStr.length < searchStr.length) {
39
+ return false;
40
+ }
41
+ const pos = startPos == null ? 0 : startPos < 0 ? value.length + startPos : startPos;
42
+ for (let i1 = pos, i2 = 0; i1 < valueStr.length && i2 < searchStr.length; ++i1, ++i2) {
43
+ while (isCaseSpaceEtc(valueStr[i1]) && i1 < valueStr.length) {
44
+ ++i1;
45
+ }
46
+ while (isCaseSpaceEtc(searchStr[i2]) && i2 < searchStr.length) {
47
+ ++i2;
48
+ }
49
+ while (valueStr[i1] != searchStr[i2] && i1 < valueStr.length) {
50
+ ++i1;
51
+ }
52
+ if (valueStr[i1] != searchStr[i2] && i2 < searchStr.length) {
53
+ return false;
54
+ }
55
+ }
56
+ return true;
57
+ };
58
+ export const startsWithString = (value, search, startPos, ignoreCaseSpaceEtc) => {
59
+ if (!ignoreCaseSpaceEtc) {
60
+ return value.startsWith(search, startPos);
61
+ }
62
+ const valueStr = value.toLowerCase();
63
+ const searchStr = search.toLowerCase();
64
+ if (valueStr.length < searchStr.length) {
65
+ return false;
66
+ }
67
+ const pos = startPos == null ? 0 : startPos < 0 ? value.length + startPos : startPos;
68
+ for (let i1 = pos, i2 = 0; i1 < valueStr.length && i2 < searchStr.length; ++i1, ++i2) {
69
+ while (isCaseSpaceEtc(valueStr[i1]) && i1 < valueStr.length) {
70
+ ++i1;
71
+ }
72
+ while (isCaseSpaceEtc(searchStr[i2]) && i2 < searchStr.length) {
73
+ ++i2;
74
+ }
75
+ if (valueStr[i1] != searchStr[i2] && i2 < searchStr.length) {
76
+ return false;
77
+ }
78
+ }
79
+ return true;
80
+ };
81
+ export const endsWithString = (value, search, endPos, ignoreCaseSpaceEtc) => {
82
+ if (!ignoreCaseSpaceEtc) {
83
+ return value.endsWith(search, endPos);
84
+ }
85
+ const valueStr = value.toLowerCase();
86
+ const searchStr = search.toLowerCase();
87
+ if (valueStr.length < searchStr.length) {
88
+ return false;
89
+ }
90
+ const pos = endPos == null ? valueStr.length : endPos < 0 ? value.length + endPos : endPos;
91
+ for (let i1 = pos - 1, i2 = searchStr.length - 1; i1 > -1 && i2 > -1; --i1, --i2) {
92
+ while (isCaseSpaceEtc(valueStr[i1]) && i1 > -1) {
93
+ --i1;
94
+ }
95
+ while (isCaseSpaceEtc(searchStr[i2]) && i2 > -1) {
96
+ --i2;
97
+ }
98
+ if (valueStr[i1] != searchStr[i2] && i2 > -1) {
99
+ return false;
100
+ }
101
+ }
102
+ return true;
103
+ };
104
+ export const encodeString = (value, encoding = 'utf8') => {
105
+ if (value == null) {
106
+ return new Uint8Array(0).buffer;
107
+ }
108
+ if (encoding === 'utf8') {
109
+ return new TextEncoder().encode(value).buffer;
110
+ }
111
+ else {
112
+ const dv = new DataView(new Uint16Array(value.length).buffer);
113
+ const lessOrEqual = encoding.endsWith('le');
114
+ for (let i = 0; i < value.length; ++i) {
115
+ dv.setUint16(i << 1, value.charCodeAt(i), lessOrEqual);
116
+ }
117
+ return dv.buffer;
118
+ }
119
+ };
120
+ const decodeString = (value, encoding = 'utf8', byteOffset, byteLength) => {
121
+ if (value == null) {
122
+ return undefined;
123
+ }
124
+ if (encoding === 'utf8') {
125
+ return new TextDecoder().decode(new DataView(value, byteOffset, byteLength));
126
+ }
127
+ else {
128
+ const dv = new DataView(value, byteOffset, byteLength);
129
+ const lessOrEqual = encoding.endsWith('le');
130
+ let str = '';
131
+ for (let i = 0; i < dv.byteLength; i += 2) {
132
+ str += String.fromCharCode(dv.getUint16(i, lessOrEqual));
133
+ }
134
+ return str;
135
+ }
136
+ };
137
+ const typeStringEquator = Type.functionType(Type.Boolean, [Type.String, Type.String]);
138
+ const typeStringComparator = Type.functionType(Type.Boolean, [Type.String, Type.String, Type.OptionalInteger, Type.OptionalBoolean]);
139
+ const typeStringMutator = Type.functionType(Type.String, [Type.String]);
140
+ export const funcLike = new Constant((value1, value2) => equateStrings(value1, value2, true), typeStringEquator);
141
+ export const funcUnlike = new Constant((value1, value2) => !equateStrings(value1, value2, true), typeStringEquator);
142
+ export const funcContains = new Constant((value, search, start, ignoreCaseSpaceEtc) => containsString(value, search, start == null ? undefined : Number(start), ignoreCaseSpaceEtc), typeStringComparator);
143
+ export const funcStartsWith = new Constant((value, search, start, ignoreCaseSpaceEtc) => startsWithString(value, search, start == null ? undefined : Number(start), ignoreCaseSpaceEtc), typeStringComparator);
144
+ export const funcEndsWith = new Constant((value, search, end, ignoreCaseSpaceEtc) => endsWithString(value, search, end == null ? undefined : Number(end), ignoreCaseSpaceEtc), typeStringComparator);
145
+ export const funcChar = new Constant((value, pos) => value == null
146
+ ? undefined
147
+ : value.charAt(pos < 0 ? value.length + Number(pos) : Number(pos)), Type.functionType(Type.OptionalString, [Type.String, Type.Integer]));
148
+ export const funcCharCode = new Constant((value, pos) => value == null
149
+ ? undefined
150
+ : value.charCodeAt(pos < 0 ? value.length + Number(pos) : Number(pos)), Type.functionType(Type.OptionalInteger, [Type.String, Type.Float]));
151
+ export const funcTrim = new Constant((value) => value.trim(), typeStringMutator);
152
+ export const funcTrimStart = new Constant((value) => value.trimStart(), typeStringMutator);
153
+ export const funcTrimEnd = new Constant((value) => value.trimEnd(), typeStringMutator);
154
+ export const funcLowerCase = new Constant((value) => value.toLowerCase(), typeStringMutator);
155
+ export const funcUpperCase = new Constant((value) => value.toUpperCase(), typeStringMutator);
156
+ export const funcSplit = new Constant((value, separator = ' ') => value.split(separator), Type.functionType(Type.arrayType([Type.String]), [Type.String, Type.OptionalString]));
157
+ const funcAlphanum = new Constant((value) => {
158
+ const lowerCase = value.toLowerCase();
159
+ let result = '';
160
+ for (let i = 0; i < lowerCase.length; ++i) {
161
+ if (!isCaseSpaceEtc(value[i])) {
162
+ result += value[i];
163
+ }
164
+ }
165
+ return result;
166
+ }, typeStringMutator);
167
+ const funcRandomString = new Constant((value) => {
168
+ let str = '';
169
+ while (str.length < value) {
170
+ str += Math.random().toString(36).slice(2);
171
+ }
172
+ return str.slice(0, Number(value));
173
+ }, Type.functionType(Type.String, [Type.Integer]), false);
174
+ const funcDecodeString = new Constant((value, encoding = 'utf8', byteOffset, byteLength) => decodeString(value, encoding, byteOffset == null ? undefined : Number(byteOffset), byteLength == null ? undefined : Number(byteLength)), Type.functionType(Type.OptionalString, [Type.Buffer, Type.OptionalString, Type.OptionalInteger, Type.OptionalInteger]));
175
+ export const constString = {
176
+ Alphanum: funcAlphanum,
177
+ Random: funcRandomString,
178
+ Decode: funcDecodeString,
179
+ };