affinirum 1.4.2 → 1.5.1

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 (89) hide show
  1. package/README.md +226 -154
  2. package/dst/Affinirum.js +24 -15
  3. package/dst/Atom.d.ts +3 -1
  4. package/dst/Constant.js +1 -1
  5. package/dst/Functions.js +7 -4
  6. package/dst/Keywords.js +4 -4
  7. package/dst/ParserState.d.ts +2 -1
  8. package/dst/ParserState.js +43 -82
  9. package/dst/Type.d.ts +11 -7
  10. package/dst/Type.js +113 -38
  11. package/dst/Value.d.ts +1 -0
  12. package/dst/Value.js +19 -1
  13. package/dst/atom/ArrayAtom.d.ts +7 -3
  14. package/dst/atom/ArrayAtom.js +53 -8
  15. package/dst/atom/FunctionAtom.d.ts +4 -1
  16. package/dst/atom/FunctionAtom.js +65 -15
  17. package/dst/atom/ObjectAtom.d.ts +7 -5
  18. package/dst/atom/ObjectAtom.js +35 -13
  19. package/dst/atom/PrimitiveAtom.d.ts +3 -1
  20. package/dst/atom/PrimitiveAtom.js +10 -1
  21. package/dst/cjs/Affinirum.js +24 -15
  22. package/dst/cjs/Constant.js +1 -1
  23. package/dst/cjs/Functions.js +6 -3
  24. package/dst/cjs/Keywords.js +4 -4
  25. package/dst/cjs/ParserState.js +42 -81
  26. package/dst/cjs/Type.js +113 -38
  27. package/dst/cjs/Value.js +21 -0
  28. package/dst/cjs/atom/ArrayAtom.js +53 -8
  29. package/dst/cjs/atom/FunctionAtom.js +65 -15
  30. package/dst/cjs/atom/ObjectAtom.js +35 -13
  31. package/dst/cjs/atom/PrimitiveAtom.js +10 -1
  32. package/dst/cjs/native/Aggregable.js +3 -2
  33. package/dst/cjs/native/Array.js +10 -8
  34. package/dst/cjs/native/Boolean.js +8 -5
  35. package/dst/cjs/native/Buffer.js +32 -3
  36. package/dst/cjs/native/Enumerable.js +7 -4
  37. package/dst/cjs/native/Float.js +26 -13
  38. package/dst/cjs/native/Integer.js +64 -28
  39. package/dst/cjs/native/Iterable.js +8 -5
  40. package/dst/cjs/native/Number.js +62 -33
  41. package/dst/cjs/native/Object.js +2 -1
  42. package/dst/cjs/native/String.js +71 -24
  43. package/dst/cjs/native/Timestamp.js +76 -18
  44. package/dst/cjs/native/Unknown.js +1 -1
  45. package/dst/cjs/native/notation/AN.js +152 -11
  46. package/dst/cjs/native/notation/IP.js +4 -3
  47. package/dst/cjs/native/notation/JSON.js +1 -1
  48. package/dst/cjs/node/ArrayNode.js +6 -6
  49. package/dst/cjs/node/BlockNode.js +5 -4
  50. package/dst/cjs/node/CallNode.js +28 -22
  51. package/dst/cjs/node/CastNode.js +42 -0
  52. package/dst/cjs/node/ObjectNode.js +2 -4
  53. package/dst/cjs/node/SwitchNode.js +10 -10
  54. package/dst/cjs/node/VariableNode.js +3 -1
  55. package/dst/native/Aggregable.js +3 -2
  56. package/dst/native/Array.js +10 -8
  57. package/dst/native/Boolean.d.ts +1 -0
  58. package/dst/native/Boolean.js +7 -4
  59. package/dst/native/Buffer.d.ts +2 -0
  60. package/dst/native/Buffer.js +29 -2
  61. package/dst/native/Enumerable.js +7 -4
  62. package/dst/native/Float.d.ts +2 -0
  63. package/dst/native/Float.js +25 -12
  64. package/dst/native/Integer.d.ts +5 -1
  65. package/dst/native/Integer.js +61 -27
  66. package/dst/native/Iterable.js +8 -5
  67. package/dst/native/Number.d.ts +4 -3
  68. package/dst/native/Number.js +57 -32
  69. package/dst/native/Object.js +2 -1
  70. package/dst/native/String.d.ts +2 -7
  71. package/dst/native/String.js +68 -16
  72. package/dst/native/Timestamp.d.ts +4 -2
  73. package/dst/native/Timestamp.js +73 -17
  74. package/dst/native/Unknown.js +1 -1
  75. package/dst/native/notation/AN.d.ts +1 -0
  76. package/dst/native/notation/AN.js +152 -12
  77. package/dst/native/notation/IP.js +4 -3
  78. package/dst/native/notation/JSON.js +1 -1
  79. package/dst/node/ArrayNode.d.ts +1 -1
  80. package/dst/node/ArrayNode.js +6 -6
  81. package/dst/node/BlockNode.js +5 -4
  82. package/dst/node/CallNode.js +28 -22
  83. package/dst/node/CastNode.d.ts +15 -0
  84. package/dst/node/CastNode.js +38 -0
  85. package/dst/node/ObjectNode.d.ts +1 -1
  86. package/dst/node/ObjectNode.js +2 -4
  87. package/dst/node/SwitchNode.js +10 -10
  88. package/dst/node/VariableNode.js +3 -1
  89. package/package.json +5 -4
