@gabrielbryk/jq-ts 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1347,6 +1347,51 @@ const stdBuiltins = [
1347
1347
  name: "empty",
1348
1348
  arity: 0,
1349
1349
  apply: function* () {}
1350
+ },
1351
+ {
1352
+ name: "toboolean",
1353
+ arity: 0,
1354
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1355
+ yield emit$1(isTruthy(input), span, tracker);
1356
+ }
1357
+ },
1358
+ {
1359
+ name: "walk",
1360
+ arity: 1,
1361
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1362
+ const f = args[0];
1363
+ const walkRec = function* (curr) {
1364
+ tracker.step(span);
1365
+ let newStruct = curr;
1366
+ if (Array.isArray(curr)) {
1367
+ const newArr = [];
1368
+ for (const item of curr) for (const walkedItem of walkRec(item)) newArr.push(walkedItem);
1369
+ newStruct = newArr;
1370
+ } else if (isPlainObject(curr)) {
1371
+ const newObj = {};
1372
+ const keys = Object.keys(curr).sort();
1373
+ let objValid = true;
1374
+ for (const key of keys) {
1375
+ const val = curr[key];
1376
+ let lastVal;
1377
+ let found = false;
1378
+ for (const walkedVal of walkRec(val)) {
1379
+ lastVal = walkedVal;
1380
+ found = true;
1381
+ }
1382
+ if (found) newObj[key] = lastVal;
1383
+ else {
1384
+ objValid = false;
1385
+ break;
1386
+ }
1387
+ }
1388
+ if (!objValid) return;
1389
+ newStruct = newObj;
1390
+ }
1391
+ yield* evaluate$1(f, newStruct, env, tracker);
1392
+ };
1393
+ yield* walkRec(input);
1394
+ }
1350
1395
  }
1351
1396
  ];
1352
1397
 
@@ -1360,6 +1405,232 @@ const errorBuiltins = [{
1360
1405
  }
1361
1406
  }];
1362
1407
 
