affinirum 1.4.0 → 1.4.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 (53) hide show
  1. package/README.md +66 -48
  2. package/dst/Affinirum.js +3 -3
  3. package/dst/Constants.js +2 -0
  4. package/dst/StaticScope.js +1 -1
  5. package/dst/Type.js +1 -1
  6. package/dst/atom/FunctionAtom.js +1 -1
  7. package/dst/atom/ObjectAtom.js +1 -1
  8. package/dst/cjs/Affinirum.js +3 -3
  9. package/dst/cjs/Constants.js +2 -0
  10. package/dst/cjs/StaticScope.js +1 -1
  11. package/dst/cjs/Type.js +1 -1
  12. package/dst/cjs/atom/FunctionAtom.js +1 -1
  13. package/dst/cjs/atom/ObjectAtom.js +1 -1
  14. package/dst/cjs/native/Array.js +3 -3
  15. package/dst/cjs/native/Boolean.js +7 -4
  16. package/dst/cjs/native/Buffer.js +45 -13
  17. package/dst/cjs/native/Float.js +4 -7
  18. package/dst/cjs/native/Integer.js +10 -7
  19. package/dst/cjs/native/Iterable.js +2 -1
  20. package/dst/cjs/native/Object.js +3 -3
  21. package/dst/cjs/native/String.js +34 -17
  22. package/dst/cjs/native/Timestamp.js +27 -13
  23. package/dst/cjs/native/Unknown.js +10 -10
  24. package/dst/cjs/native/notation/AN.js +6 -6
  25. package/dst/cjs/native/notation/IP.js +239 -0
  26. package/dst/cjs/native/notation/JSON.js +5 -5
  27. package/dst/cjs/node/ObjectNode.js +1 -1
  28. package/dst/native/Array.d.ts +1 -7
  29. package/dst/native/Array.js +3 -3
  30. package/dst/native/Boolean.d.ts +1 -7
  31. package/dst/native/Boolean.js +7 -4
  32. package/dst/native/Buffer.d.ts +4 -5
  33. package/dst/native/Buffer.js +43 -12
  34. package/dst/native/Float.d.ts +2 -21
  35. package/dst/native/Float.js +4 -7
  36. package/dst/native/Integer.d.ts +2 -10
  37. package/dst/native/Integer.js +10 -7
  38. package/dst/native/Iterable.js +2 -1
  39. package/dst/native/Object.d.ts +1 -3
  40. package/dst/native/Object.js +3 -3
  41. package/dst/native/String.d.ts +3 -7
  42. package/dst/native/String.js +34 -17
  43. package/dst/native/Timestamp.d.ts +4 -9
  44. package/dst/native/Timestamp.js +27 -13
  45. package/dst/native/Unknown.js +11 -11
  46. package/dst/native/notation/AN.d.ts +1 -4
  47. package/dst/native/notation/AN.js +6 -6
  48. package/dst/native/notation/IP.d.ts +1 -0
  49. package/dst/native/notation/IP.js +236 -0
  50. package/dst/native/notation/JSON.d.ts +1 -5
  51. package/dst/native/notation/JSON.js +5 -5
  52. package/dst/node/ObjectNode.js +1 -1
  53. package/package.json +1 -1
package/README.md CHANGED
@@ -129,71 +129,85 @@ Whitespace characters are ignored.
129
129
 
130
130
  ### Native Global Functions
131
131
 
132
+ Native conversions use the following null handling:
133
+
134
+ - `Format` returns an empty string for a **null**.
135
+ - `Encode` returns a zero-length buffer for a **null**.
136
+ - `Decode` and `Parse` return **null** when their input value is **null**.
137
+
138
+ Empty strings and zero-length buffers remain ordinary values; they are not serialized null markers.
139
+ Invalid non-null inputs are function-specific and may raise an evaluation error.
140
+
132
141
  #### Boolean
133
- - **Boolean.Or(values:...array):boolean** — Boolean disjunction
134
- - **Boolean.And(values: ...array):boolean** — Boolean conjunction
142
+ - **Boolean.Or(values: ...(boolean | array)):boolean** — Boolean disjunction of booleans and arrays of booleans
143
+ - **Boolean.And(values: ...(boolean | array)):boolean** — Boolean conjunction of booleans and arrays of booleans
135
144
  - **Boolean.Not(value: boolean):boolean** — Boolean negation
136
- - **Boolean.Decode(value: buffer, offset: integer?):boolean?** — Decode boolean from buffer
137
- - **Boolean.Parse():boolean?** — Parse boolean from string
145
+ - **Boolean.Decode(value: buffer?, offset: integer?):boolean?** — Decode a byte at the optional offset; missing or out-of-range input returns **void**
146
+ - **Boolean.Parse(value: string?):boolean?** — Parse case-insensitive `true` or `false`; other values return **void**
138
147
 
139
148
  #### Timestamp
140
149
  - **Timestamp.Now():timestamp** — Current date and time
141
150
  - **Timestamp.Epoch(value: float | integer, epoch: timestamp?):timestamp** — Create timestamp from milliseconds
142
- - **Timestamp.Decode(value: buffer, offset: integer?):timestamp?** — Decode timestamp from buffer
143
- - **Timestamp.Parse(value: string):timestamp?** — Parse string to timestamp
151
+ - **Timestamp.Decode(value: buffer?, encoding: string?, offset: integer?):timestamp?** — Decode a timestamp using `i64` (default) or `i64le`
152
+ - **Timestamp.Parse(value: string?):timestamp?** — Parse a string as a timestamp
144
153
 
