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