1408
+ //#endregion
1409
+ //#region src/builtins/strings.ts
1410
+ const checkContains = (a, b) => {
1411
+ if (a === b) return true;
1412
+ if (typeof a !== typeof b) return false;
1413
+ if (typeof a === "string" && typeof b === "string") return a.includes(b);
1414
+ if (Array.isArray(a) && Array.isArray(b)) return b.every((bItem) => a.some((aItem) => checkContains(aItem, bItem)));
1415
+ if (isPlainObject(a) && isPlainObject(b)) {
1416
+ const keys = Object.keys(b);
1417
+ for (const key of keys) {
1418
+ if (!Object.prototype.hasOwnProperty.call(a, key)) return false;
1419
+ const valA = a[key];
1420
+ const valB = b[key];
1421
+ if (!checkContains(valA, valB)) return false;
1422
+ }
1423
+ return true;
1424
+ }
1425
+ return valueEquals(a, b);
1426
+ };
1427
+ const stringBuiltins = [
1428
+ {
1429
+ name: "split",
1430
+ arity: 1,
1431
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1432
+ if (typeof input !== "string") throw new RuntimeError("split input must be a string", span);
1433
+ const sepGen = evaluate$1(args[0], input, env, tracker);
1434
+ for (const sep of sepGen) {
1435
+ if (typeof sep !== "string") throw new RuntimeError("split separator must be a string", span);
1436
+ yield emit$1(input.split(sep), span, tracker);
1437
+ }
1438
+ }
1439
+ },
1440
+ {
1441
+ name: "join",
1442
+ arity: 1,
1443
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1444
+ if (!Array.isArray(input)) throw new RuntimeError("join input must be an array", span);
1445
+ const sepGen = evaluate$1(args[0], input, env, tracker);
1446
+ for (const sep of sepGen) {
1447
+ if (typeof sep !== "string") throw new RuntimeError("join separator must be a string", span);
1448
+ const parts = [];
1449
+ for (const item of input) {
1450
+ if (typeof item !== "string") throw new RuntimeError(`join expects strings, but got ${describeType(item)}`, span);
1451
+ parts.push(item);
1452
+ }
1453
+ yield emit$1(parts.join(sep), span, tracker);
1454
+ }
1455
+ }
1456
+ },
1457
+ {
1458
+ name: "startswith",
1459
+ arity: 1,
1460
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1461
+ if (typeof input !== "string") throw new RuntimeError("startswith input must be a string", span);
1462
+ const prefixGen = evaluate$1(args[0], input, env, tracker);
1463
+ for (const prefix of prefixGen) {
1464
+ if (typeof prefix !== "string") throw new RuntimeError("startswith prefix must be a string", span);
1465
+ yield emit$1(input.startsWith(prefix), span, tracker);
1466
+ }
1467
+ }
1468
+ },
1469
+ {
1470
+ name: "endswith",
1471
+ arity: 1,
1472
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1473
+ if (typeof input !== "string") throw new RuntimeError("endswith input must be a string", span);
1474
+ const suffixGen = evaluate$1(args[0], input, env, tracker);
1475
+ for (const suffix of suffixGen) {
1476
+ if (typeof suffix !== "string") throw new RuntimeError("endswith suffix must be a string", span);
1477
+ yield emit$1(input.endsWith(suffix), span, tracker);
1478
+ }
1479
+ }
1480
+ },
1481
+ {
1482
+ name: "contains",
1483
+ arity: 1,
1484
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1485
+ const bGen = evaluate$1(args[0], input, env, tracker);
1486
+ for (const b of bGen) yield emit$1(checkContains(input, b), span, tracker);
1487
+ }
1488
+ },
1489
+ {
1490
+ name: "index",
1491
+ arity: 1,
1492
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1493
+ const searchGen = evaluate$1(args[0], input, env, tracker);
1494
+ for (const search of searchGen) if (Array.isArray(input)) {
1495
+ let foundIndex = null;
1496
+ for (let i = 0; i < input.length; i++) {
1497
+ const val = input[i];
1498
+ if (val !== void 0 && valueEquals(val, search)) {
1499
+ foundIndex = i;
1500
+ break;
1501
+ }
1502
+ }
1503
+ yield emit$1(foundIndex, span, tracker);
1504
+ } else if (typeof input === "string") {
1505
+ if (typeof search !== "string") throw new RuntimeError("index expects string search when input is string", span);
1506
+ const idx = input.indexOf(search);
1507
+ yield emit$1(idx === -1 ? null : idx, span, tracker);
1508
+ } else if (input === null) yield emit$1(null, span, tracker);
1509
+ else throw new RuntimeError("index expects string or array", span);
1510
+ }
1511
+ },
1512
+ {
1513
+ name: "rindex",
1514
+ arity: 1,
1515
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1516
+ const searchGen = evaluate$1(args[0], input, env, tracker);
1517
+ for (const search of searchGen) if (Array.isArray(input)) {
1518
+ let foundIndex = null;
1519
+ for (let i = input.length - 1; i >= 0; i--) {
1520
+ const val = input[i];
1521
+ if (val !== void 0 && valueEquals(val, search)) {
1522
+ foundIndex = i;
1523
+ break;
1524
+ }
1525
+ }
1526
+ yield emit$1(foundIndex, span, tracker);
1527
+ } else if (typeof input === "string") {
1528
+ if (typeof search !== "string") throw new RuntimeError("rindex expects string search when input is string", span);
1529
+ const idx = input.lastIndexOf(search);
1530
+ yield emit$1(idx === -1 ? null : idx, span, tracker);
1531
+ } else if (input === null) yield emit$1(null, span, tracker);
1532
+ else throw new RuntimeError("rindex expects string or array", span);
1533
+ }
1534
+ },
1535
+ {
1536
+ name: "indices",
1537
+ arity: 1,
1538
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1539
+ const searchGen = evaluate$1(args[0], input, env, tracker);
1540
+ for (const search of searchGen) {
1541
+ const indices = [];
1542
+ if (Array.isArray(input)) for (let i = 0; i < input.length; i++) {
1543
+ const val = input[i];
1544
+ if (val !== void 0 && valueEquals(val, search)) indices.push(i);
1545
+ }
1546
+ else if (typeof input === "string") {
1547
+ if (typeof search !== "string") throw new RuntimeError("indices expects string", span);
1548
+ if (search.length === 0) {} else {
1549
+ let pos = 0;
1550
+ while (pos < input.length) {
1551
+ const idx = input.indexOf(search, pos);
1552
+ if (idx === -1) break;
1553
+ indices.push(idx);
1554
+ pos = idx + 1;
1555
+ pos = idx + search.length;
1556
+ }
1557
+ }
1558
+ } else {
1559
+ if (input === null) {
1560
+ yield emit$1(null, span, tracker);
1561
+ continue;
1562
+ }
1563
+ throw new RuntimeError("indices expects string or array", span);
1564
+ }
1565
+ yield emit$1(indices, span, tracker);
1566
+ }
1567
+ }
1568
+ },
1569
+ {
1570
+ name: "explode",
1571
+ arity: 0,
1572
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1573
+ if (typeof input !== "string") throw new RuntimeError("explode expects string", span);
1574
+ yield emit$1(Array.from(input).map((c) => c.codePointAt(0)), span, tracker);
1575
+ }
1576
+ },
1577
+ {
1578
+ name: "implode",
1579
+ arity: 0,
1580
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1581
+ if (!Array.isArray(input)) throw new RuntimeError("implode expects array", span);
1582
+ const chars = [];
1583
+ for (const item of input) {
1584
+ if (typeof item !== "number") throw new RuntimeError("implode item must be number", span);
1585
+ chars.push(String.fromCodePoint(item));
1586
+ }
1587
+ yield emit$1(chars.join(""), span, tracker);
1588
+ }
1589
+ },
1590
+ {
1591
+ name: "ltrimstr",
1592
+ arity: 1,
1593
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1594
+ if (typeof input !== "string") throw new RuntimeError("ltrimstr expects string", span);
1595
+ const prefixGen = evaluate$1(args[0], input, env, tracker);
1596
+ for (const prefix of prefixGen) {
1597
+ if (typeof prefix !== "string") throw new RuntimeError("ltrimstr prefix must be a string", span);
1598
+ if (input.startsWith(prefix)) yield emit$1(input.slice(prefix.length), span, tracker);
1599
+ else yield emit$1(input, span, tracker);
1600
+ }
1601
+ }
1602
+ },
1603
+ {
1604
+ name: "rtrimstr",
1605
+ arity: 1,
1606
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
1607
+ if (typeof input !== "string") throw new RuntimeError("rtrimstr expects string", span);
1608
+ const suffixGen = evaluate$1(args[0], input, env, tracker);
1609
+ for (const suffix of suffixGen) {
1610
+ if (typeof suffix !== "string") throw new RuntimeError("rtrimstr suffix must be a string", span);
1611
+ if (input.endsWith(suffix)) yield emit$1(input.slice(0, input.length - suffix.length), span, tracker);
1612
+ else yield emit$1(input, span, tracker);
1613
+ }
1614
+ }
1615
+ },
1616
+ {
1617
+ name: "ascii_downcase",
1618
+ arity: 0,
1619
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1620
+ if (typeof input !== "string") throw new RuntimeError("ascii_downcase expects string", span);
1621
+ yield emit$1(input.toLowerCase(), span, tracker);
1622
+ }
1623
+ },
1624
+ {
1625
+ name: "ascii_upcase",
1626
+ arity: 0,
1627
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1628
+ if (typeof input !== "string") throw new RuntimeError("ascii_upcase expects string", span);
1629
+ yield emit$1(input.toUpperCase(), span, tracker);
1630
+ }
1631
+ }
1632
+ ];
1633
+
1363
1634
  //#endregion
