@gabrielbryk/jq-ts 1.2.0 → 1.3.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.
package/dist/index.cjs CHANGED
@@ -864,8 +864,27 @@ var Parser = class {
864
864
  if (!this.match("RBrace")) {
865
865
  do {
866
866
  const key = this.parseObjectKey();
867
- this.consume("Colon", "Expected \":\" after object key");
868
- const value = this.parseDef(false);
867
+ let value;
868
+ if (this.match("Colon")) value = this.parseDef(false);
869
+ else if (key.kind === "KeyIdentifier") value = {
870
+ kind: "FieldAccess",
871
+ target: {
872
+ kind: "Identity",
873
+ span: key.span
874
+ },
875
+ field: key.name,
876
+ span: key.span
877
+ };
878
+ else if (key.kind === "KeyString") value = {
879
+ kind: "FieldAccess",
880
+ target: {
881
+ kind: "Identity",
882
+ span: key.span
883
+ },
884
+ field: key.value,
885
+ span: key.span
886
+ };
887
+ else throw this.error(this.peek(), "Expected \":\" after object key");
869
888
  entries.push({
870
889
  key,
871
890
  value
@@ -1347,6 +1366,51 @@ const stdBuiltins = [
1347
1366
  name: "empty",
1348
1367
  arity: 0,
1349
1368
  apply: function* () {}
1369
+ },
1370
+ {
1371
+ name: "toboolean",
1372
+ arity: 0,
1373
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1374
+ yield emit$1(isTruthy(input), span, tracker);
1375
+ }
1376
+ },
1377
+ {
1378
+ name: "walk",
1379
+ arity: 1,
1380
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1381
+ const f = args[0];
1382
+ const walkRec = function* (curr) {
1383
+ tracker.step(span);
1384
+ let newStruct = curr;
1385
+ if (Array.isArray(curr)) {
1386
+ const newArr = [];
1387
+ for (const item of curr) for (const walkedItem of walkRec(item)) newArr.push(walkedItem);
1388
+ newStruct = newArr;
1389
+ } else if (isPlainObject(curr)) {
1390
+ const newObj = {};
1391
+ const keys = Object.keys(curr).sort();
1392
+ let objValid = true;
1393
+ for (const key of keys) {
1394
+ const val = curr[key];
1395
+ let lastVal;
1396
+ let found = false;
1397
+ for (const walkedVal of walkRec(val)) {
1398
+ lastVal = walkedVal;
1399
+ found = true;
1400
+ }
1401
+ if (found) newObj[key] = lastVal;
1402
+ else {
1403
+ objValid = false;
1404
+ break;
1405
+ }
1406
+ }
1407
+ if (!objValid) return;
1408
+ newStruct = newObj;
1409
+ }
1410
+ yield* evaluate$1(f, newStruct, env, tracker);
1411
+ };
1412
+ yield* walkRec(input);
1413
+ }
1350
1414
  }
1351
1415
  ];
1352
1416
 
@@ -1360,6 +1424,232 @@ const errorBuiltins = [{
1360
1424
  }
1361
1425
  }];
1362
1426
 
1427
+ //#endregion
1428
+ //#region src/builtins/strings.ts
1429
+ const checkContains = (a, b) => {
1430
+ if (a === b) return true;
1431
+ if (typeof a !== typeof b) return false;
1432
+ if (typeof a === "string" && typeof b === "string") return a.includes(b);
1433
+ if (Array.isArray(a) && Array.isArray(b)) return b.every((bItem) => a.some((aItem) => checkContains(aItem, bItem)));
1434
+ if (isPlainObject(a) && isPlainObject(b)) {
1435
+ const keys = Object.keys(b);
1436
+ for (const key of keys) {
1437
+ if (!Object.prototype.hasOwnProperty.call(a, key)) return false;
1438
+ const valA = a[key];
1439
+ const valB = b[key];
1440
+ if (!checkContains(valA, valB)) return false;
1441
+ }
1442
+ return true;
1443
+ }
1444
+ return valueEquals(a, b);
1445
+ };
1446
+ const stringBuiltins = [
1447
+ {
1448
+ name: "split",
1449
+ arity: 1,
1450
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1451
+ if (typeof input !== "string") throw new RuntimeError("split input must be a string", span);
1452
+ const sepGen = evaluate$1(args[0], input, env, tracker);
1453
+ for (const sep of sepGen) {
1454
+ if (typeof sep !== "string") throw new RuntimeError("split separator must be a string", span);
1455
+ yield emit$1(input.split(sep), span, tracker);
1456
+ }
1457
+ }
1458
+ },
1459
+ {
1460
+ name: "join",
1461
+ arity: 1,
1462
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1463
+ if (!Array.isArray(input)) throw new RuntimeError("join input must be an array", span);
1464
+ const sepGen = evaluate$1(args[0], input, env, tracker);
1465
+ for (const sep of sepGen) {
1466
+ if (typeof sep !== "string") throw new RuntimeError("join separator must be a string", span);
1467
+ const parts = [];
1468
+ for (const item of input) {
1469
+ if (typeof item !== "string") throw new RuntimeError(`join expects strings, but got ${describeType(item)}`, span);
1470
+ parts.push(item);
1471
+ }
1472
+ yield emit$1(parts.join(sep), span, tracker);
1473
+ }
1474
+ }
1475
+ },
1476
+ {
1477
+ name: "startswith",
1478
+ arity: 1,
1479
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1480
+ if (typeof input !== "string") throw new RuntimeError("startswith input must be a string", span);
1481
+ const prefixGen = evaluate$1(args[0], input, env, tracker);
1482
+ for (const prefix of prefixGen) {
1483
+ if (typeof prefix !== "string") throw new RuntimeError("startswith prefix must be a string", span);
1484
+ yield emit$1(input.startsWith(prefix), span, tracker);
1485
+ }
1486
+ }
1487
+ },
1488
+ {
1489
+ name: "endswith",
1490
+ arity: 1,
1491
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1492
+ if (typeof input !== "string") throw new RuntimeError("endswith input must be a string", span);
1493
+ const suffixGen = evaluate$1(args[0], input, env, tracker);
1494
+ for (const suffix of suffixGen) {
1495
+ if (typeof suffix !== "string") throw new RuntimeError("endswith suffix must be a string", span);
1496
+ yield emit$1(input.endsWith(suffix), span, tracker);
1497
+ }
1498
+ }
1499
+ },
1500
+ {
1501
+ name: "contains",
1502
+ arity: 1,
1503
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1504
+ const bGen = evaluate$1(args[0], input, env, tracker);
1505
+ for (const b of bGen) yield emit$1(checkContains(input, b), span, tracker);
1506
+ }
1507
+ },
1508
+ {
1509
+ name: "index",
1510
+ arity: 1,
1511
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1512
+ const searchGen = evaluate$1(args[0], input, env, tracker);
1513
+ for (const search of searchGen) if (Array.isArray(input)) {
1514
+ let foundIndex = null;
1515
+ for (let i = 0; i < input.length; i++) {
1516
+ const val = input[i];
1517
+ if (val !== void 0 && valueEquals(val, search)) {
1518
+ foundIndex = i;
1519
+ break;
1520
+ }
1521
+ }
1522
+ yield emit$1(foundIndex, span, tracker);
1523
+ } else if (typeof input === "string") {
1524
+ if (typeof search !== "string") throw new RuntimeError("index expects string search when input is string", span);
1525
+ const idx = input.indexOf(search);
1526
+ yield emit$1(idx === -1 ? null : idx, span, tracker);
1527
+ } else if (input === null) yield emit$1(null, span, tracker);
1528
+ else throw new RuntimeError("index expects string or array", span);
1529
+ }
1530
+ },
1531
+ {
1532
+ name: "rindex",
1533
+ arity: 1,
1534
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1535
+ const searchGen = evaluate$1(args[0], input, env, tracker);
1536
+ for (const search of searchGen) if (Array.isArray(input)) {
1537
+ let foundIndex = null;
1538
+ for (let i = input.length - 1; i >= 0; i--) {
1539
+ const val = input[i];
1540
+ if (val !== void 0 && valueEquals(val, search)) {
1541
+ foundIndex = i;
1542
+ break;
1543
+ }
1544
+ }
1545
+ yield emit$1(foundIndex, span, tracker);
1546
+ } else if (typeof input === "string") {
1547
+ if (typeof search !== "string") throw new RuntimeError("rindex expects string search when input is string", span);
1548
+ const idx = input.lastIndexOf(search);
1549
+ yield emit$1(idx === -1 ? null : idx, span, tracker);
1550
+ } else if (input === null) yield emit$1(null, span, tracker);
1551
+ else throw new RuntimeError("rindex expects string or array", span);
1552
+ }
1553
+ },
1554
+ {
1555
+ name: "indices",
1556
+ arity: 1,
1557
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1558
+ const searchGen = evaluate$1(args[0], input, env, tracker);
1559
+ for (const search of searchGen) {
1560
+ const indices = [];
1561
+ if (Array.isArray(input)) for (let i = 0; i < input.length; i++) {
1562
+ const val = input[i];
1563
+ if (val !== void 0 && valueEquals(val, search)) indices.push(i);
1564
+ }
1565
+ else if (typeof input === "string") {
1566
+ if (typeof search !== "string") throw new RuntimeError("indices expects string", span);
1567
+ if (search.length === 0) {} else {
1568
+ let pos = 0;
1569
+ while (pos < input.length) {
1570
+ const idx = input.indexOf(search, pos);
1571
+ if (idx === -1) break;
1572
+ indices.push(idx);
1573
+ pos = idx + 1;
1574
+ pos = idx + search.length;
1575
+ }
1576
+ }
1577
+ } else {
1578
+ if (input === null) {
1579
+ yield emit$1(null, span, tracker);
1580
+ continue;
1581
+ }
1582
+ throw new RuntimeError("indices expects string or array", span);
1583
+ }
1584
+ yield emit$1(indices, span, tracker);
1585
+ }
1586
+ }
1587
+ },
1588
+ {
1589
+ name: "explode",
1590
+ arity: 0,
1591
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1592
+ if (typeof input !== "string") throw new RuntimeError("explode expects string", span);
1593
+ yield emit$1(Array.from(input).map((c) => c.codePointAt(0)), span, tracker);
1594
+ }
1595
+ },
1596
+ {
1597
+ name: "implode",
1598
+ arity: 0,
1599
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1600
+ if (!Array.isArray(input)) throw new RuntimeError("implode expects array", span);
1601
+ const chars = [];
1602
+ for (const item of input) {
1603
+ if (typeof item !== "number") throw new RuntimeError("implode item must be number", span);
1604
+ chars.push(String.fromCodePoint(item));
1605
+ }
1606
+ yield emit$1(chars.join(""), span, tracker);
1607
+ }
1608
+ },
1609
+ {
1610
+ name: "ltrimstr",
1611
+ arity: 1,
1612
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1613
+ if (typeof input !== "string") throw new RuntimeError("ltrimstr expects string", span);
1614
+ const prefixGen = evaluate$1(args[0], input, env, tracker);
1615
+ for (const prefix of prefixGen) {
1616
+ if (typeof prefix !== "string") throw new RuntimeError("ltrimstr prefix must be a string", span);
1617
+ if (input.startsWith(prefix)) yield emit$1(input.slice(prefix.length), span, tracker);
1618
+ else yield emit$1(input, span, tracker);
1619
+ }
1620
+ }
1621
+ },
1622
+ {
1623
+ name: "rtrimstr",
1624
+ arity: 1,
1625
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1626
+ if (typeof input !== "string") throw new RuntimeError("rtrimstr expects string", span);
1627
+ const suffixGen = evaluate$1(args[0], input, env, tracker);
1628
+ for (const suffix of suffixGen) {
1629
+ if (typeof suffix !== "string") throw new RuntimeError("rtrimstr suffix must be a string", span);
1630
+ if (input.endsWith(suffix)) yield emit$1(input.slice(0, input.length - suffix.length), span, tracker);
1631
+ else yield emit$1(input, span, tracker);
1632
+ }
1633
+ }
1634
+ },
1635
+ {
1636
+ name: "ascii_downcase",
1637
+ arity: 0,
1638
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1639
+ if (typeof input !== "string") throw new RuntimeError("ascii_downcase expects string", span);
1640
+ yield emit$1(input.toLowerCase(), span, tracker);
1641
+ }
1642
+ },
1643
+ {
1644
+ name: "ascii_upcase",
1645
+ arity: 0,
1646
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1647
+ if (typeof input !== "string") throw new RuntimeError("ascii_upcase expects string", span);
1648
+ yield emit$1(input.toUpperCase(), span, tracker);
1649
+ }
1650
+ }
1651
+ ];
1652
+
1363
1653
  //#endregion