145
154
  #### Float
146
155
  - **Float.NAN** — Not-a-number
147
156
  - **Float.PositiveInfinity** — Positive infinity
148
157
  - **Float.NegativeInfinity** — Negative infinity
149
158
  - **Float.Epsilon** — Smallest positive float
150
- - **Float.Sum(values: ...array):float** — Numeric sum
151
- - **Float.Min(values: ...array):float** — Numeric minimum
152
- - **Float.Max(values: ...array):float** — Numeric maximum
153
- - **Float.Exponent():float** — Exponent
154
- - **Float.Logarithm():float** — Logarithm
155
- - **Float.Abs():float** — Absolute value
156
- - **Float.Ceil():float** — Ceil
157
- - **Float.Floor():float** — Floor
158
- - **Float.Round():float** — Rounded value
159
- - **Float.Truncate():float** — Truncated value
159
+ - **Float.Sum(values: ...(float | array)):float** — Numeric sum
160
+ - **Float.Min(values: ...(float | array)):float** — Numeric minimum
161
+ - **Float.Max(values: ...(float | array)):float** — Numeric maximum
162
+ - **Float.Exponent(value: float):float** — Natural exponential
163
+ - **Float.Logarithm(value: float):float** — Natural logarithm
164
+ - **Float.Abs(value: float):float** — Absolute value
165
+ - **Float.Ceil(value: float):float** — Smallest integer greater than or equal to the value
166
+ - **Float.Floor(value: float):float** — Largest integer less than or equal to the value
167
+ - **Float.Round(value: float):float** — Rounded value
168
+ - **Float.Truncate(value: float):float** — Integer portion of the value
160
169
  - **Float.Random(exclusiveTo: float):float** — Random float up to value
161
- - **Float.Decode(value: buffer, encoding: string, offset: integer?):float?** — Decode float from buffer
162
- - **Float.Parse(value: string):float?** — Parse float from string
170
+ - **Float.Decode(value: buffer?, encoding: string?, offset: integer?):float?** — Decode using `f64` (default), `f64le`, `f32`, or `f32le`
171
+ - **Float.Parse(value: string?):float?** — Parse a string as a float
163
172
 
164
173
  #### Integer
165
- - **Integer.Sum(values: ...array):integer** — Numeric sum
166
- - **Integer.Min(values: ...array):integer** — Numeric minimum
167
- - **Integer.Max(values: ...array):integer** — Numeric maximum
174
+ - **Integer.Sum(values: ...(integer | array)):integer** — Numeric sum
175
+ - **Integer.Min(values: ...(integer | array)):integer** — Numeric minimum
176
+ - **Integer.Max(values: ...(integer | array)):integer** — Numeric maximum
168
177
  - **Integer.Random(exclusiveTo: integer):integer** — Random integer up to value
169
- - **Integer.Decode(value: buffer, encoding: string, offset: integer?):integer?** — Decode integer from buffer
170
- - **Integer.Parse(value: string):integer?** — Parse integer from string
178
+ - **Integer.Decode(value: buffer?, encoding: string?, offset: integer?):integer?** — Decode using signed `i8`, `i16`, `i16le`, `i32`, `i32le`, `i64` (default), or `i64le`, or unsigned `n8`, `n16`, `n16le`, `n32`, `n32le`, `n64`, or `n64le`
179
+ - **Integer.Parse(value: string?):integer?** — Parse a string as an integer
171
180
 
172
181
  #### String
182
+ - **String.Alphanum(value: string):string** — Keep only ASCII letters and digits
173
183
  - **String.Random(length: integer):string** — Random alphanumeric string
174
- - **String.Decode(value: buffer, encoding: string?, offset: integer?, length: integer?):string?** — Decode buffer to string
184
+ - **String.Decode(value: buffer?, encoding: string?, offset: integer?, length: integer?):string?** — Decode using `utf8` (default), `sbcs`, `ucs2`, or `ucs2le`
175
185
 
176
186
  #### Buffer
177
187
  - **Buffer.Random(length: integer):buffer** — Buffer of given length filled with random bytes
178
- - **Buffer.Parse(value: string):buffer?** — Parse buffer from hexadecimal string
188
+ - **Buffer.Parse(value: string?):buffer?** — Parse a hexadecimal string as a buffer
179
189
 
180
190
  #### Array
181
191
  - **Array.Join(values: ...array):array** — Join arrays of any depths into a single array
182
192
  - **Array.Range(inclusiveFrom: integer, exclusiveTo: integer):array** — New array filled with integers in range
183
- - **Array.Unique():array** — Array of unique values
193
+ - **Array.Unique(value: array):array** — Array of unique values
184
194
  - **Array.Intersection(a1: array, a2: array):array** — Common values from two arrays
185
195
  - **Array.Difference(a1: array, a2: array):array** — Symmetrical difference between arrays
186
196
 
187
197
  #### Object
188
- - **Object.Merge(values: ...array):object** — Merge multiple objects
198
+ - **Object.Merge(values: ...(object | array)):object** — Merge objects and arrays of objects
189
199
 
190
200
  #### AN