1364
1635
  //#region src/builtins/collections.ts
1365
1636
  function sortStable(arr, compare$1) {
@@ -1548,195 +1819,213 @@ const collectionBuiltins = [
1548
1819
  if (item === null || typeof item !== "object" || Array.isArray(item)) throw new RuntimeError("with_entries filter must produce objects", span);
1549
1820
  const obj = item;
1550
1821
  if (!("key" in obj) || !("value" in obj)) throw new RuntimeError("with_entries items must have \"key\" and \"value\"", span);
1551
- const key = obj["key"];
1552
- if (typeof key !== "string") throw new RuntimeError("with_entries keys must be strings", span);
1553
- result[key] = obj["value"];
1554
- }
1555
- yield emit$1(result, span, tracker);
1556
- }
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 = [
1580
- {
1581
- name: "split",
1582
- arity: 1,
1583
- 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);
1589
- }
1590
- }
1591
- },
1592
- {
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);
1822
+ const key = obj["key"];
1823
+ if (typeof key !== "string") throw new RuntimeError("with_entries keys must be strings", span);
1824
+ result[key] = obj["value"];
1606
1825
  }
1826
+ yield emit$1(result, span, tracker);
1607
1827
  }
1608
1828
  },
1609
1829
  {
1610
- name: "startswith",
1830
+ name: "group_by",
1611
1831
  arity: 1,
1612
1832
  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);