1364
1654
  //#region src/builtins/collections.ts
1365
1655
  function sortStable(arr, compare$1) {
@@ -1554,189 +1844,207 @@ const collectionBuiltins = [
1554
1844
  }
1555
1845
  yield emit$1(result, span, tracker);
1556
1846
  }
1557
- }
1558
- ];
1559
-
1560
- //#endregion
1561
- //#region src/builtins/strings.ts
1562
- const checkContains = (a, b) => {
1563
- if (a === b) return true;
1564
- if (typeof a !== typeof b) return false;
1565
- if (typeof a === "string" && typeof b === "string") return a.includes(b);
1566
- if (Array.isArray(a) && Array.isArray(b)) return b.every((bItem) => a.some((aItem) => checkContains(aItem, bItem)));
1567
- if (isPlainObject(a) && isPlainObject(b)) {
1568
- const keys = Object.keys(b);
1569
- for (const key of keys) {
1570
- if (!Object.prototype.hasOwnProperty.call(a, key)) return false;
1571
- const valA = a[key];
1572
- const valB = b[key];
1573
- if (!checkContains(valA, valB)) return false;
1574
- }
1575
- return true;
1576
- }
1577
- return valueEquals(a, b);
1578
- };
1579
- const stringBuiltins = [
1847
+ },
1580
1848
  {
1581
- name: "split",
1849
+ name: "group_by",
1582
1850
  arity: 1,
1583
1851
  apply: function* (input, args, env, tracker, evaluate$1, span) {
1584
- if (typeof input !== "string") throw new RuntimeError("split input must be a string", span);
1585
- const sepGen = evaluate$1(args[0], input, env, tracker);
1586
- for (const sep of sepGen) {
1587
- if (typeof sep !== "string") throw new RuntimeError("split separator must be a string", span);
1588
- yield emit$1(input.split(sep), span, tracker);
1852
+ if (!Array.isArray(input)) throw new RuntimeError("group_by expects an array", span);
1853
+ const pairs = [];
1854
+ const filter = args[0];
1855
+ for (const item of input) {
1856
+ tracker.step(span);
1857
+ const keys = Array.from(evaluate$1(filter, item, env, tracker));
1858
+ if (keys.length !== 1) throw new RuntimeError("group_by key expression must return exactly one value", span);
1859
+ pairs.push({
1860
+ val: item,
1861
+ key: keys[0]
1862
+ });
1863
+ }
1864
+ const sorted = sortStable(pairs, (a, b) => compareValues(a.key, b.key));
1865
+ const groups = [];
1866
+ if (sorted.length > 0) {
1867
+ let currentGroup = [sorted[0].val];
1868
+ let currentKey = sorted[0].key;
1869
+ for (let i = 1; i < sorted.length; i++) {
1870
+ const pair = sorted[i];
1871
+ if (compareValues(pair.key, currentKey) === 0) currentGroup.push(pair.val);
1872
+ else {
1873
+ groups.push(currentGroup);
1874
+ currentGroup = [pair.val];
1875
+ currentKey = pair.key;
1876
+ }
1877
+ }
1878
+ groups.push(currentGroup);
1589
1879
  }
1880
+ yield emit$1(groups, span, tracker);
1590
1881
  }
1591
1882
  },
1592
1883
  {
1593
- name: "join",
1594
- arity: 1,
1595
- apply: function* (input, args, env, tracker, evaluate$1, span) {
1596
- if (!Array.isArray(input)) throw new RuntimeError("join input must be an array", span);
1597
- const sepGen = evaluate$1(args[0], input, env, tracker);
1598
- for (const sep of sepGen) {
1599
- if (typeof sep !== "string") throw new RuntimeError("join separator must be a string", span);
1600
- const parts = [];
1601
- for (const item of input) {
1602
- if (typeof item !== "string") throw new RuntimeError(`join expects strings, but got ${describeType(item)}`, span);
1603
- parts.push(item);
1604
- }
1605
- yield emit$1(parts.join(sep), span, tracker);
1606
- }
1884
+ name: "reverse",
1885
+ arity: 0,
1886
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1887
+ if (!Array.isArray(input)) throw new RuntimeError("reverse expects an array", span);
1888
+ yield emit$1([...input].reverse(), span, tracker);
1607
1889
  }
1608
1890
  },
1609
1891
  {
1610
- name: "startswith",
1611
- arity: 1,
1612
- apply: function* (input, args, env, tracker, evaluate$1, span) {
1613
- if (typeof input !== "string") throw new RuntimeError("startswith input must be a string", span);
1614
- const prefixGen = evaluate$1(args[0], input, env, tracker);
1615
- for (const prefix of prefixGen) {
1616
- if (typeof prefix !== "string") throw new RuntimeError("startswith prefix must be a string", span);
1617
- yield emit$1(input.startsWith(prefix), span, tracker);
1618
- }
1892
+ name: "flatten",
1893
+ arity: 0,
1894
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1895
+ if (!Array.isArray(input)) throw new RuntimeError("flatten expects an array", span);
1896
+ const flattenRec = (arr) => {
1897
+ let res = [];
1898
+ for (const item of arr) if (Array.isArray(item)) res = res.concat(flattenRec(item));
1899
+ else res.push(item);
1900
+ return res;
1901
+ };
1902
+ yield emit$1(flattenRec(input), span, tracker);
1619
1903
  }
1620
1904
  },
1621
1905
  {
1622
- name: "endswith",
1906
+ name: "flatten",
1623
1907
  arity: 1,
1624
1908
  apply: function* (input, args, env, tracker, evaluate$1, span) {
1625
- if (typeof input !== "string") throw new RuntimeError("endswith input must be a string", span);
1626
- const suffixGen = evaluate$1(args[0], input, env, tracker);
1627
- for (const suffix of suffixGen) {
1628
- if (typeof suffix !== "string") throw new RuntimeError("endswith suffix must be a string", span);
1629
- yield emit$1(input.endsWith(suffix), span, tracker);
1909
+ if (!Array.isArray(input)) throw new RuntimeError("flatten expects an array", span);
1910
+ const depths = evaluate$1(args[0], input, env, tracker);
1911
+ for (const depthVal of depths) {
1912
+ if (typeof depthVal !== "number") throw new RuntimeError("flatten depth must be a number", span);
1913
+ const flattenDepth = (arr, d) => {
1914
+ if (d <= 0) return arr;
1915
+ let res = [];
1916
+ for (const item of arr) if (Array.isArray(item)) res = res.concat(flattenDepth(item, d - 1));
1917
+ else res.push(item);
1918
+ return res;
1919
+ };
1920
+ yield emit$1(flattenDepth(input, depthVal), span, tracker);
1630
1921
  }
1631
1922
  }
1632
1923
  },
1633
1924
  {
1634
- name: "contains",
1635
- arity: 1,
1636
- apply: function* (input, args, env, tracker, evaluate$1, span) {
1637
- const bGen = evaluate$1(args[0], input, env, tracker);
1638
- for (const b of bGen) yield emit$1(checkContains(input, b), span, tracker);
1925
+ name: "keys_unsorted",
1926
+ arity: 0,
1927
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1928
+ if (Array.isArray(input)) yield emit$1(Array.from({ length: input.length }, (_, i) => i), span, tracker);
1929
+ else if (input !== null && typeof input === "object") yield emit$1(Object.keys(input), span, tracker);
1930
+ else throw new RuntimeError(`keys_unsorted expects an array or object`, span);
1639
1931
  }
1640
1932
  },
1641
1933
  {
1642
- name: "index",
1643
- arity: 1,
1644
- apply: function* (input, args, env, tracker, evaluate$1, span) {
1645
- const searchGen = evaluate$1(args[0], input, env, tracker);
1646
- for (const search of searchGen) if (Array.isArray(input)) {
1647
- let foundIndex = null;
1648
- for (let i = 0; i < input.length; i++) {
1649
- const val = input[i];
1650
- if (val !== void 0 && valueEquals(val, search)) {
1651
- foundIndex = i;
1652
- break;
1653
- }
1934
+ name: "transpose",
1935
+ arity: 0,
1936
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1937
+ if (!Array.isArray(input)) throw new RuntimeError("transpose expects an array", span);
1938
+ const arr = input;
1939
+ if (arr.length === 0) {
1940
+ yield emit$1([], span, tracker);
1941
+ return;
1942
+ }
1943
+ let maxLen = 0;
1944
+ for (const row of arr) {
1945
+ if (!Array.isArray(row)) throw new RuntimeError("transpose input must be array of arrays", span);
1946
+ if (row.length > maxLen) maxLen = row.length;
1947
+ }
1948
+ const result = [];
1949
+ for (let j = 0; j < maxLen; j++) {
1950
+ const newRow = [];
1951
+ for (let i = 0; i < arr.length; i++) {
1952
+ const row = arr[i];
1953
+ const val = j < row.length ? row[j] : null;
1954
+ newRow.push(val);
1654
1955
  }
1655
- yield emit$1(foundIndex, span, tracker);
1656
- } else if (typeof input === "string") {
1657
- if (typeof search !== "string") throw new RuntimeError("index expects string search when input is string", span);
1658
- const idx = input.indexOf(search);
1659
- yield emit$1(idx === -1 ? null : idx, span, tracker);
1660
- } else if (input === null) yield emit$1(null, span, tracker);
1661
- else throw new RuntimeError("index expects string or array", span);
1956
+ result.push(newRow);
1957
+ }
1958
+ yield emit$1(result, span, tracker);
1662
1959
  }
1663
1960
  },
1664
1961
  {
1665
- name: "rindex",
1962
+ name: "bsearch",
1666
1963
  arity: 1,
1667
1964
  apply: function* (input, args, env, tracker, evaluate$1, span) {
1668
- const searchGen = evaluate$1(args[0], input, env, tracker);
1669
- for (const search of searchGen) if (Array.isArray(input)) {
1670
- let foundIndex = null;
1671
- for (let i = input.length - 1; i >= 0; i--) {
1672
- const val = input[i];
1673
- if (val !== void 0 && valueEquals(val, search)) {
1674
- foundIndex = i;
1965
+ if (!Array.isArray(input)) throw new RuntimeError("bsearch expects an array", span);
1966
+ const targetGen = evaluate$1(args[0], input, env, tracker);
1967
+ for (const target of targetGen) {
1968
+ let low = 0;
1969
+ let high = input.length - 1;
1970
+ let idx = -1;
1971
+ while (low <= high) {
1972
+ const mid = Math.floor((low + high) / 2);
1973
+ const cmp = compareValues(input[mid], target);
1974
+ if (cmp === 0) {
1975
+ idx = mid;
1675
1976
  break;
1676
- }
1977
+ } else if (cmp < 0) low = mid + 1;
1978
+ else high = mid - 1;
1677
1979
  }
1678
- yield emit$1(foundIndex, span, tracker);
1679
- } else if (typeof input === "string") {
1680
- if (typeof search !== "string") throw new RuntimeError("rindex expects string search when input is string", span);
1681
- const idx = input.lastIndexOf(search);
1682
- yield emit$1(idx === -1 ? null : idx, span, tracker);
1683
- } else if (input === null) yield emit$1(null, span, tracker);
1684
- else throw new RuntimeError("rindex expects string or array", span);
1980
+ if (idx !== -1) yield emit$1(idx, span, tracker);
1981
+ else yield emit$1(-low - 1, span, tracker);
1982
+ }
1685
1983
  }
1686
1984
  },
1687
1985
  {
1688
- name: "indices",
1986
+ name: "combinations",
1987
+ arity: 0,
1988
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1989
+ if (!Array.isArray(input)) throw new RuntimeError("combinations expects an array", span);
1990
+ if (input.some((x) => !Array.isArray(x))) throw new RuntimeError("combinations input must be array of arrays", span);
1991
+ const arrays = input;
1992
+ if (arrays.length === 0) {
1993
+ yield emit$1([], span, tracker);
1994
+ return;
1995
+ }
1996
+ const helper = function* (idx, current) {
1997
+ if (idx === arrays.length) {
1998
+ yield [...current];
1999
+ return;
2000
+ }
2001
+ const arr = arrays[idx];
2002
+ if (arr.length === 0) return;
2003
+ for (const item of arr) {
2004
+ current.push(item);
2005
+ yield* helper(idx + 1, current);
2006
+ current.pop();
2007
+ }
2008
+ };
2009
+ for (const combo of helper(0, [])) yield emit$1(combo, span, tracker);
2010
+ }
2011
+ },
2012
+ {
2013
+ name: "combinations",
1689
2014
  arity: 1,
1690
2015
  apply: function* (input, args, env, tracker, evaluate$1, span) {
1691
- const searchGen = evaluate$1(args[0], input, env, tracker);
1692
- for (const search of searchGen) {
1693
- const indices = [];
1694
- if (Array.isArray(input)) for (let i = 0; i < input.length; i++) {
1695
- const val = input[i];
1696
- if (val !== void 0 && valueEquals(val, search)) indices.push(i);
2016
+ if (!Array.isArray(input)) throw new RuntimeError("combinations expects an array", span);
2017
+ const nGen = evaluate$1(args[0], input, env, tracker);
2018
+ for (const nVal of nGen) {
2019
+ if (typeof nVal !== "number") throw new RuntimeError("combinations(n) expects n to be number", span);
2020
+ if (nVal === 0) {
2021
+ yield emit$1([], span, tracker);
2022
+ continue;
1697
2023
  }
1698
- else if (typeof input === "string") {
1699
- if (typeof search !== "string") throw new RuntimeError("indices expects string", span);
1700
- if (search.length === 0) {} else {
1701
- let pos = 0;
1702
- while (pos < input.length) {
1703
- const idx = input.indexOf(search, pos);
1704
- if (idx === -1) break;
1705
- indices.push(idx);
1706
- pos = idx + 1;
1707
- pos = idx + search.length;
1708
- }
2024
+ const arrays = [];
2025
+ for (let i = 0; i < nVal; i++) arrays.push(input);
2026
+ const helper = function* (idx, current) {
2027
+ if (idx === arrays.length) {
2028
+ yield [...current];
2029
+ return;
1709
2030
  }
1710
- } else {
1711
- if (input === null) {
1712
- yield emit$1(null, span, tracker);
1713
- continue;
2031
+ const arr = arrays[idx];
2032
+ for (const item of arr) {
2033
+ current.push(item);
2034
+ yield* helper(idx + 1, current);
2035
+ current.pop();
1714
2036
  }
1715
- throw new RuntimeError("indices expects string or array", span);
1716
- }
1717
- yield emit$1(indices, span, tracker);
2037
+ };
2038
+ if (input.length === 0 && nVal > 0) {} else for (const combo of helper(0, [])) yield emit$1(combo, span, tracker);
1718
2039
  }
1719
2040
  }
1720
2041
  },
1721
2042
  {
1722
- name: "explode",
1723
- arity: 0,
1724
- apply: function* (input, _args, _env, tracker, _eval, span) {
1725
- if (typeof input !== "string") throw new RuntimeError("explode expects string", span);
1726
- yield emit$1(Array.from(input).map((c) => c.codePointAt(0)), span, tracker);
1727
- }
1728
- },
1729
- {
1730
- name: "implode",
1731
- arity: 0,
1732
- apply: function* (input, _args, _env, tracker, _eval, span) {
1733
- if (!Array.isArray(input)) throw new RuntimeError("implode expects array", span);
1734
- const chars = [];
1735
- for (const item of input) {
1736
- if (typeof item !== "number") throw new RuntimeError("implode item must be number", span);
1737
- chars.push(String.fromCodePoint(item));
1738
- }
1739
- yield emit$1(chars.join(""), span, tracker);
2043
+ name: "inside",
2044
+ arity: 1,
2045
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
2046
+ const bGen = evaluate$1(args[0], input, env, tracker);
2047
+ for (const b of bGen) yield emit$1(checkContains(b, input), span, tracker);
1740
2048
  }
1741
2049
  }
1742
2050
  ];
@@ -2110,8 +2418,216 @@ const iteratorBuiltins = [
2110
2418
  };
2111
2419
  yield* rec(input);
2112
2420
  }
2421
+ },
2422
+ {
2423
+ name: "while",
2424
+ arity: 2,
2425
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
2426
+ const condExpr = args[0];
2427
+ const updateExpr = args[1];
2428
+ const rec = function* (curr) {
2429
+ tracker.step(span);
2430
+ let condMatches = false;
2431
+ for (const c of evaluate$1(condExpr, curr, env, tracker)) if (isTruthy(c)) {
2432
+ condMatches = true;
2433
+ break;
2434
+ }
2435
+ if (condMatches) {
2436
+ yield emit$1(curr, span, tracker);
2437
+ for (const next of evaluate$1(updateExpr, curr, env, tracker)) yield* rec(next);
2438
+ }
2439
+ };
2440
+ yield* rec(input);
2441
+ }
2442
+ },
2443
+ {
2444
+ name: "until",
2445
+ arity: 2,
2446
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
2447
+ const condExpr = args[0];
2448
+ const updateExpr = args[1];
2449
+ const rec = function* (curr) {
2450
+ tracker.step(span);
2451
+ let condMatches = false;
2452
+ for (const c of evaluate$1(condExpr, curr, env, tracker)) if (isTruthy(c)) {
2453
+ condMatches = true;
2454
+ break;
2455
+ }
2456
+ if (condMatches) yield emit$1(curr, span, tracker);
2457
+ else for (const next of evaluate$1(updateExpr, curr, env, tracker)) yield* rec(next);
2458
+ };
2459
+ yield* rec(input);
2460
+ }
2461
+ },
2462
+ {
2463
+ name: "repeat",
2464
+ arity: 1,
2465
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
2466
+ while (true) {
2467
+ tracker.step(span);
2468
+ yield* evaluate$1(args[0], input, env, tracker);
2469
+ }
2470
+ }
2471
+ }
2472
+ ];
2473
+
2474
+ //#endregion
2475
+ //#region src/eval/ops.ts
2476
+ /**
2477
+ * Applies a unary negation (`-`).
2478
+ *
2479
+ * @param value - The value to negate.
2480
+ * @param span - Source span for errors.
2481
+ * @returns The negated value.
2482
+ */
2483
+ const applyUnaryNeg = (value, span) => {
2484
+ if (typeof value === "number") return -value;
2485
+ throw new RuntimeError(`Invalid operand for unary -: ${describeType(value)}`, span);
2486
+ };
2487
+ /**
2488
+ * Applies a binary operator.
2489
+ *
2490
+ * Supports arithmetic (`+`, `-`, `*`, `/`, `%`) and comparators.
2491
+ *
2492
+ * @param op - The operator string.
2493
+ * @param left - The left operand.
2494
+ * @param right - The right operand.
2495
+ * @param span - Source span for errors.
2496
+ * @returns The result of the operation.
2497
+ */
2498
+ const applyBinaryOp = (op, left, right, span) => {
2499
+ switch (op) {
2500
+ case "+": return add(left, right, span);
2501
+ case "-": return sub(left, right, span);
2502
+ case "*": return mul(left, right, span);
2503
+ case "/": return div(left, right, span);
2504
+ case "%": return mod(left, right, span);
2505
+ case "Eq": return isEqual(left, right);
2506
+ case "Neq": return !isEqual(left, right);
2507
+ case "Lt": return compare(left, right) < 0;
2508
+ case "Lte": return compare(left, right) <= 0;
2509
+ case "Gt": return compare(left, right) > 0;
2510
+ case "Gte": return compare(left, right) >= 0;
2511
+ default: throw new RuntimeError(`Unknown binary operator: ${op}`, span);
2512
+ }
2513
+ };
2514
+ function isEqual(a, b) {
2515
+ if (a === b) return true;
2516
+ if (a === null || b === null) return false;
2517
+ if (typeof a !== typeof b) return false;
2518
+ if (Array.isArray(a) && Array.isArray(b)) {
2519
+ if (a.length !== b.length) return false;
2520
+ return a.every((v, i) => isEqual(v, b[i]));
2521
+ }
2522
+ if (isPlainObject$1(a) && isPlainObject$1(b)) {
2523
+ const ka = Object.keys(a).sort();
2524
+ const kb = Object.keys(b).sort();
2525
+ if (ka.length !== kb.length) return false;
2526
+ if (!ka.every((k, i) => k === kb[i])) return false;
2527
+ return ka.every((k) => isEqual(a[k], b[k]));
2528
+ }
2529
+ return false;
2530
+ }
2531
+ function compare(a, b) {
2532
+ if (a === b) return 0;
2533
+ const typeOrder = (v) => {
2534
+ if (v === null) return 0;
2535
+ if (typeof v === "boolean") return 1;
2536
+ if (typeof v === "number") return 2;
2537
+ if (typeof v === "string") return 3;
2538
+ if (Array.isArray(v)) return 4;
2539
+ if (isPlainObject$1(v)) return 5;
2540
+ return 6;
2541
+ };
2542
+ const ta = typeOrder(a);
2543
+ const tb = typeOrder(b);
2544
+ if (ta !== tb) return ta - tb;
2545
+ if (typeof a === "boolean" && typeof b === "boolean") return (a ? 1 : 0) - (b ? 1 : 0);
2546
+ if (typeof a === "number" && typeof b === "number") return a - b;
2547
+ if (typeof a === "string" && typeof b === "string") return a < b ? -1 : 1;
2548
+ if (Array.isArray(a) && Array.isArray(b)) {
2549
+ for (let i = 0; i < Math.min(a.length, b.length); i++) {
2550
+ const c = compare(a[i], b[i]);
2551
+ if (c !== 0) return c;
2552
+ }
2553
+ return a.length - b.length;
2113
2554
  }
2114
- ];
2555
+ if (isPlainObject$1(a) && isPlainObject$1(b)) {
2556
+ const keysA = Object.keys(a).sort();
2557
+ const keysB = Object.keys(b).sort();
2558
+ for (let i = 0; i < Math.min(keysA.length, keysB.length); i++) {
2559
+ const kA = keysA[i];
2560
+ const kB = keysB[i];
2561
+ if (kA !== kB) return kA < kB ? -1 : 1;
2562
+ const c = compare(a[kA], b[kB]);
2563
+ if (c !== 0) return c;
2564
+ }
2565
+ return keysA.length - keysB.length;
2566
+ }
2567
+ return 0;
2568
+ }
2569
+ function add(left, right, span) {
2570
+ if (left === null && right === null) return null;
2571
+ if (left === null && typeof right === "number") return right;
2572
+ if (typeof left === "number" && right === null) return left;
2573
+ if (typeof left === "number" && typeof right === "number") return left + right;
2574
+ if (typeof left === "string" && typeof right === "string") return left + right;
2575
+ if (Array.isArray(left) && Array.isArray(right)) return [...left, ...right];
2576
+ if (isPlainObject$1(left) && isPlainObject$1(right)) return {
2577
+ ...left,
2578
+ ...right
2579
+ };
2580
+ throw new RuntimeError(`Cannot add ${describeType(left)} and ${describeType(right)}`, span);
2581
+ }
2582
+ function sub(left, right, span) {
2583
+ if (typeof left === "number" && typeof right === "number") return left - right;
2584
+ if (Array.isArray(left) && Array.isArray(right)) {
2585
+ const toRemove = new Set(right.map(stableStringify));
2586
+ return left.filter((x) => !toRemove.has(stableStringify(x)));
2587
+ }
2588
+ throw new RuntimeError(`Cannot subtract ${describeType(right)} from ${describeType(left)}`, span);
2589
+ }
2590
+ function mul(left, right, span) {
2591
+ if (typeof left === "number" && typeof right === "number") return left * right;
2592
+ if (typeof left === "string" && typeof right === "number") return repeatString(left, right);
2593
+ if (typeof left === "number" && typeof right === "string") return repeatString(right, left);
2594
+ if (isPlainObject$1(left) && isPlainObject$1(right)) return mergeDeep(left, right);
2595
+ throw new RuntimeError(`Cannot multiply ${describeType(left)} by ${describeType(right)}`, span);
2596
+ }
2597
+ function div(left, right, span) {
2598
+ if (typeof left === "number" && typeof right === "number") {
2599
+ if (right === 0) throw new RuntimeError("Division by zero", span);
2600
+ return left / right;
2601
+ }
2602
+ if (typeof left === "string" && typeof right === "string") return left.split(right);
2603
+ throw new RuntimeError(`Cannot divide ${describeType(left)} by ${describeType(right)}`, span);
2604
+ }
2605
+ function mod(left, right, span) {
2606
+ if (typeof left === "number" && typeof right === "number") {
2607
+ if (right === 0) throw new RuntimeError("Modulo by zero", span);
2608
+ return left % right;
2609
+ }
2610
+ throw new RuntimeError(`Cannot modulo ${describeType(left)} by ${describeType(right)}`, span);
2611
+ }
2612
+ function repeatString(str, count) {
2613
+ if (count <= 0) return null;
2614
+ const n = Math.floor(count);
2615
+ if (n <= 0) return null;
2616
+ return str.repeat(n);
2617
+ }
2618
+ function mergeDeep(target, source) {
2619
+ const result = { ...target };
2620
+ for (const key of Object.keys(source)) {
2621
+ const sVal = source[key];
2622
+ const tVal = result[key];
2623
+ if (isPlainObject$1(sVal) && tVal !== void 0 && isPlainObject$1(tVal)) result[key] = mergeDeep(tVal, sVal);
2624
+ else result[key] = sVal;
2625
+ }
2626
+ return result;
2627
+ }
2628
+ function isPlainObject$1(v) {
2629
+ return typeof v === "object" && v !== null && !Array.isArray(v);
2630
+ }
2115
2631
 