191
- - **AN.Format(value: ??, whitespace: string?):string** — Format string as AN
201
+ - **AN.Format(value: ??, whitespace: string?):string** — Format a value as AN notation
192
202
 
193
203
  #### JSON
194
- - **JSON.Format(value: (void | boolean | float | string | array | object), whitespace: string?):string** — Format as JSON string
195
- - **JSON.Parse(value: string):(void | boolean | float | string | array | object)** — Parse JSON-formatted string
204
+ - **JSON.Format(value: (void | boolean | timestamp | float | integer | string | array | object), whitespace: string?):string** — Format a value as JSON
205
+ - **JSON.Parse(value: string?):(void | boolean | timestamp | float | integer | string | array | object)** — Parse a JSON-formatted string; standard JSON values are not revived as timestamps or integers
196
206
 
207
+ #### IP
208
+ - **IP.Match(values: array, search: ...(string | array)):boolean** — Check whether any searched IPv4 or IPv6 address belongs to the supplied addresses or CIDR ranges
209
+ - **IP.Encode(value: string?):buffer** — Encode an IPv4, IPv6, or CIDR string as a network-byte-order buffer; malformed or **void** input returns a zero-length buffer
210
+ - **IP.Decode(value: buffer?):string?** — Decode a 4-, 5-, 16-, or 17-byte buffer as canonical IPv4, IPv6, or CIDR notation; malformed or **void** input returns **void**
197
211
 
198
212
  ### Native Method Functions
199
213
 
@@ -202,36 +216,36 @@ Whitespace characters are ignored.
202
216
  - **??.Equal(value: ??):boolean** — Equals to
203
217
  - **??.Unequal(value: ??):boolean** — Not equals to
204
218
  - **(boolean | buffer | function).Encode():buffer** — Encode value to buffer
205
- - **(timestamp | float | integer | string | array | object).Encode(encoding: string?):buffer** — Encode value to buffer
219
+ - **(void | timestamp | float | integer | string | array | object).Encode(encoding: string?):buffer** — Encode a value; defaults are `i64` for timestamps and integers, `f64` for floats, and `utf8` for strings
206
220
  - **(boolean | string | function).Format():string** — Format value to string
207
- - **(timestamp | float | integer | buffer | array | object).Format(formatting: string?):string** — Format value to string
221
+ - **(void | timestamp | float | integer | buffer | array | object).Format(formatting: string?):string** — Format a value; buffers use `hex` by default and also support `base64`
208
222
 
209
223
  #### Aggregable Functions
210
- - **float.Add(values: ...array):float** — Add or concatenate values
211
- - **integer.Add(values: ...array):integer** — Add or concatenate values
212
- - **buffer.Add(values: ...array):buffer** — Add or concatenate values
213
- - **string.Add(values: ...array):string** — Add or concatenate values
214
- - **array.Add(values: ...array):array** — Add or concatenate values
224
+ - **float.Add(value: integer):float** — Add an integer
225
+ - **integer.Add(value: float):float** — Add a float
226
+ - **integer.Add(value: integer):integer** — Add an integer
227
+ - **buffer.Add(value: buffer):buffer** — Concatenate buffers
228
+ - **string.Add(value: string):string** — Concatenate strings
229
+ - **array.Add(value: array):array** — Concatenate arrays
215
230
 
216
231
  #### Array Functions
217
232
  - **array.First(condition: function):??** — First item satisfying condition
218
233
  - **array.Last(condition: function):??** — Last item satisfying condition
219
- - **array.FirstIndex(condition: function):integer** — First index satisfying condition
220
- - **array.LastIndex(condition: function):integer** — Last index satisfying condition
234
+ - **array.FirstIndex(condition: function):integer?** — First index satisfying condition, or **void** if none does
235
+ - **array.LastIndex(condition: function):integer?** — Last index satisfying condition, or **void** if none does
221
236
  - **array.Every(condition: function):boolean** — All items satisfy condition
222
237
  - **array.Any(condition: function):boolean** — Any item satisfies condition
223
238
  - **array.Reverse():array** — Reversed array
224
239
  - **array.Flatten(depth: integer = 1):array** — Flatten array
225
240
  - **array.Derive(transformation: function):array** — Derived array
226
241
  - **array.Filter(condition: function):array** — Filtered array
227
- - **array.Reduce(reducer: function):??** — Reduced value
242
+ - **array.Reduce(reducer: function, initial: ?? = void):??** — Reduced value
228
243
  - **array.Compose(generator: function):object** — Compose object from array
229
- - **array.Prepend(items: ...array):array** — Prepend items
230
- - **array.Append(items: ...array):array** — Append items
244
+ - **array.Prepend(items: ...??):array** — Prepend items
245
+ - **array.Append(items: ...??):array** — Append items
231
246
 
232
247
  #### Buffer Functions
233
- - **buffer.Byte(pos: integer):buffer** — Byte at position
234
- - **Buffer.FormatBuffer(value: buffer):string** — Hexadecimal string from buffer
248
+ - **buffer.Byte(pos: integer):buffer?** — Byte at position, or **void** when unavailable
235
249
 
236
250
  #### Enumerable Functions
237
251
  - **buffer.Slice(start: integer?, end: integer?):buffer** — Slice section
@@ -264,8 +278,9 @@ Whitespace characters are ignored.
264
278
  - **integer.Subtract(subtrahend):integer** — Subtract value
265
279
  - **float.Negate():float** — Negate number