1833
+ if (!Array.isArray(input)) throw new RuntimeError("group_by expects an array", span);
1834
+ const pairs = [];
1835
+ const filter = args[0];
1836
+ for (const item of input) {
1837
+ tracker.step(span);
1838
+ const keys = Array.from(evaluate$1(filter, item, env, tracker));
1839
+ if (keys.length !== 1) throw new RuntimeError("group_by key expression must return exactly one value", span);
1840
+ pairs.push({
1841
+ val: item,
1842
+ key: keys[0]
1843
+ });
1844
+ }
1845
+ const sorted = sortStable(pairs, (a, b) => compareValues(a.key, b.key));
1846
+ const groups = [];
1847
+ if (sorted.length > 0) {
1848
+ let currentGroup = [sorted[0].val];
1849
+ let currentKey = sorted[0].key;
1850
+ for (let i = 1; i < sorted.length; i++) {
1851
+ const pair = sorted[i];
1852
+ if (compareValues(pair.key, currentKey) === 0) currentGroup.push(pair.val);
1853
+ else {
1854
+ groups.push(currentGroup);
1855
+ currentGroup = [pair.val];
1856
+ currentKey = pair.key;
1857
+ }
1858
+ }
1859
+ groups.push(currentGroup);
1618
1860
  }
1861
+ yield emit$1(groups, span, tracker);
1619
1862
  }
1620
1863
  },
1621
1864
  {
1622
- name: "endswith",
1865
+ name: "reverse",
1866
+ arity: 0,
1867
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1868
+ if (!Array.isArray(input)) throw new RuntimeError("reverse expects an array", span);
1869
+ yield emit$1([...input].reverse(), span, tracker);
1870
+ }
1871
+ },
1872
+ {
1873
+ name: "flatten",
1874
+ arity: 0,
1875
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1876
+ if (!Array.isArray(input)) throw new RuntimeError("flatten expects an array", span);
1877
+ const flattenRec = (arr) => {
1878
+ let res = [];
1879
+ for (const item of arr) if (Array.isArray(item)) res = res.concat(flattenRec(item));
1880
+ else res.push(item);
1881
+ return res;
1882
+ };
1883
+ yield emit$1(flattenRec(input), span, tracker);
1884
+ }
1885
+ },
1886
+ {
1887
+ name: "flatten",
1623
1888
  arity: 1,
1624
1889
  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);
1890
+ if (!Array.isArray(input)) throw new RuntimeError("flatten expects an array", span);
1891
+ const depths = evaluate$1(args[0], input, env, tracker);
1892
+ for (const depthVal of depths) {
1893
+ if (typeof depthVal !== "number") throw new RuntimeError("flatten depth must be a number", span);
1894
+ const flattenDepth = (arr, d) => {
1895
+ if (d <= 0) return arr;
1896
+ let res = [];
1897
+ for (const item of arr) if (Array.isArray(item)) res = res.concat(flattenDepth(item, d - 1));
1898
+ else res.push(item);
1899
+ return res;
1900
+ };
1901
+ yield emit$1(flattenDepth(input, depthVal), span, tracker);
1630
1902
  }
1631
1903
  }
1632
1904
  },
1633
1905
  {
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);
1906
+ name: "keys_unsorted",
1907
+ arity: 0,
1908
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1909
+ if (Array.isArray(input)) yield emit$1(Array.from({ length: input.length }, (_, i) => i), span, tracker);
1910
+ else if (input !== null && typeof input === "object") yield emit$1(Object.keys(input), span, tracker);
1911
+ else throw new RuntimeError(`keys_unsorted expects an array or object`, span);
1639
1912
  }
1640
1913
  },