2116
2632
  //#endregion
2117
2633
  //#region src/builtins/math.ts
@@ -2296,6 +2812,23 @@ const mathBuiltins = [
2296
2812
  }
2297
2813
  yield emit$1(maxItem, span, tracker);
2298
2814
  }
2815
+ },
2816
+ {
2817
+ name: "add",
2818
+ arity: 0,
2819
+ apply: function* (input, _args, _env, tracker, _eval, span) {
2820
+ if (!Array.isArray(input)) throw new RuntimeError("add expects an array", span);
2821
+ if (input.length === 0) {
2822
+ yield emit$1(null, span, tracker);
2823
+ return;
2824
+ }
2825
+ let acc = input[0];
2826
+ for (let i = 1; i < input.length; i++) {
2827
+ tracker.step(span);
2828
+ acc = add(acc, input[i], span);
2829
+ }
2830
+ yield emit$1(acc, span, tracker);
2831
+ }
2299
2832
  }
2300
2833
  ];
2301
2834
 
@@ -2504,164 +3037,6 @@ var LimitTracker = class {
2504
3037
  }
2505
3038
  };
2506
3039
 
2507
- //#endregion
2508
- //#region src/eval/ops.ts
2509
- /**
2510
- * Applies a unary negation (`-`).
2511
- *
2512
- * @param value - The value to negate.
2513
- * @param span - Source span for errors.
2514
- * @returns The negated value.
2515
- */
2516
- const applyUnaryNeg = (value, span) => {
2517
- if (typeof value === "number") return -value;
2518
- throw new RuntimeError(`Invalid operand for unary -: ${describeType(value)}`, span);
2519
- };
2520
- /**
2521
- * Applies a binary operator.
2522
- *
2523
- * Supports arithmetic (`+`, `-`, `*`, `/`, `%`) and comparators.
2524
- *
2525
- * @param op - The operator string.
2526
- * @param left - The left operand.
2527
- * @param right - The right operand.
2528
- * @param span - Source span for errors.
2529
- * @returns The result of the operation.
2530
- */
2531
- const applyBinaryOp = (op, left, right, span) => {
2532
- switch (op) {
2533
- case "+": return add(left, right, span);
2534
- case "-": return sub(left, right, span);
2535
- case "*": return mul(left, right, span);
2536
- case "/": return div(left, right, span);
2537
- case "%": return mod(left, right, span);
2538
- case "Eq": return isEqual(left, right);
2539
- case "Neq": return !isEqual(left, right);
2540
- case "Lt": return compare(left, right) < 0;
2541
- case "Lte": return compare(left, right) <= 0;
2542
- case "Gt": return compare(left, right) > 0;
2543
- case "Gte": return compare(left, right) >= 0;
2544
- default: throw new RuntimeError(`Unknown binary operator: ${op}`, span);
2545
- }
2546
- };
2547
- function isEqual(a, b) {
2548
- if (a === b) return true;
2549
- if (a === null || b === null) return false;
2550
- if (typeof a !== typeof b) return false;
2551
- if (Array.isArray(a) && Array.isArray(b)) {
2552
- if (a.length !== b.length) return false;
2553
- return a.every((v, i) => isEqual(v, b[i]));
2554
- }
2555
- if (isPlainObject$1(a) && isPlainObject$1(b)) {
2556
- const ka = Object.keys(a).sort();
2557
- const kb = Object.keys(b).sort();
2558
- if (ka.length !== kb.length) return false;
2559
- if (!ka.every((k, i) => k === kb[i])) return false;
2560
- return ka.every((k) => isEqual(a[k], b[k]));
2561
- }
2562
- return false;
2563
- }
2564
- function compare(a, b) {
2565
- if (a === b) return 0;
2566
- const typeOrder = (v) => {
2567
- if (v === null) return 0;
2568
- if (typeof v === "boolean") return 1;
2569
- if (typeof v === "number") return 2;
2570
- if (typeof v === "string") return 3;
2571
- if (Array.isArray(v)) return 4;
2572
- if (isPlainObject$1(v)) return 5;
2573
- return 6;
2574
- };
2575
- const ta = typeOrder(a);
2576
- const tb = typeOrder(b);
2577
- if (ta !== tb) return ta - tb;
2578
- if (typeof a === "boolean" && typeof b === "boolean") return (a ? 1 : 0) - (b ? 1 : 0);
2579
- if (typeof a === "number" && typeof b === "number") return a - b;
2580
- if (typeof a === "string" && typeof b === "string") return a < b ? -1 : 1;
2581
- if (Array.isArray(a) && Array.isArray(b)) {
2582
- for (let i = 0; i < Math.min(a.length, b.length); i++) {
2583
- const c = compare(a[i], b[i]);
2584
- if (c !== 0) return c;
2585
- }
2586
- return a.length - b.length;
2587
- }
2588
- if (isPlainObject$1(a) && isPlainObject$1(b)) {
2589
- const keysA = Object.keys(a).sort();
2590
- const keysB = Object.keys(b).sort();
2591
- for (let i = 0; i < Math.min(keysA.length, keysB.length); i++) {
2592
- const kA = keysA[i];
2593
- const kB = keysB[i];
2594
- if (kA !== kB) return kA < kB ? -1 : 1;
2595
- const c = compare(a[kA], b[kB]);
2596
- if (c !== 0) return c;
2597
- }
2598
- return keysA.length - keysB.length;
2599
- }
2600
- return 0;
2601
- }
2602
- function add(left, right, span) {
2603
- if (left === null && right === null) return null;
2604
- if (left === null && typeof right === "number") return right;
2605
- if (typeof left === "number" && right === null) return left;
2606
- if (typeof left === "number" && typeof right === "number") return left + right;
2607
- if (typeof left === "string" && typeof right === "string") return left + right;
2608
- if (Array.isArray(left) && Array.isArray(right)) return [...left, ...right];
2609
- if (isPlainObject$1(left) && isPlainObject$1(right)) return {
2610
- ...left,
2611
- ...right
2612
- };
2613
- throw new RuntimeError(`Cannot add ${describeType(left)} and ${describeType(right)}`, span);
2614
- }
2615
- function sub(left, right, span) {
2616
- if (typeof left === "number" && typeof right === "number") return left - right;
2617
- if (Array.isArray(left) && Array.isArray(right)) {
2618
- const toRemove = new Set(right.map(stableStringify));
2619
- return left.filter((x) => !toRemove.has(stableStringify(x)));
2620
- }
2621
- throw new RuntimeError(`Cannot subtract ${describeType(right)} from ${describeType(left)}`, span);
2622
- }
2623
- function mul(left, right, span) {
2624
- if (typeof left === "number" && typeof right === "number") return left * right;
2625
- if (typeof left === "string" && typeof right === "number") return repeatString(left, right);
2626
- if (typeof left === "number" && typeof right === "string") return repeatString(right, left);
2627
- if (isPlainObject$1(left) && isPlainObject$1(right)) return mergeDeep(left, right);
2628
- throw new RuntimeError(`Cannot multiply ${describeType(left)} by ${describeType(right)}`, span);
2629
- }
2630
- function div(left, right, span) {
2631
- if (typeof left === "number" && typeof right === "number") {
2632
- if (right === 0) throw new RuntimeError("Division by zero", span);
2633
- return left / right;
2634
- }
2635
- if (typeof left === "string" && typeof right === "string") return left.split(right);
2636
- throw new RuntimeError(`Cannot divide ${describeType(left)} by ${describeType(right)}`, span);
2637
- }
2638
- function mod(left, right, span) {
2639
- if (typeof left === "number" && typeof right === "number") {
2640
- if (right === 0) throw new RuntimeError("Modulo by zero", span);
2641
- return left % right;
2642
- }
2643
- throw new RuntimeError(`Cannot modulo ${describeType(left)} by ${describeType(right)}`, span);
2644
- }
2645
- function repeatString(str, count) {
2646
- if (count <= 0) return null;
2647
- const n = Math.floor(count);
2648
- if (n <= 0) return null;
2649
- return str.repeat(n);
2650
- }
2651
- function mergeDeep(target, source) {
2652
- const result = { ...target };
2653
- for (const key of Object.keys(source)) {
2654
- const sVal = source[key];
2655
- const tVal = result[key];
2656
- if (isPlainObject$1(sVal) && tVal !== void 0 && isPlainObject$1(tVal)) result[key] = mergeDeep(tVal, sVal);
2657
- else result[key] = sVal;
2658
- }
2659
- return result;
2660
- }
2661
- function isPlainObject$1(v) {
2662
- return typeof v === "object" && v !== null && !Array.isArray(v);
2663
- }
2664
-
2665
3040
  //#endregion