266
280
  - **integer.Negate():integer** — Negate number
267
- - **float.Multiply(values: ...array):float** — Multiply values
268
- - **integer.Multiply(values: ...array):integer** — Multiply values
281
+ - **float.Multiply(value: integer):float** — Multiply by an integer
282
+ - **integer.Multiply(value: float):float** — Multiply by a float
283
+ - **integer.Multiply(value: integer):integer** — Multiply by an integer
269
284
  - **float.Divide(divisor):float** — Divide value
270
285
  - **integer.Divide(divisor):integer** — Divide value
271
286
  - **float.Remainder(divisor):float** — Remainder
@@ -293,14 +308,13 @@ Whitespace characters are ignored.
293
308
  - **string.EndsWith(search: string, endPos: integer?, ignoreCaseSpaceEtc: boolean?):boolean** — Suffix check
294
309
  - **string.Char(pos: integer):string** — Character at position
295
310
  - **string.CharCode(pos: integer):integer** — Character code
296
- - **string.Alphanum():string** — Alphanumeric digest
297
311
  - **string.Trim():string** — Trim whitespace
298
312
  - **string.TrimStart():string** — Trim leading whitespace
299
313
  - **string.TrimEnd():string** — Trim trailing whitespace
300
314
  - **string.LowerCase():string** — To lowercase
301
315
  - **string.UpperCase():string** — To uppercase
302
316
  - **string.Split(separator: string = ' '):array** — Split into array
303
- - **string.ReplaceWith(replacement: string, search: ...array):string** — Replace all instances of substring with another
317
+ - **string.ReplaceWith(replacement: string, search: ...(string | array)):string** — Replace all instances of the searched strings
304
318
 
305
319
  #### Timestamp Functions
306
320
  - **timestamp.Year(utc: boolean?):integer** — Get year
@@ -313,6 +327,10 @@ Whitespace characters are ignored.
313
327
  - **timestamp.Second(utc: boolean?):integer** — Second
314
328
  - **timestamp.Millisecond(utc: boolean?):integer** — Millisecond
315
329
  - **timestamp.EpochTime(epoch: timestamp?):integer** — Milliseconds since epoch or 1970-01-01 00:00:00 UTC
330
+ - **timestamp.DaysSince(value: timestamp):float** — Difference from another timestamp in days
331
+ - **timestamp.HoursSince(value: timestamp):float** — Difference from another timestamp in hours
332
+ - **timestamp.MinutesSince(value: timestamp):float** — Difference from another timestamp in minutes
333
+ - **timestamp.SecondsSince(value: timestamp):float** — Difference from another timestamp in seconds
316
334
 
317
335
  ## Reference
318
336
 
