@mojir/lits 2.1.11 → 2.1.13

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.
@@ -448,6 +448,8 @@ function isNumber(value, options) {
448
448
  if (options === void 0) { options = {}; }
449
449
  if (typeof value !== 'number')
450
450
  return false;
451
+ if (Number.isNaN(value))
452
+ return false;
451
453
  if (options.integer && !Number.isInteger(value))
452
454
  return false;
453
455
  if (options.finite && !Number.isFinite(value))
@@ -1068,6 +1070,35 @@ var collectionNormalExpression = {
1068
1070
  },
1069
1071
  paramCount: 2,
1070
1072
  },
1073
+ 'filteri': {
1074
+ evaluate: function (_a, sourceCodeInfo, contextStack, _b) {
1075
+ var _c = __read(_a, 2), coll = _c[0], fn = _c[1];
1076
+ var executeFunction = _b.executeFunction;
1077
+ assertColl(coll, sourceCodeInfo);
1078
+ assertFunctionLike(fn, sourceCodeInfo);
1079
+ if (Array.isArray(coll)) {
1080
+ var result = coll.filter(function (elem, index) { return executeFunction(fn, [elem, index], contextStack, sourceCodeInfo); });
1081
+ return result;
1082
+ }
1083
+ if (isString(coll)) {
1084
+ return coll
1085
+ .split('')
1086
+ .filter(function (elem, index) { return executeFunction(fn, [elem, index], contextStack, sourceCodeInfo); })
1087
+ .join('');
1088
+ }
1089
+ return Object.entries(coll)
1090
+ .filter(function (_a) {
1091
+ var _b = __read(_a, 2), key = _b[0], value = _b[1];
1092
+ return executeFunction(fn, [value, key], contextStack, sourceCodeInfo);
1093
+ })
1094
+ .reduce(function (result, _a) {
1095
+ var _b = __read(_a, 2), key = _b[0], value = _b[1];
1096
+ result[key] = value;
1097
+ return result;
1098
+ }, {});
1099
+ },
1100
+ paramCount: 2,
1101
+ },
1071
1102
  'map': {
1072
1103
  evaluate: function (params, sourceCodeInfo, contextStack, _a) {
1073
1104
  var executeFunction = _a.executeFunction;
@@ -1110,6 +1141,30 @@ var collectionNormalExpression = {
1110
1141
  },
1111
1142
  paramCount: { min: 2 },
1112
1143
  },
1144
+ 'mapi': {
1145
+ evaluate: function (_a, sourceCodeInfo, contextStack, _b) {
1146
+ var _c = __read(_a, 2), coll = _c[0], fn = _c[1];
1147
+ var executeFunction = _b.executeFunction;
1148
+ assertColl(coll, sourceCodeInfo);
1149
+ assertFunctionLike(fn, sourceCodeInfo);
1150
+ if (Array.isArray(coll)) {
1151
+ return coll.map(function (elem, index) { return executeFunction(fn, [elem, index], contextStack, sourceCodeInfo); });
1152
+ }
1153
+ if (isString(coll)) {
1154
+ return coll
1155
+ .split('')
1156
+ .map(function (elem, index) { return executeFunction(fn, [elem, index], contextStack, sourceCodeInfo); })
1157
+ .join('');
1158
+ }
1159
+ return Object.entries(coll)
1160
+ .reduce(function (acc, _a) {
1161
+ var _b = __read(_a, 2), key = _b[0], value = _b[1];
1162
+ acc[key] = executeFunction(fn, [value, key], contextStack, sourceCodeInfo);
1163
+ return acc;
1164
+ }, {});
1165
+ },
1166
+ paramCount: 2,
1167
+ },
1113
1168
  'reduce': {
1114
1169
  evaluate: function (_a, sourceCodeInfo, contextStack, _b) {
1115
1170
  var _c = __read(_a, 3), coll = _c[0], fn = _c[1], initial = _c[2];
@@ -1143,6 +1198,39 @@ var collectionNormalExpression = {
1143
1198
  },
1144
1199
  paramCount: 3,
1145
1200
  },
1201
+ 'reducei': {
1202
+ evaluate: function (_a, sourceCodeInfo, contextStack, _b) {
1203
+ var _c = __read(_a, 3), coll = _c[0], fn = _c[1], initial = _c[2];
1204
+ var executeFunction = _b.executeFunction;
1205
+ assertColl(coll, sourceCodeInfo);
1206
+ assertFunctionLike(fn, sourceCodeInfo);
1207
+ assertAny(initial, sourceCodeInfo);
1208
+ if (typeof coll === 'string') {
1209
+ assertString(initial, sourceCodeInfo);
1210
+ if (coll.length === 0)
1211
+ return initial;
1212
+ return coll.split('').reduce(function (result, elem, index) {
1213
+ return executeFunction(fn, [result, elem, index], contextStack, sourceCodeInfo);
1214
+ }, initial);
1215
+ }
1216
+ else if (Array.isArray(coll)) {
1217
+ if (coll.length === 0)
1218
+ return initial;
1219
+ return coll.reduce(function (result, elem, index) {
1220
+ return executeFunction(fn, [result, elem, index], contextStack, sourceCodeInfo);
1221
+ }, initial);
1222
+ }
1223
+ else {
1224
+ if (Object.keys(coll).length === 0)
1225
+ return initial;
1226
+ return Object.entries(coll).reduce(function (result, _a) {
1227
+ var _b = __read(_a, 2), key = _b[0], elem = _b[1];
1228
+ return executeFunction(fn, [result, elem, key], contextStack, sourceCodeInfo);
1229
+ }, initial);
1230
+ }
1231
+ },
1232
+ paramCount: 3,
1233
+ },
1146
1234
  'reduce-right': {
1147
1235
  evaluate: function (_a, sourceCodeInfo, contextStack, _b) {
1148
1236
  var _c = __read(_a, 3), coll = _c[0], fn = _c[1], initial = _c[2];
@@ -1175,6 +1263,38 @@ var collectionNormalExpression = {
1175
1263
  },
1176
1264
  paramCount: 3,
1177
1265
  },
1266
+ 'reducei-right': {
1267
+ evaluate: function (_a, sourceCodeInfo, contextStack, _b) {
1268
+ var _c = __read(_a, 3), coll = _c[0], fn = _c[1], initial = _c[2];
1269
+ var executeFunction = _b.executeFunction;
1270
+ assertColl(coll, sourceCodeInfo);
1271
+ assertFunctionLike(fn, sourceCodeInfo);
1272
+ assertAny(initial, sourceCodeInfo);
1273
+ if (typeof coll === 'string') {
1274
+ if (coll.length === 0)
1275
+ return initial;
1276
+ return coll.split('').reduceRight(function (result, elem, index) {
1277
+ return executeFunction(fn, [result, elem, index], contextStack, sourceCodeInfo);
1278
+ }, initial);
1279
+ }
1280
+ else if (Array.isArray(coll)) {
1281
+ if (coll.length === 0)
1282
+ return initial;
1283
+ return coll.reduceRight(function (result, elem, index) {
1284
+ return executeFunction(fn, [result, elem, index], contextStack, sourceCodeInfo);
1285
+ }, initial);
1286
+ }
1287
+ else {
1288
+ if (Object.keys(coll).length === 0)
1289
+ return initial;
1290
+ return Object.entries(coll).reduceRight(function (result, _a) {
1291
+ var _b = __read(_a, 2), key = _b[0], elem = _b[1];
1292
+ return executeFunction(fn, [result, elem, key], contextStack, sourceCodeInfo);
1293
+ }, initial);
1294
+ }
1295
+ },
1296
+ paramCount: 3,
1297
+ },
1178
1298
  'reductions': {
1179
1299
  evaluate: function (_a, sourceCodeInfo, contextStack, _b) {
1180
1300
  var _c = __read(_a, 3), coll = _c[0], fn = _c[1], initial = _c[2];
@@ -1221,6 +1341,52 @@ var collectionNormalExpression = {
1221
1341
  },
1222
1342
  paramCount: 3,
1223
1343
  },
1344
+ 'reductionsi': {
1345
+ evaluate: function (_a, sourceCodeInfo, contextStack, _b) {
1346
+ var _c = __read(_a, 3), coll = _c[0], fn = _c[1], initial = _c[2];
1347
+ var executeFunction = _b.executeFunction;
1348
+ assertColl(coll, sourceCodeInfo);
1349
+ assertFunctionLike(fn, sourceCodeInfo);
1350
+ assertAny(initial, sourceCodeInfo);
1351
+ assertAny(initial, sourceCodeInfo);
1352
+ if (typeof coll === 'string') {
1353
+ assertString(initial, sourceCodeInfo);
1354
+ if (coll.length === 0)
1355
+ return [initial];
1356
+ var resultArray_4 = [initial];
1357
+ coll.split('').reduce(function (result, elem, index) {
1358
+ var newVal = executeFunction(fn, [result, elem, index], contextStack, sourceCodeInfo);
1359
+ resultArray_4.push(newVal);
1360
+ return newVal;
1361
+ }, initial);
1362
+ return resultArray_4;
1363
+ }
1364
+ else if (Array.isArray(coll)) {
1365
+ if (coll.length === 0)
1366
+ return [initial];
1367
+ var resultArray_5 = [initial];
1368
+ coll.reduce(function (result, elem, index) {
1369
+ var newVal = executeFunction(fn, [result, elem, index], contextStack, sourceCodeInfo);
1370
+ resultArray_5.push(newVal);
1371
+ return newVal;
1372
+ }, initial);
1373
+ return resultArray_5;
1374
+ }
1375
+ else {
1376
+ if (Object.keys(coll).length === 0)
1377
+ return [initial];
1378
+ var resultArray_6 = [initial];
1379
+ Object.entries(coll).reduce(function (result, _a) {
1380
+ var _b = __read(_a, 2), key = _b[0], elem = _b[1];
1381
+ var newVal = executeFunction(fn, [result, elem, key], contextStack, sourceCodeInfo);
1382
+ resultArray_6.push(newVal);
1383
+ return newVal;
1384
+ }, initial);
1385
+ return resultArray_6;
1386
+ }
1387
+ },
1388
+ paramCount: 3,
1389
+ },
1224
1390
  'get': {
1225
1391
  evaluate: function (params, sourceCodeInfo) {
1226
1392
  var _a = __read(params, 2), coll = _a[0], key = _a[1];
@@ -1474,7 +1640,7 @@ var collectionNormalExpression = {
1474
1640
  };
1475
1641
 
1476
1642
  var arrayNormalExpression = {
1477
- range: {
1643
+ 'range': {
1478
1644
  evaluate: function (params, sourceCodeInfo) {
1479
1645
  var _a = __read(params, 3), first = _a[0], second = _a[1], third = _a[2];
1480
1646
  var from;
@@ -1512,7 +1678,7 @@ var arrayNormalExpression = {
1512
1678
  },
1513
1679
  paramCount: { min: 1, max: 3 },
1514
1680
  },
1515
- repeat: {
1681
+ 'repeat': {
1516
1682
  evaluate: function (_a, sourceCodeInfo) {
1517
1683
  var _b = __read(_a, 2), value = _b[0], count = _b[1];
1518
1684
  assertNumber(count, sourceCodeInfo, { integer: true, nonNegative: true });
@@ -1523,7 +1689,7 @@ var arrayNormalExpression = {
1523
1689
  },
1524
1690
  paramCount: 2,
1525
1691
  },
1526
- flatten: {
1692
+ 'flatten': {
1527
1693
  evaluate: function (_a, sourceCodeInfo) {
1528
1694
  var _b = __read(_a, 2), seq = _b[0], depth = _b[1];
1529
1695
  assertArray(seq, sourceCodeInfo);
@@ -1534,7 +1700,7 @@ var arrayNormalExpression = {
1534
1700
  },
1535
1701
  paramCount: { min: 1, max: 2 },
1536
1702
  },
1537
- mapcat: {
1703
+ 'mapcat': {
1538
1704
  evaluate: function (_a, sourceCodeInfo, contextStack, _b) {
1539
1705
  var _c = __read(_a, 2), arr = _c[0], fn = _c[1];
1540
1706
  var executeFunction = _b.executeFunction;
@@ -1544,6 +1710,38 @@ var arrayNormalExpression = {
1544
1710
  },
1545
1711
  paramCount: 2,
1546
1712
  },
1713
+ 'moving-fn': {
1714
+ evaluate: function (_a, sourceCodeInfo, contextStack, _b) {
1715
+ var _c = __read(_a, 3), arr = _c[0], windowSize = _c[1], fn = _c[2];
1716
+ var executeFunction = _b.executeFunction;
1717
+ assertArray(arr, sourceCodeInfo);
1718
+ assertNumber(windowSize, sourceCodeInfo, { integer: true, lte: arr.length });
1719
+ assertFunctionLike(fn, sourceCodeInfo);
1720
+ var result = [];
1721
+ for (var i = 0; i <= arr.length - windowSize; i++) {
1722
+ var window_1 = arr.slice(i, i + windowSize);
1723
+ var value = executeFunction(fn, [window_1], contextStack, sourceCodeInfo);
1724
+ result.push(value);
1725
+ }
1726
+ return result;
1727
+ },
1728
+ paramCount: 3,
1729
+ },
1730
+ 'running-fn': {
1731
+ evaluate: function (_a, sourceCodeInfo, contextStack, _b) {
1732
+ var _c = __read(_a, 2), arr = _c[0], fn = _c[1];
1733
+ var executeFunction = _b.executeFunction;
1734
+ assertArray(arr, sourceCodeInfo);
1735
+ assertFunctionLike(fn, sourceCodeInfo);
1736
+ var result = [];
1737
+ for (var i = 0; i < arr.length; i += 1) {
1738
+ var subArr = arr.slice(0, i + 1);
1739
+ result.push(executeFunction(fn, [subArr], contextStack, sourceCodeInfo));
1740
+ }
1741
+ return result;
1742
+ },
1743
+ paramCount: 2,
1744
+ },
1547
1745
  };
1548
1746
 
1549
1747
  var sequenceNormalExpression = {
@@ -2275,7 +2473,7 @@ function isVector(vector) {
2275
2473
  if (vectors.has(vector)) {
2276
2474
  return true;
2277
2475
  }
2278
- if (vector.every(function (elem) { return isNumber(elem, { finite: true }); })) {
2476
+ if (vector.every(function (elem) { return isNumber(elem); })) {
2279
2477
  annotatedArrays.add(vector);
2280
2478
  vectors.add(vector);
2281
2479
  return true;
@@ -2384,7 +2582,7 @@ function isMatrix(matrix) {
2384
2582
  if (row.length !== nbrOfCols) {
2385
2583
  return false;
2386
2584
  }
2387
- if (row.some(function (cell) { return !isNumber(cell, { finite: true }); })) {
2585
+ if (row.some(function (cell) { return !isNumber(cell); })) {
2388
2586
  return false;
2389
2587
  }
2390
2588
  }
@@ -3917,14 +4115,6 @@ var predicatesNormalExpression = {
3917
4115
  },
3918
4116
  paramCount: 1,
3919
4117
  },
3920
- 'nan?': {
3921
- evaluate: function (_a, sourceCodeInfo) {
3922
- var _b = __read(_a, 1), value = _b[0];
3923
- assertNumber(value, sourceCodeInfo);
3924
- return Number.isNaN(value);
3925
- },
3926
- paramCount: 1,
3927
- },
3928
4118
  'positive-infinity?': {
3929
4119
  evaluate: function (_a, sourceCodeInfo) {
3930
4120
  var _b = __read(_a, 1), value = _b[0];
@@ -12486,8 +12676,13 @@ function evaluateNode(node, contextStack) {
12486
12676
  return contextStack.evaluateSymbol(node);
12487
12677
  case NodeTypes.ReservedSymbol:
12488
12678
  return evaluateReservedSymbol(node);
12489
- case NodeTypes.NormalExpression:
12490
- return annotate(evaluateNormalExpression(node, contextStack));
12679
+ case NodeTypes.NormalExpression: {
12680
+ var result = evaluateNormalExpression(node, contextStack);
12681
+ if (typeof result === 'number' && Number.isNaN(result)) {
12682
+ throw new LitsError('Number is NaN', node[2]);
12683
+ }
12684
+ return annotate(result);
12685
+ }
12491
12686
  case NodeTypes.SpecialExpression:
12492
12687
  return annotate(evaluateSpecialExpression(node, contextStack));
12493
12688
  /* v8 ignore next 2 */
@@ -12879,6 +13074,8 @@ var binaryOperators = [
12879
13074
  '|>', // pipe
12880
13075
  ];
12881
13076
  var otherOperators = [
13077
+ '?', // conditional operator
13078
+ ':', // conditional operator
12882
13079
  '->', // lambda
12883
13080
  '...', // rest
12884
13081
  '.', // property accessor
@@ -12888,17 +13085,12 @@ var otherOperators = [
12888
13085
  ];
12889
13086
  var symbolicOperators = __spreadArray(__spreadArray([], __read(binaryOperators), false), __read(otherOperators), false);
12890
13087
  var nonFunctionOperators = [
12891
- '??',
12892
- '&&',
12893
- '||',
12894
13088
  'comment',
12895
13089
  'cond',
12896
13090
  'def',
12897
13091
  'defined?',
12898
- // 'defn',
12899
13092
  'do',
12900
13093
  'doseq',
12901
- // 'fn',
12902
13094
  'if',
12903
13095
  'let',
12904
13096
  'loop',
@@ -13476,8 +13668,9 @@ function untokenize(tokenStream) {
13476
13668
  }, '');
13477
13669
  }
13478
13670
 
13479
- var exponentiationPrecedence = 11;
13480
- var binaryFunctionalOperatorPrecedence = 2;
13671
+ var exponentiationPrecedence = 12;
13672
+ var binaryFunctionalOperatorPrecedence = 3;
13673
+ var conditionalOperatorPrecedence = 1;
13481
13674
  var placeholderRegexp = /^\$([1-9]\d?)?$/;
13482
13675
  function withSourceCodeInfo(node, sourceCodeInfo) {
13483
13676
  if (sourceCodeInfo) {
@@ -13492,38 +13685,39 @@ function getPrecedence(operatorSign, sourceCodeInfo) {
13492
13685
  case '*': // multiplication
13493
13686
  case '/': // division
13494
13687
  case '%': // remainder
13495
- return 10;
13688
+ return 11;
13496
13689
  case '+': // addition
13497
13690
  case '-': // subtraction
13498
- return 9;
13691
+ return 10;
13499
13692
  case '<<': // left shift
13500
13693
  case '>>': // signed right shift
13501
13694
  case '>>>': // unsigned right shift
13502
- return 8;
13695
+ return 9;
13503
13696
  case '++': // string concatenation
13504
- return 7;
13697
+ return 8;
13505
13698
  case '<': // less than
13506
13699
  case '<=': // less than or equal
13507
13700
  case '≤': // less than or equal
13508
13701
  case '>': // greater than
13509
13702
  case '>=': // greater than or equal
13510
13703
  case '≥': // greater than or equal
13511
- return 6;
13704
+ return 7;
13512
13705
  case '=': // equal
13513
13706
  case '!=': // not equal
13514
13707
  case '≠': // not equal
13515
- return 5;
13708
+ return 6;
13516
13709
  case '&': // bitwise AND
13517
13710
  case 'xor': // bitwise XOR
13518
13711
  case '|': // bitwise OR
13519
- return 4;
13712
+ return 5;
13520
13713
  case '&&': // logical AND
13521
13714
  case '||': // logical OR
13522
13715
  case '??': // nullish coalescing
13523
- return 3;
13716
+ return 4;
13717
+ // leave room for binaryFunctionalOperatorPrecedence = 3
13524
13718
  case '|>': // pipe
13525
- return 1;
13526
- // leave room for binaryFunctionalOperatorPrecedence = 2
13719
+ return 2;
13720
+ // leave room for conditionalOperatorPrecedence = 1
13527
13721
  /* v8 ignore next 2 */
13528
13722
  default:
13529
13723
  throw new LitsError("Unknown binary operator: ".concat(operatorSign), sourceCodeInfo);
@@ -13571,13 +13765,15 @@ function fromBinaryOperatorToNode(operator, symbolNode, left, right, sourceCodeI
13571
13765
  case '||':
13572
13766
  case '??':
13573
13767
  return withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes[operatorName], [left, right]]], sourceCodeInfo);
13574
- /* v8 ignore next 10 */
13768
+ /* v8 ignore next 11 */
13575
13769
  case '.':
13576
13770
  case ';':
13577
13771
  case ':=':
13578
13772
  case ',':
13579
13773
  case '->':
13580
13774
  case '...':
13775
+ case '?':
13776
+ case ':':
13581
13777
  throw new LitsError("Unknown binary operator: ".concat(operatorName), sourceCodeInfo);
13582
13778
  default:
13583
13779
  throw new LitsError("Unknown binary operator: ".concat(operatorName), sourceCodeInfo);
@@ -13693,6 +13889,19 @@ var Parser = /** @class */ (function () {
13693
13889
  }
13694
13890
  left = createNamedNormalExpressionNode(operatorSymbol, [left, right], operator[2]);
13695
13891
  }
13892
+ else if ((operator === null || operator === void 0 ? void 0 : operator[1]) === '?') {
13893
+ if (conditionalOperatorPrecedence <= precedence) {
13894
+ break;
13895
+ }
13896
+ this.advance();
13897
+ var trueNode = this.parseExpression();
13898
+ if (!isOperatorToken(this.peek(), ':')) {
13899
+ throw new LitsError('Expected :', this.peekSourceCodeInfo());
13900
+ }
13901
+ this.advance();
13902
+ var falseNode = this.parseExpression();
13903
+ left = withSourceCodeInfo([NodeTypes.SpecialExpression, [specialExpressionTypes.if, [left, trueNode, falseNode]]], left[2]);
13904
+ }
13696
13905
  else {
13697
13906
  break;
13698
13907
  }