1641
1914
  {
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
- }
1915
+ name: "transpose",
1916
+ arity: 0,
1917
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1918
+ if (!Array.isArray(input)) throw new RuntimeError("transpose expects an array", span);
1919
+ const arr = input;
1920
+ if (arr.length === 0) {
1921
+ yield emit$1([], span, tracker);
1922
+ return;
1923
+ }
1924
+ let maxLen = 0;
1925
+ for (const row of arr) {
1926
+ if (!Array.isArray(row)) throw new RuntimeError("transpose input must be array of arrays", span);
1927
+ if (row.length > maxLen) maxLen = row.length;
1928
+ }
1929
+ const result = [];
1930
+ for (let j = 0; j < maxLen; j++) {
1931
+ const newRow = [];
1932
+ for (let i = 0; i < arr.length; i++) {
1933
+ const row = arr[i];
1934
+ const val = j < row.length ? row[j] : null;
1935
+ newRow.push(val);
1654
1936
  }
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);
1937
+ result.push(newRow);
1938
+ }
1939
+ yield emit$1(result, span, tracker);
1662
1940
  }
1663
1941
  },
1664
1942
  {
1665
- name: "rindex",
1943
+ name: "bsearch",
1666
1944
  arity: 1,
1667
1945
  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;
1946
+ if (!Array.isArray(input)) throw new RuntimeError("bsearch expects an array", span);
1947
+ const targetGen = evaluate$1(args[0], input, env, tracker);
1948
+ for (const target of targetGen) {
1949
+ let low = 0;
1950
+ let high = input.length - 1;
1951
+ let idx = -1;
1952
+ while (low <= high) {
1953
+ const mid = Math.floor((low + high) / 2);
1954
+ const cmp = compareValues(input[mid], target);
1955
+ if (cmp === 0) {
1956
+ idx = mid;
1675
1957
  break;
1676
- }
1958
+ } else if (cmp < 0) low = mid + 1;
1959
+ else high = mid - 1;
1677
1960
  }
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);
1961
+ if (idx !== -1) yield emit$1(idx, span, tracker);
1962
+ else yield emit$1(-low - 1, span, tracker);
1963
+ }
1685
1964
  }
1686
1965
  },
1687
1966
  {
1688
- name: "indices",
1967
+ name: "combinations",
1968
+ arity: 0,
1969
+ apply: function* (input, _args, _env, tracker, _eval, span) {
1970
+ if (!Array.isArray(input)) throw new RuntimeError("combinations expects an array", span);
1971
+ if (input.some((x) => !Array.isArray(x))) throw new RuntimeError("combinations input must be array of arrays", span);
1972
+ const arrays = input;
1973
+ if (arrays.length === 0) {
1974
+ yield emit$1([], span, tracker);
1975
+ return;
1976
+ }
1977
+ const helper = function* (idx, current) {
1978
+ if (idx === arrays.length) {
1979
+ yield [...current];
1980
+ return;
1981
+ }
1982
+ const arr = arrays[idx];
1983
+ if (arr.length === 0) return;
1984
+ for (const item of arr) {
1985
+ current.push(item);
1986
+ yield* helper(idx + 1, current);
1987
+ current.pop();
1988
+ }
1989
+ };
1990
+ for (const combo of helper(0, [])) yield emit$1(combo, span, tracker);
1991
+ }
1992
+ },
1993
+ {
1994
+ name: "combinations",
1689
1995
  arity: 1,
1690
1996
  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);
1997
+ if (!Array.isArray(input)) throw new RuntimeError("combinations expects an array", span);
1998
+ const nGen = evaluate$1(args[0], input, env, tracker);
1999
+ for (const nVal of nGen) {
2000
+ if (typeof nVal !== "number") throw new RuntimeError("combinations(n) expects n to be number", span);
2001
+ if (nVal === 0) {
2002
+ yield emit$1([], span, tracker);
2003
+ continue;
1697
2004
  }
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
- }
2005
+ const arrays = [];
2006
+ for (let i = 0; i < nVal; i++) arrays.push(input);
2007
+ const helper = function* (idx, current) {
2008
+ if (idx === arrays.length) {
2009
+ yield [...current];
2010
+ return;
1709
2011
  }
1710
- } else {
1711
- if (input === null) {
1712
- yield emit$1(null, span, tracker);
1713
- continue;
2012
+ const arr = arrays[idx];
2013
+ for (const item of arr) {
2014
+ current.push(item);
2015
+ yield* helper(idx + 1, current);
2016
+ current.pop();
1714
2017
  }
1715
- throw new RuntimeError("indices expects string or array", span);
1716
- }
1717
- yield emit$1(indices, span, tracker);
2018
+ };
2019
+ if (input.length === 0 && nVal > 0) {} else for (const combo of helper(0, [])) yield emit$1(combo, span, tracker);
1718
2020
  }