package/dst/Affinirum.js CHANGED
@@ -6,6 +6,7 @@ import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, fun
6
6
  import { funcCoalesce, funcEqual, funcNotEqual } from "./native/Unknown.js";
7
7
  import { Constant } from "./Constant.js";
8
8
  import { Variable } from "./Variable.js";
9
+ import { normalize } from "./Value.js";
9
10
  import { Type } from "./Type.js";
10
11
  import { Keywords } from "./Keywords.js";
11
12
  import { Constants } from "./Constants.js";
@@ -13,6 +14,7 @@ import { Functions } from "./Functions.js";
13
14
  import { ArrayNode } from "./node/ArrayNode.js";
14
15
  import { BlockNode } from "./node/BlockNode.js";
15
16
  import { CallNode } from "./node/CallNode.js";
17
+ import { CastNode } from "./node/CastNode.js";
16
18
  import { ConstantNode } from "./node/ConstantNode.js";
17
19
  import { JumpNode } from "./node/JumpNode.js";
18
20
  import { LoopNode } from "./node/LoopNode.js";
@@ -93,23 +95,23 @@ export class Affinirum {
93
95
  evaluate(values) {
94
96
  const variables = this._scope.variables();
95
97
  for (const name in variables) {
96
- if (!Object.prototype.hasOwnProperty.call(values, name)) {
98
+ if (!values || !Object.hasOwn(values, name)) {
97
99
  this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
98
100
  }
99
101
  const variable = variables[name];
100
- const value = values?.[name] ?? undefined;
101
- if (!variable.type.match(Type.of(value))) {
102
+ const value = normalize(values?.[name] ?? undefined);
103
+ if (!variable.type.acceptValue(value)) {
102
104
  this._varframes.get(name)?.throwError(`unexpected type ${Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
103
105
  }
104
106
  variable.value = value;
105
107
  }
106
108
  try {
107
- return this._root.evaluate();
109
+ return normalize(this._root.evaluate() ?? undefined);
108
110
  }
109
111
  catch (e) {
110
112
  if (e instanceof JumpException) {
111
113
  if (e.jump === "exit") {
112
- return e.value;
114
+ return normalize(e.value ?? undefined);
113
115
  }
114
116
  else {
115
117
  throw this._root.throwError(`unexpected ${e.jump} jump`);
@@ -235,9 +237,9 @@ export class Affinirum {
235
237
  return node;
236
238
  }
237
239
  _accessor(state, scope) {
240
+ const frame = state.starts();
238
241
  let node = this._term(state, scope);
239
- while (state.isDot || state.isQuestion || state.isParenthesesOpen || state.isBracketsOpen) {
240
- const frame = state.starts();
242
+ while (state.isDot || state.isQuestion || state.isParenthesesOpen || state.isBracketsOpen || state.isCast) {
241
243
  if (state.isDot || state.isQuestion) { // property access or property existance operator
242
244
  const operator = state.isDot ? funcAt : funcHas;
243
245
  if (state.next().isLiteral && (typeof state.literal.value === "string" || typeof state.literal.value === "bigint")) {
@@ -283,13 +285,20 @@ export class Affinirum {
283
285
  break;
284
286
  }
285
287
  }
288
+ state.closeParentheses();
286
289
  node = new CallNode(frame.ends(state), node, subnodes);
287
- state.closeParentheses().next();
290
+ state.next();
288
291
  }
289
292
  else if (state.isBracketsOpen) { // index access operator
290
- const fnode = new ConstantNode(frame, funcAt);
291
- node = new CallNode(frame, fnode, [node, this._unit(state.next(), scope)]);
292
- state.closeBrackets().next();
293
+ const subnodes = [node, this._unit(state.next(), scope)];
294
+ state.closeBrackets();
295
+ const fnode = new ConstantNode(frame.ends(state), funcAt);
296
+ node = new CallNode(frame, fnode, subnodes);
297
+ state.next();
298
+ }
299
+ else if (state.isCast) { // type cast operator
300
+ const type = this._type(state.next(), scope);
301
+ node = new CastNode(frame.ends(state), node, type);
293
302
  }
294
303
  }
295
304
  return node;
@@ -462,8 +471,8 @@ export class Affinirum {
462
471
  state.throwError("unexpected expression token");
463
472
  }
464
473
  _function(state, scope) {
465
- state.next().openParentheses();
466
474
  const frame = state.starts();
475
+ state.next().openParentheses();
467
476
  let variadic = false;
468
477
  const variables = new Map();
469
478
  while (!state.next().isParenthesesClose) {
@@ -501,8 +510,8 @@ export class Affinirum {
501
510
  const args = Array.from(variables.values());
502
511
  const subnode = this._unit(state, scope.subscope(variables));
503
512
  const func = (...values) => {
504
- args.forEach((arg, ix) => arg.value = values[ix]);
505
- return subnode.evaluate();
513
+ args.forEach((arg, ix) => arg.value = normalize(values[ix]));
514
+ return normalize(subnode.evaluate());
506
515
  };
507
516
  const constant = new Constant(func, Type.functionType(retType, args.map((v) => v.type), variadic));
508
517
  return new ConstantNode(frame, constant, subnode);
@@ -554,7 +563,7 @@ export class Affinirum {
554
563
  }
555
564
  state.closeBrackets().next();
556
565
  return colon
557
- ? Type.objectType(Object.fromEntries(itemKeyTypes.map(([key, type]) => [key, type])))
566
+ ? Type.objectType(itemKeyTypes.map(([key, type]) => [key, type]))
558
567
  : Type.arrayType(itemKeyTypes.map(([, v]) => v));
559
568
  }
560
569
  else if (state.isTilda) { // function type
package/dst/Atom.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export interface IAtom {
2
- match(atom: IAtom): boolean;
2
+ equate(atom: IAtom): boolean;
3
+ accept(atom: IAtom): boolean;
4
+ intersect(atom: IAtom): boolean;
3
5
  weight(): number;
4
6
  toString(): string;
5
7
  }
package/dst/Constant.js CHANGED
@@ -12,7 +12,7 @@ export class Constant {
12
12
  return this._value;
13
13
  }
14
14
  get type() {
15
- return Type.isPrimitiveType(this._value) ? Type.of(this._value) : this._type;
15
+ return Type.isPrimitive(this._value) ? Type.of(this._value) : this._type;
16
16
  }
17
17
  set type(type) {
18
18
  this._type = type;
package/dst/Functions.js CHANGED
@@ -1,9 +1,12 @@
1
1
  import { funcAdd } from "./native/Aggregable.js";
2
2
  import { funcFirst, funcLast, funcFirstIndex, funcLastIndex, funcEvery, funcAny, funcFlatten, funcReverse, funcDerive, funcFilter, funcReduce, funcCompose, funcPrepend, funcAppend } from "./native/Array.js";
3
+ import { funcBoolean } from "./native/Boolean.js";
3
4
  import { funcByte } from "./native/Buffer.js";
4
5
  import { funcSlice, funcSplice, funcInject } from "./native/Enumerable.js";
6
+ import { funcFloat } from "./native/Float.js";
7
+ import { funcInteger } from "./native/Integer.js";
5
8
  import { funcLength, funcContains, funcAt, funcHas } from "./native/Iterable.js";
6
- import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcModulo, funcPower, funcRoot, funcNegate, funcCast, funcCastToFloat, funcCastToInteger } from "./native/Number.js";
9
+ import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcModulo, funcPower, funcRoot, funcNegate } from "./native/Number.js";
7
10
  import { funcEntries, funcKeys, funcValues } from "./native/Object.js";
8
11
  import { funcLike, funcUnlike, funcStartsWith, funcEndsWith, funcChar, funcCharCode, funcTrim, funcTrimStart, funcTrimEnd, funcLowerCase, funcUpperCase, funcSplit, funcReplaceWith } from "./native/String.js";
9
12
  import { funcYear, funcMonth, funcMonthIndex, funcWeekdayIndex, funcDay, funcHour, funcMinute, funcSecond, funcMillisecond, funcEpochTime, funcDaysSince, funcHoursSince, funcMinutesSince, funcSecondsSince } from "./native/Timestamp.js";
@@ -50,9 +53,9 @@ export const Functions = [
50
53
  ["Power", funcPower],
51
54
  ["Root", funcRoot],
52
55
  ["Negate", funcNegate],
53
- ["Cast", funcCast],
54
- ["CastToFloat", funcCastToFloat],
55
- ["CastToInteger", funcCastToInteger],
56
+ ["Boolean", funcBoolean],
57
+ ["Float", funcFloat],
58
+ ["Integer", funcInteger],
56
59
  // Object
57
60
  ["Entries", funcEntries],
58
61
  ["Keys", funcKeys],
package/dst/Keywords.js CHANGED
@@ -1,10 +1,10 @@
1
1
  export const Keywords = [
2
2
  "true", "false",
3
3
  "null", "void",
4
- "boolean",
5
- "timestamp",
6
- "float",
7
- "integer",
4
+ "bool", "boolean",
5
+ "time", "timestamp",
6
+ "f64", "float",
7
+ "i64", "integer",
8
8
  "buffer",
9
9
  "string",
10
10
  "array",
@@ -32,10 +32,11 @@ export declare class ParserState extends ParserFrame {
32
32
  get isSemicolon(): boolean;
33
33
  get isColon(): boolean;
34
34
  get isComma(): boolean;
35
+ get isTilda(): boolean;
35
36
  get isDot(): boolean;
36
37
  get isQuestion(): boolean;
37
38
  get isEllipsis(): boolean;
38
- get isTilda(): boolean;
39
+ get isCast(): boolean;
39
40
  get isVar(): boolean;
40
41
  get isVal(): boolean;
41
42
  get isIf(): boolean;
@@ -1,10 +1,11 @@
1
1
  import { funcOr, funcAnd, funcNot } from "./native/Boolean.js";
2
- import { parseBuffer } from "./native/Buffer.js";
2
+ import { parseLiteralBuffer } from "./native/Buffer.js";
3
3
  import { funcAdd } from "./native/Aggregable.js";
4
- import { isSignSymbol, isTokenStartSymbol, isNumericSymbol, isTokenSymbol, isDateSymbol, isTimeSymbol, isDateTimeSeparatorSymbol, replaceWith } from "./native/String.js";
5
- import { Constant } from "./Constant.js";
6
- import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcPower } from "./native/Number.js";
4
+ import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcPower, isNumericSymbol, parseLiteralNumber } from "./native/Number.js";
7
5
  import { funcCoalesce, funcEqual, funcNotEqual } from "./native/Unknown.js";
6
+ import { isTokenStartSymbol, isTokenSymbol, parseLiteralString } from "./native/String.js";
7
+ import { parseLiteralTimestamp } from "./native/Timestamp.js";
8
+ import { Constant } from "./Constant.js";
8
9
  import { Type } from "./Type.js";
9
10
  import { ParserFrame } from "./ParserFrame.js";
10
11
  class Literal {
@@ -39,10 +40,11 @@ const idBracesClose = Symbol();
39
40
  const idSemicolon = Symbol();
40
41
  const idColon = Symbol();
41
42
  const idComma = Symbol();
43
+ const idTilda = Symbol();
42
44
  const idDot = Symbol();
43
45
  const idQuestion = Symbol();
44
46
  const idEllipsis = Symbol();
45
- const idTilda = Symbol();
47
+ const idCast = Symbol();
46
48
  const idVar = Symbol();
47
49
  const idVal = Symbol();
48
50
  const idIf = Symbol();
@@ -114,6 +116,9 @@ export class ParserState extends ParserFrame {
114
116
  get isComma() {
115
117
  return this._fragment === idComma;
116
118
  }
119
+ get isTilda() {
120
+ return this._fragment === idTilda;
121
+ }
117
122
  get isDot() {
118
123
  return this._fragment === idDot;
119
124
  }
@@ -123,8 +128,8 @@ export class ParserState extends ParserFrame {
123
128
  get isEllipsis() {
124
129
  return this._fragment === idEllipsis;
125
130
  }
126
- get isTilda() {
127
- return this._fragment === idTilda;
131
+ get isCast() {
132
+ return this._fragment === idCast;
128
133
  }
129
134
  get isVar() {
130
135
  return this._fragment === idVar;
@@ -240,7 +245,15 @@ export class ParserState extends ParserFrame {
240
245
  this._fragment = idSemicolon;
241
246
  break;
242
247
  case ":":
243
- this._fragment = idColon;
248
+ switch (this._expr.charAt(this._end)) {
249
+ case ":":
250
+ ++this._end;
251
+ this._fragment = idCast;
252
+ break;
253
+ default:
254
+ this._fragment = idColon;
255
+ break;
256
+ }
244
257
  break;
245
258
  case ",":
246
259
  this._fragment = idComma;
@@ -437,53 +450,25 @@ export class ParserState extends ParserFrame {
437
450
  break;
438
451
  }
439
452
  break;
440
- case "@":
441
- while (isDateSymbol(this._expr.charAt(this._end))) {
442
- ++this._end;
443
- }
444
- if (isDateTimeSeparatorSymbol(this._expr.charAt(this._end))) {
445
- ++this._end;
446
- while (isTimeSymbol(this._expr.charAt(this._end))) {
447
- ++this._end;
448
- }
449
- if (this._expr.charAt(this._end) === "."
450
- && isNumericSymbol(this._expr.charAt(this._end + 1))) {
451
- ++this._end;
452
- while (isNumericSymbol(this._expr.charAt(this._end))) {
453
- ++this._end;
454
- }
455
- if (this._expr.charAt(this._end) === "Z") {
456
- ++this._end;
457
- }
458
- }
459
- }
460
- this._fragment = new Literal(new Date(this._expr.substring(this._start + 1, this._end)));
453
+ case "@": {
454
+ const [ts, tsEnd] = parseLiteralTimestamp(this._expr, this._start);
455
+ this._fragment = new Literal(ts);
456
+ this._end = tsEnd;
461
457
  break;
462
- case "`":
463
- while (this._expr.charAt(this._end) !== ""
464
- && this._expr.charAt(this._end) !== c) {
465
- ++this._end;
466
- }
467
- if (this._end >= this._expr.length) {
468
- this._start = this._expr.length;
469
- throw new Error(`missing closing buffer quotation mark ${c}`);
470
- }
471
- this._fragment = new Literal(parseBuffer(replaceWith(this._expr.substring(this._start + 1, this._end), "", " ", "\t", "\n")));
472
- ++this._end;
458
+ }
459
+ case "`": {
460
+ const [buf, bufEnd] = parseLiteralBuffer(this._expr, this._start);
461
+ this._fragment = new Literal(buf);
462
+ this._end = bufEnd;
473
463
  break;
464
+ }
474
465
  case "\"":
475
- case "'":
476
- while (this._expr.charAt(this._end) !== ""
477
- && this._expr.charAt(this._end) !== c) {
478
- ++this._end;
479
- }
480
- if (this._end >= this._expr.length) {
481
- this._start = this._expr.length;
482
- throw new Error(`missing closing string quotation mark ${c}`);
483
- }
484
- this._fragment = new Literal(replaceWith(this._expr.substring(this._start + 1, this._end), "", "\\\n"));
485
- ++this._end;
466
+ case "'": {
467
+ const [str, strEnd] = parseLiteralString(this._expr, this._start);
468
+ this._fragment = new Literal(str);
469
+ this._end = strEnd;
486
470
  break;
471
+ }
487
472
  default:
488
473
  if (isTokenStartSymbol(c)) {
489
474
  while (isTokenSymbol(this._expr.charAt(this._end))) {
@@ -503,15 +488,19 @@ export class ParserState extends ParserFrame {
503
488
  case "void":
504
489
  this._fragment = Type.Void;
505
490
  break;
491
+ case "bool":
506
492
  case "boolean":
507
493
  this._fragment = Type.Boolean;
508
494
  break;
495
+ case "time":
509
496
  case "timestamp":
510
497
  this._fragment = Type.Timestamp;
511
498
  break;
499
+ case "f64":
512
500
  case "float":
513
501
  this._fragment = Type.Float;
514
502
  break;
503
+ case "i64":
515
504
  case "integer":
516
505
  this._fragment = Type.Integer;
517
506
  break;
@@ -557,37 +546,9 @@ export class ParserState extends ParserFrame {
557
546
  }
558
547
  }
559
548
  else if (isNumericSymbol(c)) {
560
- let integer = true;
561
- while (isNumericSymbol(this._expr.charAt(this._end))) {
562
- ++this._end;
563
- }
564
- if (this._expr.charAt(this._end) === ".") {
565
- ++this._end;
566
- if (isNumericSymbol(this._expr.charAt(this._end))) {
567
- ++this._end;
568
- while (isNumericSymbol(this._expr.charAt(this._end))) {
569
- ++this._end;
570
- }
571
- integer = false;
572
- }
573
- else {
574
- --this._end;
575
- }
576
- }
577
- if (this._expr.charAt(this._end) === "e") {
578
- ++this._end;
579
- if (isNumericSymbol(this._expr.charAt(this._end))
580
- || isSignSymbol(this._expr.charAt(this._end))) {
581
- ++this._end;
582
- while (isNumericSymbol(this._expr.charAt(this._end))) {
583
- ++this._end;
584
- }
585
- }
586
- integer = false;
587
- }
588
- this._fragment = integer
589
- ? new Literal(BigInt(this._expr.substring(this._start, this._end)))
590
- : new Literal(parseFloat(this._expr.substring(this._start, this._end)));
549
+ const [num, numEnd] = parseLiteralNumber(this._expr, this._start);
550
+ this._fragment = new Literal(num);
551
+ this._end = numEnd;
591
552
  }
592
553
  else {
593
554
  throw new Error(`unknown symbol ${c}`);
package/dst/Type.d.ts CHANGED
@@ -20,20 +20,24 @@ export declare class Type {
20
20
  get isArray(): boolean;
21
21
  get isObject(): boolean;
22
22
  get isFunction(): boolean;
23
- get isNumeric(): boolean;
24
- toOptional(): Type;
25
- functionAtomRetType(type: Type): Type;
26
- functionAtoms(type: Type, argc: number): FunctionAtom[];
23
+ equate(type: Type | undefined): boolean;
24
+ accept(type: Type | Atom): boolean;
25
+ acceptValue(value: Value): boolean;
27
26
  reduce(mask: Type): Type | undefined;
28
- match(type: Type | Atom): boolean;
27
+ intersect(type: Type): boolean;
29
28
  weight(): number;
29
+ arrayAtomValType(index: number): Type;
30
+ functionAtomRetType(type: Type): Type;
31
+ functionAtoms(type: Type, argc: number): FunctionAtom[];
32
+ toOptional(): Type;
33
+ toMandatory(): Type;
30
34
  toString(): string;
31
35
  static union(...types: Type[]): Type;
32
36
  static of(value: Value): Type;
33
- static isPrimitiveType(value: Value): value is string | number | bigint | boolean | Date | ArrayBuffer | null | undefined;
37
+ static isPrimitive(value: Value): value is string | number | bigint | boolean | Date | ArrayBuffer | null | undefined;
34
38
  static primitiveType(primitive: Primitive): Type;
35
39
  static arrayType(itemTypes?: Type[]): Type;
36
- static objectType(propTypes?: Record<string, Type>): Type;
40
+ static objectType(propTypes?: [string, Type][]): Type;
37
41
  static functionType(retType?: Type, argTypes?: Type[], isVariadic?: boolean): Type;
38
42
  static readonly Unknown: Type;
39
43
  static readonly Void: Type;
package/dst/Type.js CHANGED
@@ -14,54 +14,92 @@ export class Type {
14
14
  return this._atoms.length === 1;
15
15
  }
16
16
  get isOptional() {
17
- return this.isUnknown || this._atoms.length > 1 && this._atoms.some((i) => i.match(Type.Void._atoms[0]));
17
+ return this.isUnknown || this._atoms.length > 1 && this._atoms.some((a) => a.equate(Type.Void._atoms[0]));
18
18
  }
19
19
  get isVoid() {
20
- return this.isAtom && this._atoms[0].match(Type.Void._atoms[0]);
20
+ return this.isAtom && this._atoms[0].equate(Type.Void._atoms[0]);
21
21
  }
22
22
  get isBoolean() {
23
- return this.isAtom && this._atoms[0].match(Type.Boolean._atoms[0]);
23
+ return this.isAtom && this._atoms[0].equate(Type.Boolean._atoms[0]);
24
24
  }
25
25
  get isTimestamp() {
26
- return this.isAtom && this._atoms[0].match(Type.Timestamp._atoms[0]);
26
+ return this.isAtom && this._atoms[0].equate(Type.Timestamp._atoms[0]);
27
27
  }
28
28
  get isFloat() {
29
- return this.isAtom && this._atoms[0].match(Type.Float._atoms[0]);
29
+ return this.isAtom && this._atoms[0].equate(Type.Float._atoms[0]);
30
30
  }
31
31
  get isInteger() {
32
- return this.isAtom && this._atoms[0].match(Type.Integer._atoms[0]);
32
+ return this.isAtom && this._atoms[0].equate(Type.Integer._atoms[0]);
33
33
  }
34
34
  get isBuffer() {
35
- return this.isAtom && this._atoms[0].match(Type.Buffer._atoms[0]);
35
+ return this.isAtom && this._atoms[0].equate(Type.Buffer._atoms[0]);
36
36
  }
37
37
  get isString() {
38
- return this.isAtom && this._atoms[0].match(Type.String._atoms[0]);
38
+ return this.isAtom && this._atoms[0].equate(Type.String._atoms[0]);
39
39
  }
40
40
  get isArray() {
41
- return this._atoms.every((i) => i instanceof ArrayAtom);
41
+ return this._atoms.every((a) => a instanceof ArrayAtom);
42
42
  }
43
43
  get isObject() {
44
- return this._atoms.every((i) => i instanceof ObjectAtom);
44
+ return this._atoms.every((a) => a instanceof ObjectAtom);
45
45
  }
46
46
  get isFunction() {
47
- return this._atoms.every((i) => i instanceof FunctionAtom);
47
+ return this._atoms.every((a) => a instanceof FunctionAtom);
48
48
  }
49
- get isNumeric() {
50
- return this._atoms.length === 2
51
- && (this._atoms[0].match(Type.Float._atoms[0]) || this._atoms[0].match(Type.Integer._atoms[0]));
52
- }
53
- toOptional() {
54
- return this._atoms.length ? Type.union(Type.Void, this) : this;
49
+ equate(type) {
50
+ if (this._atoms.length !== type?._atoms.length) {
51
+ return false;
52
+ }
53
+ const unmatched = [...type._atoms];
54
+ for (const atom of this._atoms) {
55
+ const index = unmatched.findIndex((a) => atom.equate(a));
56
+ if (index < 0) {
57
+ return false;
58
+ }
59
+ unmatched.splice(index, 1);
60
+ }
61
+ return true;
55
62
  }
56
- functionAtomRetType(type) {
57
- const atoms = this._atoms.filter((i) => i instanceof FunctionAtom && i.retType.match(type));
58
- return Type.union(...atoms.map((i) => i.retType));
63
+ accept(type) {
64
+ if (type instanceof Type) {
65
+ if (this.isUnknown || type.isUnknown) {
66
+ return true;
67
+ }
68
+ return type._atoms.every((a) => this._atoms.some((atom) => atom.accept(a)));
69
+ }
70
+ return this._atoms.some((a) => a.accept(type));
59
71
  }
60
- functionAtoms(type, argc) {
72
+ acceptValue(value) {
61
73
  if (this.isUnknown) {
62
- return Type.Function._atoms;
74
+ return true;
63
75
  }
64
- return this._atoms.filter((i) => i instanceof FunctionAtom && i.minArity <= argc && i.maxArity >= argc && (type.isVoid || i.retType.match(type)));
76
+ const valueAtom = Type.of(value)._atoms[0];
77
+ return this._atoms.some((atom) => {
78
+ if (!atom.accept(valueAtom)) {
79
+ return false;
80
+ }
81
+ if (atom instanceof ArrayAtom) {
82
+ const values = value;
83
+ const types = atom.valTypes();
84
+ if (!types.length) {
85
+ return true;
86
+ }
87
+ if (atom.isTuple) {
88
+ return types.every((type, ix) => type.acceptValue(Object.hasOwn(values, ix) ? values[ix] : undefined));
89
+ }
90
+ for (let i = 0; i < values.length; ++i) {
91
+ if (!types[0].acceptValue(Object.hasOwn(values, i) ? values[i] : undefined)) {
92
+ return false;
93
+ }
94
+ }
95
+ return true;
96
+ }
97
+ if (atom instanceof ObjectAtom) {
98
+ const values = value;
99
+ return atom.valTypes().every(([key, type]) => type.acceptValue(Object.hasOwn(values, key) ? values[key] : undefined));
100
+ }
101
+ return true;
102
+ });
65
103
  }
66
104
  reduce(mask) {
67
105
  if (mask.isUnknown || mask.isVoid) {
@@ -70,37 +108,74 @@ export class Type {
70
108
  if (this.isUnknown) {
71
109
  return mask;
72
110
  }
73
- const list = this._atoms.filter((i) => mask.match(i));
111
+ const list = this._atoms.filter((a) => mask.accept(a));
74
112
  return list.length === 0
75
113
  ? undefined
76
114
  : list.length === this._atoms.length
77
115
  ? this
78
116
  : new Type(list);
79
117
  }
80
- match(type) {
81
- if (type instanceof Type) {
82
- return this.isUnknown || type.isUnknown || type.isVoid || this._atoms.some((i) => type._atoms.some((j) => i.match(j)));
83
- }
84
- else {
85
- return this._atoms.some((i) => i.match(type));
118
+ intersect(type) {
119
+ if (this.isUnknown || type.isUnknown) {
120
+ return true;
86
121
  }
122
+ return this._atoms.some((a) => type._atoms.some((atom) => a.intersect(atom)));
87
123
  }
88
124
  weight() {
89
- return this._atoms.reduce((acc, i) => acc + i.weight(), 0);
125
+ return this._atoms.reduce((acc, a) => acc + a.weight(), 0);
126
+ }
127
+ arrayAtomValType(index) {
128
+ const atoms = this._atoms.filter((a) => a instanceof ArrayAtom);
129
+ if (!atoms.length) {
130
+ return Type.Unknown;
131
+ }
132
+ const valTypes = [];
133
+ for (const atom of atoms) {
134
+ const valType = atom.isTuple
135
+ ? atom.valType(index)
136
+ : atom.valType(0);
137
+ if (!valType) {
138
+ return Type.Unknown;
139
+ }
140
+ valTypes.push(valType);
141
+ }
142
+ return Type.union(...valTypes);
143
+ }
144
+ functionAtomRetType(type) {
145
+ const atoms = this._atoms.filter((a) => a instanceof FunctionAtom && type.accept(a.retType));
146
+ return Type.union(...atoms.map((a) => a.retType));
147
+ }
148
+ functionAtoms(type, argc) {
149
+ if (this.isUnknown) {
150
+ return Type.Function._atoms;
151
+ }
152
+ return this._atoms.filter((a) => a instanceof FunctionAtom && a.minArity <= argc && a.maxArity >= argc && (type.isVoid || type.accept(a.retType)));
153
+ }
154
+ toOptional() {
155
+ return this._atoms.length ? Type.union(Type.Void, this) : this;
156
+ }
157
+ toMandatory() {
158
+ if (!this.isOptional || this.isUnknown) {
159
+ return this;
160
+ }
161
+ return new Type(this._atoms.filter((a) => !a.equate(Type.Void._atoms[0])));
90
162
  }
91
163
  toString() {
92
164
  return this._atoms.length
93
- ? this._atoms.sort((a, b) => a.weight() - b.weight()).map((i) => i.toString()).join("|")
165
+ ? [...this._atoms]
166
+ .sort((a, b) => a.weight() - b.weight())
167
+ .map((a) => a.toString())
168
+ .join("|")
94
169
  : "??";
95
170
  }
96
171
  static union(...types) {
97
- if (types.some((i) => i.isUnknown)) {
172
+ if (types.some((t) => t.isUnknown)) {
98
173
  return Type.Unknown;
99
174
  }
100
175
  const type = new Type([]);
101
- types.map((i) => i._atoms).flat().forEach((i) => {
102
- if (!type.match(i)) {
103
- type._atoms.push(i);
176
+ types.flatMap((t) => t._atoms).forEach((a) => {
177
+ if (!type._atoms.some((atom) => atom.equate(a))) {
178
+ type._atoms.push(a);
104
179
  }
105
180
  });
106
181
  return type;
@@ -126,7 +201,7 @@ export class Type {
126
201
  ? Type.Object
127
202
  : Type.Function;
128
203
  }
129
- static isPrimitiveType(value) {
204
+ static isPrimitive(value) {
130
205
  return value == null
131
206
  || typeof value === "boolean"
132
207
  || value instanceof Date
@@ -141,7 +216,7 @@ export class Type {
141
216
  static arrayType(itemTypes = []) {
142
217
  return new Type([new ArrayAtom(itemTypes)]);
143
218
  }
144
- static objectType(propTypes = Object.create(null)) {
219
+ static objectType(propTypes = []) {
145
220
  return new Type([new ObjectAtom(propTypes)]);
146
221
  }
147
222
  static functionType(retType = Type.Unknown, argTypes = [], isVariadic = false) {
package/dst/Value.d.ts CHANGED
@@ -2,3 +2,4 @@ export type PrimitiveValue = undefined | null | number | boolean | Date | bigint
2
2
  export type Value = PrimitiveValue | Value[] | {
3
3
  [key: string]: Value;
4
4
  } | ((...args: any[]) => Value);
5
+ export declare const normalize: (value: Value) => Value;