2666
3041
  //#region src/eval/env.ts
2667
3042
  /**
@@ -2727,6 +3102,7 @@ const ensureInteger = (value, span) => {
2727
3102
  */
2728
3103
  const evalAssignment = function* (node, input, env, tracker, evaluate$1) {
2729
3104
  const paths = Array.from(evaluatePath(node.left, input, env, tracker, evaluate$1));
3105
+ paths.sort((a, b) => compareValues(a, b) * -1);
2730
3106
  if (paths.length === 0) {
2731
3107
  yield emit(input, node.span, tracker);
2732
3108
  return;
@@ -2780,7 +3156,10 @@ function* applyUpdates(current, paths, index, op, rhsNode, contextInput, env, tr
2780
3156
  newValues.push(res);
2781
3157
  }
2782
3158
  }
2783
- if (newValues.length === 0) return;
3159
+ if (newValues.length === 0) {
3160
+ yield* applyUpdates(deletePaths(current, [path], rhsNode.span), paths, index + 1, op, rhsNode, contextInput, env, tracker, evaluate$1);
3161
+ return;
3162
+ }
2784
3163
  for (const val of newValues) yield* applyUpdates(updatePath(current, path, () => val, rhsNode.span) ?? current, paths, index + 1, op, rhsNode, contextInput, env, tracker, evaluate$1);
2785
3164
  }
2786
3165