1719
2021
  }
1720
2022
  },
1721
2023
  {
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);
2024
+ name: "inside",
2025
+ arity: 1,
2026
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
2027
+ const bGen = evaluate$1(args[0], input, env, tracker);
2028
+ for (const b of bGen) yield emit$1(checkContains(b, input), span, tracker);
1740
2029
  }
1741
2030
  }
1742
2031
  ];
@@ -2110,8 +2399,216 @@ const iteratorBuiltins = [
2110
2399
  };
2111
2400
  yield* rec(input);
2112
2401
  }
2402
+ },
2403
+ {
2404
+ name: "while",
2405
+ arity: 2,
2406
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
2407
+ const condExpr = args[0];
2408
+ const updateExpr = args[1];
2409
+ const rec = function* (curr) {
2410
+ tracker.step(span);
2411
+ let condMatches = false;
2412
+ for (const c of evaluate$1(condExpr, curr, env, tracker)) if (isTruthy(c)) {
2413
+ condMatches = true;
2414
+ break;
2415
+ }
2416
+ if (condMatches) {
2417
+ yield emit$1(curr, span, tracker);
2418
+ for (const next of evaluate$1(updateExpr, curr, env, tracker)) yield* rec(next);
2419
+ }
2420
+ };
2421
+ yield* rec(input);
2422
+ }
2423
+ },
2424
+ {
2425
+ name: "until",
2426
+ arity: 2,
2427
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
2428
+ const condExpr = args[0];
2429
+ const updateExpr = args[1];
2430
+ const rec = function* (curr) {
2431
+ tracker.step(span);
2432
+ let condMatches = false;
2433
+ for (const c of evaluate$1(condExpr, curr, env, tracker)) if (isTruthy(c)) {
2434
+ condMatches = true;
2435
+ break;
2436
+ }
2437
+ if (condMatches) yield emit$1(curr, span, tracker);
2438
+ else for (const next of evaluate$1(updateExpr, curr, env, tracker)) yield* rec(next);
2439
+ };
2440
+ yield* rec(input);
2441
+ }
2442
+ },
2443
+ {
2444
+ name: "repeat",
2445
+ arity: 1,
2446
+ apply: function* (input, args, env, tracker, evaluate$1, span) {
2447
+ while (true) {
2448
+ tracker.step(span);
2449
+ yield* evaluate$1(args[0], input, env, tracker);
2450
+ }
2451
+ }
2452
+ }
2453
+ ];
2454
+
2455
+ //#endregion
2456
+ //#region src/eval/ops.ts
2457
+ /**
2458
+ * Applies a unary negation (`-`).
2459
+ *
2460
+ * @param value - The value to negate.
2461
+ * @param span - Source span for errors.
2462
+ * @returns The negated value.
2463
+ */
2464
+ const applyUnaryNeg = (value, span) => {
2465
+ if (typeof value === "number") return -value;
2466
+ throw new RuntimeError(`Invalid operand for unary -: ${describeType(value)}`, span);
2467
+ };
2468
+ /**
2469
+ * Applies a binary operator.
2470
+ *
2471
+ * Supports arithmetic (`+`, `-`, `*`, `/`, `%`) and comparators.
2472
+ *
2473
+ * @param op - The operator string.
2474
+ * @param left - The left operand.
2475
+ * @param right - The right operand.
2476
+ * @param span - Source span for errors.
2477
+ * @returns The result of the operation.
2478
+ */
2479
+ const applyBinaryOp = (op, left, right, span) => {
2480
+ switch (op) {
2481
+ case "+": return add(left, right, span);
2482
+ case "-": return sub(left, right, span);
2483
+ case "*": return mul(left, right, span);
2484
+ case "/": return div(left, right, span);
2485
+ case "%": return mod(left, right, span);
2486
+ case "Eq": return isEqual(left, right);
2487
+ case "Neq": return !isEqual(left, right);
2488
+ case "Lt": return compare(left, right) < 0;
2489
+ case "Lte": return compare(left, right) <= 0;
2490
+ case "Gt": return compare(left, right) > 0;
2491
+ case "Gte": return compare(left, right) >= 0;
2492
+ default: throw new RuntimeError(`Unknown binary operator: ${op}`, span);
2493
+ }
2494
+ };
2495
+ function isEqual(a, b) {
2496
+ if (a === b) return true;
2497
+ if (a === null || b === null) return false;
2498
+ if (typeof a !== typeof b) return false;
2499
+ if (Array.isArray(a) && Array.isArray(b)) {
2500
+ if (a.length !== b.length) return false;
2501
+ return a.every((v, i) => isEqual(v, b[i]));
2502
+ }
2503
+ if (isPlainObject$1(a) && isPlainObject$1(b)) {
2504
+ const ka = Object.keys(a).sort();
2505
+ const kb = Object.keys(b).sort();
2506
+ if (ka.length !== kb.length) return false;
2507
+ if (!ka.every((k, i) => k === kb[i])) return false;
2508
+ return ka.every((k) => isEqual(a[k], b[k]));
2509
+ }
2510
+ return false;
2511
+ }
2512
+ function compare(a, b) {
2513
+ if (a === b) return 0;
2514
+ const typeOrder = (v) => {
2515
+ if (v === null) return 0;
2516
+ if (typeof v === "boolean") return 1;
2517
+ if (typeof v === "number") return 2;
2518
+ if (typeof v === "string") return 3;
2519
+ if (Array.isArray(v)) return 4;
2520
+ if (isPlainObject$1(v)) return 5;
2521
+ return 6;
2522
+ };
2523
+ const ta = typeOrder(a);
2524
+ const tb = typeOrder(b);
2525
+ if (ta !== tb) return ta - tb;
2526
+ if (typeof a === "boolean" && typeof b === "boolean") return (a ? 1 : 0) - (b ? 1 : 0);
2527
+ if (typeof a === "number" && typeof b === "number") return a - b;
2528
+ if (typeof a === "string" && typeof b === "string") return a < b ? -1 : 1;
2529
+ if (Array.isArray(a) && Array.isArray(b)) {
2530
+ for (let i = 0; i < Math.min(a.length, b.length); i++) {
2531
+ const c = compare(a[i], b[i]);
2532
+ if (c !== 0) return c;
2533
+ }
2534
+ return a.length - b.length;
2113
2535
  }
2114
- ];
2536
+ if (isPlainObject$1(a) && isPlainObject$1(b)) {
2537
+ const keysA = Object.keys(a).sort();
2538
+ const keysB = Object.keys(b).sort();
2539
+ for (let i = 0; i < Math.min(keysA.length, keysB.length); i++) {
2540
+ const kA = keysA[i];
2541
+ const kB = keysB[i];
2542
+ if (kA !== kB) return kA < kB ? -1 : 1;
2543
+ const c = compare(a[kA], b[kB]);
2544
+ if (c !== 0) return c;
2545
+ }
2546
+ return keysA.length - keysB.length;
2547
+ }
2548
+ return 0;
2549
+ }
2550
+ function add(left, right, span) {
2551
+ if (left === null && right === null) return null;
2552
+ if (left === null && typeof right === "number") return right;
2553
+ if (typeof left === "number" && right === null) return left;
2554
+ if (typeof left === "number" && typeof right === "number") return left + right;
2555
+ if (typeof left === "string" && typeof right === "string") return left + right;
2556
+ if (Array.isArray(left) && Array.isArray(right)) return [...left, ...right];
2557
+ if (isPlainObject$1(left) && isPlainObject$1(right)) return {
2558
+ ...left,
2559
+ ...right
2560
+ };
2561
+ throw new RuntimeError(`Cannot add ${describeType(left)} and ${describeType(right)}`, span);
2562
+ }
2563
+ function sub(left, right, span) {
2564
+ if (typeof left === "number" && typeof right === "number") return left - right;
2565
+ if (Array.isArray(left) && Array.isArray(right)) {
2566
+ const toRemove = new Set(right.map(stableStringify));
2567
+ return left.filter((x) => !toRemove.has(stableStringify(x)));
2568
+ }
2569
+ throw new RuntimeError(`Cannot subtract ${describeType(right)} from ${describeType(left)}`, span);
2570
+ }
2571
+ function mul(left, right, span) {
2572
+ if (typeof left === "number" && typeof right === "number") return left * right;
2573
+ if (typeof left === "string" && typeof right === "number") return repeatString(left, right);
2574
+ if (typeof left === "number" && typeof right === "string") return repeatString(right, left);
2575
+ if (isPlainObject$1(left) && isPlainObject$1(right)) return mergeDeep(left, right);
2576
+ throw new RuntimeError(`Cannot multiply ${describeType(left)} by ${describeType(right)}`, span);
2577
+ }
2578
+ function div(left, right, span) {
2579
+ if (typeof left === "number" && typeof right === "number") {
2580
+ if (right === 0) throw new RuntimeError("Division by zero", span);
2581
+ return left / right;
2582
+ }
2583
+ if (typeof left === "string" && typeof right === "string") return left.split(right);
2584
+ throw new RuntimeError(`Cannot divide ${describeType(left)} by ${describeType(right)}`, span);
2585
+ }
2586
+ function mod(left, right, span) {
2587
+ if (typeof left === "number" && typeof right === "number") {
2588
+ if (right === 0) throw new RuntimeError("Modulo by zero", span);
2589
+ return left % right;
2590
+ }
2591
+ throw new RuntimeError(`Cannot modulo ${describeType(left)} by ${describeType(right)}`, span);
2592
+ }
2593
+ function repeatString(str, count) {
2594
+ if (count <= 0) return null;
2595
+ const n = Math.floor(count);
2596
+ if (n <= 0) return null;
2597
+ return str.repeat(n);
2598
+ }
2599
+ function mergeDeep(target, source) {
2600
+ const result = { ...target };
2601
+ for (const key of Object.keys(source)) {
2602
+ const sVal = source[key];
2603
+ const tVal = result[key];
2604
+ if (isPlainObject$1(sVal) && tVal !== void 0 && isPlainObject$1(tVal)) result[key] = mergeDeep(tVal, sVal);
2605
+ else result[key] = sVal;
2606
+ }
2607
+ return result;
2608
+ }
2609
+ function isPlainObject$1(v) {
2610
+ return typeof v === "object" && v !== null && !Array.isArray(v);
2611
+ }
2115
2612
 