package/dst/Affinirum.js CHANGED
@@ -43,8 +43,8 @@ export class Affinirum {
43
43
  this._script = script;
44
44
  this._strict = config?.strict ?? false;
45
45
  if (config?.variables) {
46
- for (const v in config.variables) {
47
- this._variables.set(v, new Variable(config.variables[v]));
46
+ for (const [vname, vtype] of Object.entries(config.variables)) {
47
+ this._variables.set(vname, new Variable(vtype));
48
48
  }
49
49
  }
50
50
  const state = new ParserState(this._script);
@@ -78,7 +78,7 @@ export class Affinirum {
78
78
  @returns Record with variable names and types.
79
79
  */
80
80
  variables() {
81
- const types = {};
81
+ const types = Object.create(null);
82
82
  const variables = this._scope.variables();
83
83
  for (const name in variables) {
84
84
  types[name] = variables[name].type;
package/dst/Constants.js CHANGED
@@ -7,6 +7,7 @@ import { constObject } from "./native/Object.js";
7
7
  import { constString } from "./native/String.js";
8
8
  import { constTimestamp } from "./native/Timestamp.js";
9
9
  import { constAN } from "./native/notation/AN.js";
10
+ import { constIP } from "./native/notation/IP.js";
10
11
  import { constJSON } from "./native/notation/JSON.js";
11
12
  export const Constants = [
12
13
  ["Array", constArray],
@@ -18,5 +19,6 @@ export const Constants = [
18
19
  ["String", constString],
19
20
  ["Timestamp", constTimestamp],
20
21
  ["AN", constAN],
22
+ ["IP", constIP],
21
23
  ["JSON", constJSON],
22
24
  ];
@@ -33,7 +33,7 @@ export class StaticScope {
33
33
  return scope;
34
34
  }
35
35
  variables() {
36
- const variables = {};
36
+ const variables = Object.create(null);
37
37
  for (const [name, variable] of this._variables) {
38
38
  if (!this._locals.has(name)) {
39
39
  variables[name] = variable;
package/dst/Type.js CHANGED
@@ -141,7 +141,7 @@ export class Type {
141
141
  static arrayType(itemTypes = []) {
142
142
  return new Type([new ArrayAtom(itemTypes)]);
143
143
  }
144
- static objectType(propTypes = {}) {
144
+ static objectType(propTypes = Object.create(null)) {
145
145
  return new Type([new ObjectAtom(propTypes)]);
146
146
  }
147
147
  static functionType(retType = Type.Unknown, argTypes = [], isVariadic = false) {
@@ -26,7 +26,7 @@ export class FunctionAtom {
26
26
  return this._retType.isUnknown && this._argTypes.length === 0 && this._isVariadic;
27
27
  }
28
28
  get retType() {
29
- return Type.Unknown;
29
+ return this._retType;
30
30
  }
31
31
  get minArity() {
32
32
  return this._minArity;
@@ -1,6 +1,6 @@
1
1
  export class ObjectAtom {
2
2
  _valTypes;
3
- constructor(_valTypes = {}) {
3
+ constructor(_valTypes = Object.create(null)) {
4
4
  this._valTypes = _valTypes;
5
5
  }
6
6
  get _undefined() {
@@ -46,8 +46,8 @@ class Affinirum {
46
46
  this._script = script;
47
47
  this._strict = config?.strict ?? false;
48
48
  if (config?.variables) {
49
- for (const v in config.variables) {
50
- this._variables.set(v, new Variable_js_1.Variable(config.variables[v]));
49
+ for (const [vname, vtype] of Object.entries(config.variables)) {
50
+ this._variables.set(vname, new Variable_js_1.Variable(vtype));
51
51
  }
52
52
  }
53
53
  const state = new ParserState_js_1.ParserState(this._script);
@@ -81,7 +81,7 @@ class Affinirum {
81
81
  @returns Record with variable names and types.
82
82
  */
83
83
  variables() {
84
- const types = {};
84
+ const types = Object.create(null);
85
85
  const variables = this._scope.variables();
86
86
  for (const name in variables) {
87
87
  types[name] = variables[name].type;
@@ -10,6 +10,7 @@ const Object_js_1 = require("./native/Object.js");
10
10
  const String_js_1 = require("./native/String.js");
11
11
  const Timestamp_js_1 = require("./native/Timestamp.js");
12
12
  const AN_js_1 = require("./native/notation/AN.js");
13
+ const IP_js_1 = require("./native/notation/IP.js");
13
14
  const JSON_js_1 = require("./native/notation/JSON.js");
14
15
  exports.Constants = [
15
16
  ["Array", Array_js_1.constArray],
@@ -21,5 +22,6 @@ exports.Constants = [
21
22
  ["String", String_js_1.constString],
22
23
  ["Timestamp", Timestamp_js_1.constTimestamp],
23
24
  ["AN", AN_js_1.constAN],
25
+ ["IP", IP_js_1.constIP],
24
26
  ["JSON", JSON_js_1.constJSON],
25
27
  ];
@@ -36,7 +36,7 @@ class StaticScope {
36
36
  return scope;
37
37
  }
38
38
  variables() {
39
- const variables = {};
39
+ const variables = Object.create(null);
40
40
  for (const [name, variable] of this._variables) {
41
41
  if (!this._locals.has(name)) {
42
42
  variables[name] = variable;
package/dst/cjs/Type.js CHANGED
@@ -144,7 +144,7 @@ class Type {
144
144
  static arrayType(itemTypes = []) {
145
145
  return new Type([new ArrayAtom_js_1.ArrayAtom(itemTypes)]);
146
146
  }
147
- static objectType(propTypes = {}) {
147
+ static objectType(propTypes = Object.create(null)) {
148
148
  return new Type([new ObjectAtom_js_1.ObjectAtom(propTypes)]);
149
149
  }
150
150
  static functionType(retType = Type.Unknown, argTypes = [], isVariadic = false) {
@@ -29,7 +29,7 @@ class FunctionAtom {
29
29
  return this._retType.isUnknown && this._argTypes.length === 0 && this._isVariadic;
30
30
  }
31
31
  get retType() {
32
- return Type_js_1.Type.Unknown;
32
+ return this._retType;
33
33
  }
34
34
  get minArity() {
35
35
  return this._minArity;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ObjectAtom = void 0;
4
4
  class ObjectAtom {
5
5
  _valTypes;
6
- constructor(_valTypes = {}) {
6
+ constructor(_valTypes = Object.create(null)) {
7
7
  this._valTypes = _valTypes;
8
8
  }
9
9
  get _undefined() {
@@ -53,7 +53,7 @@ exports.funcCompose = new Constant_js_1.Constant((value, callback) => {
53
53
  if (value == null) {
54
54
  return undefined;
55
55
  }
56
- const obj = {};
56
+ const obj = Object.create(null);
57
57
  for (let i = 0; i < value.length; ++i) {
58
58
  const key = value[i];
59
59
  obj[key] = callback(obj, key, BigInt(i), value);
@@ -85,10 +85,10 @@ const funcUnique = new Constant_js_1.Constant((value) => {
85
85
  }, Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array]));
86
86
  const funcIntersection = new Constant_js_1.Constant((value1, value2) => value1?.filter((i) => value2?.some((v) => (0, Unknown_js_1.equate)(v, i))) ?? [], typeArrayOperator);
87
87
  const funcDifference = new Constant_js_1.Constant((value1, value2) => [...value1?.filter((i) => value2?.every((v) => !(0, Unknown_js_1.equate)(v, i))) ?? [], ...value2?.filter((i) => value1?.every((v) => !(0, Unknown_js_1.equate)(v, i))) ?? []], typeArrayOperator);
88
- exports.constArray = {
88
+ exports.constArray = Object.assign(Object.create(null), {
89
89
  Join: funcJoin,
90
90
  Range: funcRange,
91
91
  Unique: funcUnique,
92
92
  Intersection: funcIntersection,
93
93
  Difference: funcDifference,
94
- };
94
+ });
@@ -8,18 +8,21 @@ const typeBooleanLogic = Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [ty
8
8
  exports.funcOr = new Constant_js_1.Constant((...values) => values.flat().some((v) => v), typeBooleanLogic);
9
9
  exports.funcAnd = new Constant_js_1.Constant((...values) => values.flat().every((v) => v), typeBooleanLogic);
10
10
  exports.funcNot = new Constant_js_1.Constant((value) => !value, Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.Boolean]));
11
- const funcDecodeBoolean = new Constant_js_1.Constant((value, byteOffset) => value ? Boolean(new Uint8Array(value)[byteOffset == null ? 0 : Number(byteOffset)]) : undefined, Type_js_1.Type.functionType(Type_js_1.Type.OptionalBoolean, [Type_js_1.Type.Buffer, Type_js_1.Type.OptionalInteger]));
11
+ const funcDecodeBoolean = new Constant_js_1.Constant((value, byteOffset) => {
12
+ const offset = byteOffset == null ? 0 : Number(byteOffset);
13
+ return value && offset >= 0 && value.byteLength > offset ? Boolean(new Uint8Array(value)[offset]) : undefined;
14
+ }, Type_js_1.Type.functionType(Type_js_1.Type.OptionalBoolean, [Type_js_1.Type.OptionalBuffer, Type_js_1.Type.OptionalInteger]));
12
15
  const funcParseBoolean = new Constant_js_1.Constant((value) => {
13
16
  if (value == null) {
14
17
  return undefined;
15
18
  }
16
19
  const v = value.toLowerCase();
17
20
  return v === "true" ? true : v === "false" ? false : undefined;
18
- }, Type_js_1.Type.functionType(Type_js_1.Type.OptionalBoolean, [Type_js_1.Type.String]));
19
- exports.constBoolean = {
21
+ }, Type_js_1.Type.functionType(Type_js_1.Type.OptionalBoolean, [Type_js_1.Type.OptionalString]));
22
+ exports.constBoolean = Object.assign(Object.create(null), {
20
23
  Or: exports.funcOr,
21
24
  And: exports.funcAnd,
22
25
  Not: exports.funcNot,
23
26
  Decode: funcDecodeBoolean,
24
27
  Parse: funcParseBoolean,
25
- };
28
+ });
@@ -1,8 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.constBuffer = exports.funcByte = exports.parseBuffer = exports.formatBuffer = exports.concatBuffers = exports.containsBuffer = exports.equateBuffers = void 0;
3
+ exports.constBuffer = exports.funcByte = exports.parseBuffer = exports.formatBuffer = exports.concatBuffers = exports.containsBuffer = exports.equateBuffers = exports.parseBoundedInteger = exports.EmptyBuffer = void 0;
4
4
  const Constant_js_1 = require("../Constant.js");
5
5
  const Type_js_1 = require("../Type.js");
6
+ exports.EmptyBuffer = new ArrayBuffer(0);
7
+ const ZERO = "0".charCodeAt(0), NINE = "9".charCodeAt(0);
8
+ const UPPER_A = "A".charCodeAt(0), UPPER_F = "F".charCodeAt(0);
9
+ const LOWER_A = "a".charCodeAt(0), LOWER_F = "f".charCodeAt(0);
10
+ const parseBoundedInteger = (input, base, min, max) => {
11
+ if (input.length === 0) {
12
+ throw new Error(`Invalid IP component: ${input}`);
13
+ }
14
+ let value = 0;
15
+ for (let i = 0; i < input.length; i++) {
16
+ const code = input.charCodeAt(i);
17
+ let digit;
18
+ if (code >= ZERO && code <= NINE) {
19
+ digit = code - ZERO;
20
+ }
21
+ else if (base === 16 && code >= UPPER_A && code <= UPPER_F) {
22
+ digit = code - UPPER_A + 10;
23
+ }
24
+ else if (base === 16 && code >= LOWER_A && code <= LOWER_F) {
25
+ digit = code - LOWER_A + 10;
26
+ }
27
+ else {
28
+ throw new Error(`Invalid IP component: ${input}`);
29
+ }
30
+ value = value * base + digit;
31
+ }
32
+ if (value < min || value > max) {
33
+ throw new Error(`Invalid IP component: ${input}`);
34
+ }
35
+ return value;
36
+ };
37
+ exports.parseBoundedInteger = parseBoundedInteger;
6
38
  const equateBuffers = (value1, value2) => {
7
39
  if (value1.byteLength !== value2.byteLength) {
8
40
  return false;
@@ -73,9 +105,6 @@ const concatBuffers = (value1, value2) => {
73
105
  };
74
106
  exports.concatBuffers = concatBuffers;
75
107
  const formatBuffer = (value, formatting = "hex") => {
76
- if (value == null) {
77
- return "";
78
- }
79
108
  if (formatting === "base64") {
80
109
  let binary = "";
81
110
  const bytes = new Uint8Array(value);
@@ -86,12 +115,15 @@ const formatBuffer = (value, formatting = "hex") => {
86
115
  }
87
116
  return btoa(binary);
88
117
  }
89
- const bytes = new Uint8Array(value);
90
- let str = "";
91
- for (let i = 0; i < bytes.byteLength; ++i) {
92
- str += bytes[i].toString(16).padStart(2, "0");
118
+ else if (formatting === "hex") {
119
+ const bytes = new Uint8Array(value);
120
+ let str = "";
121
+ for (let i = 0; i < bytes.byteLength; ++i) {
122
+ str += bytes[i].toString(16).padStart(2, "0");
123
+ }
124
+ return str;
93
125
  }
94
- return str;
126
+ throw new Error(`${formatting} formatting not supported`);
95
127
  };
96
128
  exports.formatBuffer = formatBuffer;
97
129
  const parseBuffer = (value) => {
@@ -100,7 +132,7 @@ const parseBuffer = (value) => {
100
132
  }
101
133
  const bytes = new Uint8Array(Math.ceil(value.length / 2));
102
134
  for (let i = 0, c = 0; c < value.length; ++i) {
103
- bytes[i] = Number.parseInt(value.slice(c, c += 2), 16);
135
+ bytes[i] = (0, exports.parseBoundedInteger)(value.slice(c, c += 2), 16, 0, 0xff);
104
136
  }
105
137
  return bytes.buffer;
106
138
  };
@@ -109,8 +141,8 @@ exports.funcByte = new Constant_js_1.Constant((value, pos) => value == null
109
141
  ? undefined
110
142
  : value.slice(Number(pos), Number(pos) + 1), Type_js_1.Type.functionType(Type_js_1.Type.OptionalBuffer, [Type_js_1.Type.Buffer, Type_js_1.Type.Integer]));
111
143
  const funcRandomBuffer = new Constant_js_1.Constant((value) => value == null || value <= 0n ? new Uint8Array(0).buffer : crypto.getRandomValues(new Uint8Array(Number(value))).buffer, Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Integer]), false);
112
- const funcParseBuffer = new Constant_js_1.Constant((value) => (0, exports.parseBuffer)(value), Type_js_1.Type.functionType(Type_js_1.Type.OptionalBuffer, [Type_js_1.Type.String]));
113
- exports.constBuffer = {
144
+ const funcParseBuffer = new Constant_js_1.Constant((value) => (0, exports.parseBuffer)(value), Type_js_1.Type.functionType(Type_js_1.Type.OptionalBuffer, [Type_js_1.Type.OptionalString]));
145
+ exports.constBuffer = Object.assign(Object.create(null), {
114
146
  Random: funcRandomBuffer,
115
147
  Parse: funcParseBuffer,
116
- };
148
+ });
@@ -4,9 +4,6 @@ exports.constFloat = exports.encodeFloat = void 0;
4
4
  const Constant_js_1 = require("../Constant.js");
5
5
  const Type_js_1 = require("../Type.js");
6
6
  const encodeFloat = (value, encoding = "f64") => {
7
- if (value == null) {
8
- return new Uint8Array(0).buffer;
9
- }
10
7
  let bits = "";
11
8
  for (let i = 0; i < encoding.length; ++i) {
12
9
  const c = encoding[i];
@@ -60,9 +57,9 @@ const funcFloor = new Constant_js_1.Constant((value) => Math.floor(Number(value)
60
57
  const funcRound = new Constant_js_1.Constant((value) => Math.round(Number(value)), typeTransform);
61
58
  const funcTruncate = new Constant_js_1.Constant((value) => Math.trunc(Number(value)), typeTransform);
62
59
  const funcRandomFloat = new Constant_js_1.Constant((value) => value == null ? undefined : Math.random() * Number(value), Type_js_1.Type.functionType(Type_js_1.Type.Float, [Type_js_1.Type.Float]), false);
63
- const funcDecodeFloat = new Constant_js_1.Constant((value, encoding = "f64", byteOffset) => decodeFloat(value, encoding, byteOffset == null ? undefined : Number(byteOffset)), Type_js_1.Type.functionType(Type_js_1.Type.OptionalFloat, [Type_js_1.Type.Buffer, Type_js_1.Type.OptionalString, Type_js_1.Type.OptionalInteger]));
64
- const funcParseFloat = new Constant_js_1.Constant((value) => value ? Number.parseFloat(value) : undefined, Type_js_1.Type.functionType(Type_js_1.Type.OptionalFloat, [Type_js_1.Type.String]));
65
- exports.constFloat = {
60
+ const funcDecodeFloat = new Constant_js_1.Constant((value, encoding = "f64", byteOffset) => decodeFloat(value, encoding, byteOffset == null ? undefined : Number(byteOffset)), Type_js_1.Type.functionType(Type_js_1.Type.OptionalFloat, [Type_js_1.Type.OptionalBuffer, Type_js_1.Type.OptionalString, Type_js_1.Type.OptionalInteger]));
61
+ const funcParseFloat = new Constant_js_1.Constant((value) => value ? Number.parseFloat(value) : undefined, Type_js_1.Type.functionType(Type_js_1.Type.OptionalFloat, [Type_js_1.Type.OptionalString]));
62
+ exports.constFloat = Object.assign(Object.create(null), {
66
63
  NAN: new Constant_js_1.Constant(Number.NaN),
67
64
  PositiveInfinity: new Constant_js_1.Constant(Number.POSITIVE_INFINITY),
68
65
  NegativeInfinity: new Constant_js_1.Constant(Number.NEGATIVE_INFINITY),
@@ -80,4 +77,4 @@ exports.constFloat = {
80
77
  Random: funcRandomFloat,
81
78
  Decode: funcDecodeFloat,
82
79
  Parse: funcParseFloat,
83
- };
80
+ });
@@ -4,9 +4,6 @@ exports.constInteger = exports.encodeInteger = void 0;
4
4
  const Constant_js_1 = require("../Constant.js");
5
5
  const Type_js_1 = require("../Type.js");
6
6
  const encodeInteger = (value, encoding = "i64") => {
7
- if (value == null) {
8
- return new Uint8Array(0).buffer;
9
- }
10
7
  let bits = "";
11
8
  for (let i = 0; i < encoding.length; ++i) {
12
9
  const c = encoding[i];
@@ -92,13 +89,19 @@ const funcSum = new Constant_js_1.Constant((...values) => BigInt.asIntN(64, valu
92
89
  const funcMin = new Constant_js_1.Constant((...values) => BigInt.asIntN(64, values.flat().reduce((min, val) => val < min ? val : min)), typeAggregator);
93
90
  const funcMax = new Constant_js_1.Constant((...values) => BigInt.asIntN(64, values.flat().reduce((max, val) => val > max ? val : max)), typeAggregator);
94
91
  const funcRandomInteger = new Constant_js_1.Constant((value) => value == null ? undefined : BigInt.asIntN(64, BigInt(Math.floor(Math.random() * Number(value)))), Type_js_1.Type.functionType(Type_js_1.Type.Integer, [Type_js_1.Type.Integer]), false);
95
- const funcDecodeInteger = new Constant_js_1.Constant((value, encoding = "i64", byteOffset) => decodeInteger(value, encoding, byteOffset == null ? undefined : Number(byteOffset)), Type_js_1.Type.functionType(Type_js_1.Type.OptionalInteger, [Type_js_1.Type.Buffer, Type_js_1.Type.OptionalString, Type_js_1.Type.OptionalInteger]));
96
- const funcParseInteger = new Constant_js_1.Constant((value) => value ? BigInt.asIntN(64, BigInt(value)) : undefined, Type_js_1.Type.functionType(Type_js_1.Type.OptionalInteger, [Type_js_1.Type.String]));
97
- exports.constInteger = {
92
+ const funcDecodeInteger = new Constant_js_1.Constant((value, encoding = "i64", byteOffset) => decodeInteger(value, encoding, byteOffset == null ? undefined : Number(byteOffset)), Type_js_1.Type.functionType(Type_js_1.Type.OptionalInteger, [Type_js_1.Type.OptionalBuffer, Type_js_1.Type.OptionalString, Type_js_1.Type.OptionalInteger]));
93
+ const funcParseInteger = new Constant_js_1.Constant((value) => {
94
+ try {
95
+ return value ? BigInt.asIntN(64, BigInt(value)) : undefined;
96
+ }
97
+ catch { }
98
+ return undefined;
99
+ }, Type_js_1.Type.functionType(Type_js_1.Type.OptionalInteger, [Type_js_1.Type.OptionalString]));
100
+ exports.constInteger = Object.assign(Object.create(null), {
98
101
  Sum: funcSum,
99
102
  Min: funcMin,
100
103
  Max: funcMax,
101
104
  Random: funcRandomInteger,
102
105
  Decode: funcDecodeInteger,
103
106
  Parse: funcParseInteger,
104
- };
107
+ });
@@ -29,7 +29,8 @@ exports.funcAt = new Constant_js_1.Constant((value, index) => {
29
29
  const ix = Number(index);
30
30
  return value[ix < 0 ? value.length + ix : ix];
31
31
  }
32
- return value[String(index)];
32
+ const key = String(index);
33
+ return Object.hasOwn(value, key) ? value[key] : undefined;
33
34
  }, Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Unknown, [Type_js_1.Type.Array, Type_js_1.Type.Integer]), Type_js_1.Type.functionType(Type_js_1.Type.Unknown, [Type_js_1.Type.Object, Type_js_1.Type.String])));
34
35
  exports.funcHas = new Constant_js_1.Constant((value, index) => {
35
36
  if (value == null) {
@@ -7,7 +7,7 @@ const typeAccessor = Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_
7
7
  exports.funcEntries = new Constant_js_1.Constant((value) => value ? Object.entries(value) : undefined, typeAccessor);
8
8
  exports.funcKeys = new Constant_js_1.Constant((value) => value ? Object.keys(value) : undefined, Type_js_1.Type.functionType(Type_js_1.Type.arrayType([Type_js_1.Type.String]), [Type_js_1.Type.Object]));
9
9
  exports.funcValues = new Constant_js_1.Constant((value) => value ? Object.values(value) : undefined, typeAccessor);
10
- const funcMerge = new Constant_js_1.Constant((...values) => values.flat().reduce((acc, val) => Object.assign(acc, val), {}), Type_js_1.Type.functionType(Type_js_1.Type.Object, [Type_js_1.Type.union(Type_js_1.Type.arrayType([Type_js_1.Type.Object]), Type_js_1.Type.Object)], true));
11
- exports.constObject = {
10
+ const funcMerge = new Constant_js_1.Constant((...values) => values.flat().reduce((acc, val) => Object.assign(acc, val), Object.create(null)), Type_js_1.Type.functionType(Type_js_1.Type.Object, [Type_js_1.Type.union(Type_js_1.Type.arrayType([Type_js_1.Type.Object]), Type_js_1.Type.Object)], true));
11
+ exports.constObject = Object.assign(Object.create(null), {
12
12
  Merge: funcMerge,
13
- };
13
+ });