2116
2613
  //#endregion
2117
2614
  //#region src/builtins/math.ts
@@ -2296,6 +2793,23 @@ const mathBuiltins = [
2296
2793
  }
2297
2794
  yield emit$1(maxItem, span, tracker);
2298
2795
  }
2796
+ },
2797
+ {
2798
+ name: "add",
2799
+ arity: 0,
2800
+ apply: function* (input, _args, _env, tracker, _eval, span) {
2801
+ if (!Array.isArray(input)) throw new RuntimeError("add expects an array", span);
2802
+ if (input.length === 0) {
2803
+ yield emit$1(null, span, tracker);
2804
+ return;
2805
+ }
2806
+ let acc = input[0];
2807
+ for (let i = 1; i < input.length; i++) {
2808
+ tracker.step(span);
2809
+ acc = add(acc, input[i], span);
2810
+ }
2811
+ yield emit$1(acc, span, tracker);
2812
+ }
2299
2813
  }
2300
2814
  ];
2301
2815
 
@@ -2504,164 +3018,6 @@ var LimitTracker = class {
2504
3018
  }
2505
3019
  };
2506
3020
 
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
3021
  //#endregion
2666
3022
  //#region src/eval/env.ts
2667
3023
  /**
@@ -2727,6 +3083,7 @@ const ensureInteger = (value, span) => {
2727
3083
  */
2728
3084
  const evalAssignment = function* (node, input, env, tracker, evaluate$1) {
2729
3085
  const paths = Array.from(evaluatePath(node.left, input, env, tracker, evaluate$1));
3086
+ paths.sort((a, b) => compareValues(a, b) * -1);
2730
3087
  if (paths.length === 0) {
2731
3088
  yield emit(input, node.span, tracker);
2732
3089
  return;
@@ -2780,7 +3137,10 @@ function* applyUpdates(current, paths, index, op, rhsNode, contextInput, env, tr
2780
3137
  newValues.push(res);
2781
3138
  }
2782
3139
  }
2783
- if (newValues.length === 0) return;
3140
+ if (newValues.length === 0) {
3141
+ yield* applyUpdates(deletePaths(current, [path], rhsNode.span), paths, index + 1, op, rhsNode, contextInput, env, tracker, evaluate$1);
3142
+ return;
3143
+ }
2784
3144
  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
3145
  }
2786
3146