@utilix-tech/sdk 0.79.0 → 0.80.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.
@@ -1423,15 +1423,231 @@ function applyJsonMergePatch(targetJson, patchJson) {
1423
1423
  }
1424
1424
  return { result: mergeJsonPatch(target, patch) };
1425
1425
  }
1426
+ function _escapeJsonPointerToken(token) {
1427
+ return token.replace(/~/g, "~0").replace(/\//g, "~1");
1428
+ }
1429
+ function _unescapeJsonPointerToken(token) {
1430
+ return token.replace(/~1/g, "/").replace(/~0/g, "~");
1431
+ }
1432
+ function _parseJsonPointer(pointer) {
1433
+ if (pointer === "") return [];
1434
+ if (!pointer.startsWith("/")) throw new Error(`Invalid JSON Pointer "${pointer}": must start with "/"`);
1435
+ return pointer.slice(1).split("/").map(_unescapeJsonPointerToken);
1436
+ }
1437
+ function _isPlainObject(value) {
1438
+ return typeof value === "object" && value !== null && !Array.isArray(value);
1439
+ }
1440
+ function _deepEqual(a, b) {
1441
+ if (a === b) return true;
1442
+ if (typeof a !== typeof b) return false;
1443
+ if (Array.isArray(a) || Array.isArray(b)) {
1444
+ if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false;
1445
+ return a.every((v, i) => _deepEqual(v, b[i]));
1446
+ }
1447
+ if (_isPlainObject(a) && _isPlainObject(b)) {
1448
+ const aKeys = Object.keys(a);
1449
+ const bKeys = Object.keys(b);
1450
+ if (aKeys.length !== bKeys.length) return false;
1451
+ return aKeys.every((k) => k in b && _deepEqual(a[k], b[k]));
1452
+ }
1453
+ return false;
1454
+ }
1455
+ function _getByTokens(doc, tokens) {
1456
+ let current = doc;
1457
+ for (const token of tokens) {
1458
+ if (Array.isArray(current)) {
1459
+ const index = token === "-" ? current.length - 1 : parseInt(token, 10);
1460
+ if (!Number.isInteger(index) || index < 0 || index >= current.length) return { found: false, value: void 0 };
1461
+ current = current[index];
1462
+ } else if (_isPlainObject(current)) {
1463
+ if (!(token in current)) return { found: false, value: void 0 };
1464
+ current = current[token];
1465
+ } else {
1466
+ return { found: false, value: void 0 };
1467
+ }
1468
+ }
1469
+ return { found: true, value: current };
1470
+ }
1471
+ function diffJsonPatch(a, b) {
1472
+ const ops = [];
1473
+ _diffValue(a, b, "", ops);
1474
+ return ops;
1475
+ }
1476
+ function _diffValue(a, b, path, ops) {
1477
+ if (_deepEqual(a, b)) return;
1478
+ if (_isPlainObject(a) && _isPlainObject(b)) {
1479
+ for (const key of Object.keys(a)) {
1480
+ if (!(key in b)) ops.push({ op: "remove", path: `${path}/${_escapeJsonPointerToken(key)}` });
1481
+ }
1482
+ for (const key of Object.keys(b)) {
1483
+ const childPath = `${path}/${_escapeJsonPointerToken(key)}`;
1484
+ if (!(key in a)) {
1485
+ ops.push({ op: "add", path: childPath, value: b[key] });
1486
+ } else {
1487
+ _diffValue(a[key], b[key], childPath, ops);
1488
+ }
1489
+ }
1490
+ return;
1491
+ }
1492
+ if (Array.isArray(a) && Array.isArray(b)) {
1493
+ const minLen = Math.min(a.length, b.length);
1494
+ for (let i = 0; i < minLen; i++) {
1495
+ _diffValue(a[i], b[i], `${path}/${i}`, ops);
1496
+ }
1497
+ if (b.length > a.length) {
1498
+ for (let i = a.length; i < b.length; i++) ops.push({ op: "add", path: `${path}/${i}`, value: b[i] });
1499
+ } else if (a.length > b.length) {
1500
+ for (let i = a.length - 1; i >= b.length; i--) ops.push({ op: "remove", path: `${path}/${i}` });
1501
+ }
1502
+ return;
1503
+ }
1504
+ ops.push({ op: "replace", path, value: b });
1505
+ }
1506
+ function applyJsonPatch(target, operations) {
1507
+ let doc = JSON.parse(JSON.stringify(target ?? null));
1508
+ const applyOne = (op, index) => {
1509
+ if (op.path === void 0 || op.op === void 0) return `Operation ${index}: missing "op" or "path"`;
1510
+ let tokens;
1511
+ try {
1512
+ tokens = _parseJsonPointer(op.path);
1513
+ } catch (e) {
1514
+ return `Operation ${index}: ${e.message}`;
1515
+ }
1516
+ const setAt = (tgtTokens, value) => {
1517
+ if (tgtTokens.length === 0) {
1518
+ doc = value;
1519
+ return null;
1520
+ }
1521
+ const parentTokens = tgtTokens.slice(0, -1);
1522
+ const lastToken = tgtTokens[tgtTokens.length - 1];
1523
+ const { found, value: parent } = _getByTokens(doc, parentTokens);
1524
+ if (!found) return `Operation ${index}: path "${op.path}" has no parent`;
1525
+ if (Array.isArray(parent)) {
1526
+ const idx = lastToken === "-" ? parent.length : parseInt(lastToken, 10);
1527
+ if (!Number.isInteger(idx) || idx < 0 || idx > parent.length) return `Operation ${index}: array index "${lastToken}" out of bounds`;
1528
+ parent.splice(idx, 0, value);
1529
+ } else if (_isPlainObject(parent)) {
1530
+ parent[lastToken] = value;
1531
+ } else {
1532
+ return `Operation ${index}: cannot add into a non-object, non-array value at "${op.path}"`;
1533
+ }
1534
+ return null;
1535
+ };
1536
+ const replaceAt = (tgtTokens, value) => {
1537
+ if (tgtTokens.length === 0) {
1538
+ doc = value;
1539
+ return null;
1540
+ }
1541
+ const parentTokens = tgtTokens.slice(0, -1);
1542
+ const lastToken = tgtTokens[tgtTokens.length - 1];
1543
+ const { found, value: parent } = _getByTokens(doc, parentTokens);
1544
+ if (!found) return `Operation ${index}: path "${op.path}" has no parent`;
1545
+ if (Array.isArray(parent)) {
1546
+ const idx = lastToken === "-" ? parent.length - 1 : parseInt(lastToken, 10);
1547
+ if (!Number.isInteger(idx) || idx < 0 || idx >= parent.length) return `Operation ${index}: array index "${lastToken}" out of bounds`;
1548
+ parent[idx] = value;
1549
+ } else if (_isPlainObject(parent)) {
1550
+ if (!(lastToken in parent)) return `Operation ${index}: path "${op.path}" does not exist`;
1551
+ parent[lastToken] = value;
1552
+ } else {
1553
+ return `Operation ${index}: cannot replace into a non-object, non-array value at "${op.path}"`;
1554
+ }
1555
+ return null;
1556
+ };
1557
+ const removeAt = (tgtTokens) => {
1558
+ if (tgtTokens.length === 0) return { error: `Operation ${index}: cannot remove the whole document (path "")` };
1559
+ const parentTokens = tgtTokens.slice(0, -1);
1560
+ const lastToken = tgtTokens[tgtTokens.length - 1];
1561
+ const { found, value: parent } = _getByTokens(doc, parentTokens);
1562
+ if (!found) return { error: `Operation ${index}: path "${op.path}" has no parent` };
1563
+ if (Array.isArray(parent)) {
1564
+ const idx = lastToken === "-" ? parent.length - 1 : parseInt(lastToken, 10);
1565
+ if (!Number.isInteger(idx) || idx < 0 || idx >= parent.length) return { error: `Operation ${index}: array index "${lastToken}" out of bounds` };
1566
+ const [removed] = parent.splice(idx, 1);
1567
+ return { error: null, removed };
1568
+ }
1569
+ if (_isPlainObject(parent)) {
1570
+ if (!(lastToken in parent)) return { error: `Operation ${index}: path "${op.path}" does not exist` };
1571
+ const removed = parent[lastToken];
1572
+ delete parent[lastToken];
1573
+ return { error: null, removed };
1574
+ }
1575
+ return { error: `Operation ${index}: cannot remove from a non-object, non-array value at "${op.path}"` };
1576
+ };
1577
+ switch (op.op) {
1578
+ case "add":
1579
+ return setAt(tokens, op.value);
1580
+ case "replace":
1581
+ return replaceAt(tokens, op.value);
1582
+ case "remove":
1583
+ return removeAt(tokens).error;
1584
+ case "test": {
1585
+ const { found, value } = _getByTokens(doc, tokens);
1586
+ if (!found) return `Operation ${index}: path "${op.path}" does not exist`;
1587
+ if (!_deepEqual(value, op.value)) return `Operation ${index}: test failed at "${op.path}"`;
1588
+ return null;
1589
+ }
1590
+ case "move": {
1591
+ if (!op.from) return `Operation ${index}: "move" requires "from"`;
1592
+ let fromTokens;
1593
+ try {
1594
+ fromTokens = _parseJsonPointer(op.from);
1595
+ } catch (e) {
1596
+ return `Operation ${index}: ${e.message}`;
1597
+ }
1598
+ const removal = removeAt(fromTokens);
1599
+ if (removal.error) return removal.error;
1600
+ return setAt(tokens, removal.removed);
1601
+ }
1602
+ case "copy": {
1603
+ if (!op.from) return `Operation ${index}: "copy" requires "from"`;
1604
+ let fromTokens;
1605
+ try {
1606
+ fromTokens = _parseJsonPointer(op.from);
1607
+ } catch (e) {
1608
+ return `Operation ${index}: ${e.message}`;
1609
+ }
1610
+ const { found, value } = _getByTokens(doc, fromTokens);
1611
+ if (!found) return `Operation ${index}: "from" path "${op.from}" does not exist`;
1612
+ return setAt(tokens, JSON.parse(JSON.stringify(value)));
1613
+ }
1614
+ default:
1615
+ return `Operation ${index}: unknown op "${op.op}"`;
1616
+ }
1617
+ };
1618
+ for (let i = 0; i < operations.length; i++) {
1619
+ const err = applyOne(operations[i], i);
1620
+ if (err) return { error: err };
1621
+ }
1622
+ return { result: doc };
1623
+ }
1624
+ function generateJsonPatch(aJson, bJson) {
1625
+ let a;
1626
+ try {
1627
+ a = JSON.parse(aJson);
1628
+ } catch {
1629
+ return { error: "First document is not valid JSON" };
1630
+ }
1631
+ let b;
1632
+ try {
1633
+ b = JSON.parse(bJson);
1634
+ } catch {
1635
+ return { error: "Second document is not valid JSON" };
1636
+ }
1637
+ return { patch: diffJsonPatch(a, b) };
1638
+ }
1426
1639
 
1427
1640
  exports.applyJsonMergePatch = applyJsonMergePatch;
1641
+ exports.applyJsonPatch = applyJsonPatch;
1428
1642
  exports.csvToJson = csvToJson;
1429
1643
  exports.detectFormat = detectFormat;
1430
1644
  exports.diffJson = diffJson;
1645
+ exports.diffJsonPatch = diffJsonPatch;
1431
1646
  exports.evaluateJsonPath = evaluateJsonPath;
1432
1647
  exports.formatDiffLine = formatDiffLine;
1433
1648
  exports.formatJson = formatJson;
1434
1649
  exports.generateExample = generateExample;
1650
+ exports.generateJsonPatch = generateJsonPatch;
1435
1651
  exports.getJsonStats = getJsonStats;
1436
1652
  exports.inferGoType = inferGoType;
1437
1653
  exports.inferPythonType = inferPythonType;
@@ -1 +1 @@
1
- export { C as ConvertOptions, a as CsvOptions, D as DiffNode, b as DiffType, G as GoField, c as GoStruct, J as JsonDiffResult, d as JsonParseResult, e as JsonPathResult, f as JsonPointerResult, g as JsonSchemaLike, h as JsonSchemaToTsOptions, M as MergePatchResult, P as ParseStatus, i as PythonClass, k as PythonField, l as PythonMode, V as ValidationError, m as ValidationResult, Z as ZodOptions, n as applyJsonMergePatch, o as csvToJson, p as detectFormat, q as diffJson, r as evaluateJsonPath, s as formatDiffLine, t as formatJson, u as generateExample, v as getJsonStats, w as inferGoType, x as inferPythonType, y as inferType, z as jsonSchemaToTypescript, A as jsonToCsv, B as jsonToGo, E as jsonToPython, F as jsonToTypescript, H as jsonToYaml, I as jsonToZod, K as mergeJsonPatch, L as minifyJson, N as parseJson, O as parseStructs, Q as resolveJsonPointer, R as toPascalCase, S as toSnakeCase, T as validateJson, U as validateJsonPath, W as validateJsonString, X as yamlToJson } from '../json-CaiYsGDQ.cjs';
1
+ export { C as ConvertOptions, a as CsvOptions, D as DiffNode, b as DiffType, G as GoField, c as GoStruct, J as JsonDiffResult, d as JsonParseResult, e as JsonPatchOp, f as JsonPatchOperation, g as JsonPathResult, h as JsonPointerResult, i as JsonSchemaLike, k as JsonSchemaToTsOptions, M as MergePatchResult, P as ParseStatus, l as PythonClass, m as PythonField, n as PythonMode, V as ValidationError, o as ValidationResult, Z as ZodOptions, p as applyJsonMergePatch, q as applyJsonPatch, r as csvToJson, s as detectFormat, t as diffJson, u as diffJsonPatch, v as evaluateJsonPath, w as formatDiffLine, x as formatJson, y as generateExample, z as generateJsonPatch, A as getJsonStats, B as inferGoType, E as inferPythonType, F as inferType, H as jsonSchemaToTypescript, I as jsonToCsv, K as jsonToGo, L as jsonToPython, N as jsonToTypescript, O as jsonToYaml, Q as jsonToZod, R as mergeJsonPatch, S as minifyJson, T as parseJson, U as parseStructs, W as resolveJsonPointer, X as toPascalCase, Y as toSnakeCase, _ as validateJson, $ as validateJsonPath, a0 as validateJsonString, a1 as yamlToJson } from '../json-Ca6gZCNB.cjs';
@@ -1 +1 @@
1
- export { C as ConvertOptions, a as CsvOptions, D as DiffNode, b as DiffType, G as GoField, c as GoStruct, J as JsonDiffResult, d as JsonParseResult, e as JsonPathResult, f as JsonPointerResult, g as JsonSchemaLike, h as JsonSchemaToTsOptions, M as MergePatchResult, P as ParseStatus, i as PythonClass, k as PythonField, l as PythonMode, V as ValidationError, m as ValidationResult, Z as ZodOptions, n as applyJsonMergePatch, o as csvToJson, p as detectFormat, q as diffJson, r as evaluateJsonPath, s as formatDiffLine, t as formatJson, u as generateExample, v as getJsonStats, w as inferGoType, x as inferPythonType, y as inferType, z as jsonSchemaToTypescript, A as jsonToCsv, B as jsonToGo, E as jsonToPython, F as jsonToTypescript, H as jsonToYaml, I as jsonToZod, K as mergeJsonPatch, L as minifyJson, N as parseJson, O as parseStructs, Q as resolveJsonPointer, R as toPascalCase, S as toSnakeCase, T as validateJson, U as validateJsonPath, W as validateJsonString, X as yamlToJson } from '../json-CaiYsGDQ.js';
1
+ export { C as ConvertOptions, a as CsvOptions, D as DiffNode, b as DiffType, G as GoField, c as GoStruct, J as JsonDiffResult, d as JsonParseResult, e as JsonPatchOp, f as JsonPatchOperation, g as JsonPathResult, h as JsonPointerResult, i as JsonSchemaLike, k as JsonSchemaToTsOptions, M as MergePatchResult, P as ParseStatus, l as PythonClass, m as PythonField, n as PythonMode, V as ValidationError, o as ValidationResult, Z as ZodOptions, p as applyJsonMergePatch, q as applyJsonPatch, r as csvToJson, s as detectFormat, t as diffJson, u as diffJsonPatch, v as evaluateJsonPath, w as formatDiffLine, x as formatJson, y as generateExample, z as generateJsonPatch, A as getJsonStats, B as inferGoType, E as inferPythonType, F as inferType, H as jsonSchemaToTypescript, I as jsonToCsv, K as jsonToGo, L as jsonToPython, N as jsonToTypescript, O as jsonToYaml, Q as jsonToZod, R as mergeJsonPatch, S as minifyJson, T as parseJson, U as parseStructs, W as resolveJsonPointer, X as toPascalCase, Y as toSnakeCase, _ as validateJson, $ as validateJsonPath, a0 as validateJsonString, a1 as yamlToJson } from '../json-Ca6gZCNB.js';
@@ -1,2 +1,2 @@
1
- export { applyJsonMergePatch, csvToJson, detectFormat, diffJson, evaluateJsonPath, formatDiffLine, formatJson, generateExample, getJsonStats, inferGoType, inferPythonType, inferType, jsonSchemaToTypescript, jsonToCsv, jsonToGo, jsonToPython, jsonToTypescript, jsonToYaml, jsonToZod, mergeJsonPatch, minifyJson, parseJson, parseStructs, resolveJsonPointer, toPascalCase, toSnakeCase, validateJson, validateJsonPath, validateJsonString, yamlToJson } from '../chunk-34C6GXCC.js';
1
+ export { applyJsonMergePatch, applyJsonPatch, csvToJson, detectFormat, diffJson, diffJsonPatch, evaluateJsonPath, formatDiffLine, formatJson, generateExample, generateJsonPatch, getJsonStats, inferGoType, inferPythonType, inferType, jsonSchemaToTypescript, jsonToCsv, jsonToGo, jsonToPython, jsonToTypescript, jsonToYaml, jsonToZod, mergeJsonPatch, minifyJson, parseJson, parseStructs, resolveJsonPointer, toPascalCase, toSnakeCase, validateJson, validateJsonPath, validateJsonString, yamlToJson } from '../chunk-WUOZHCIC.js';
2
2
  import '../chunk-MLKGABMK.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@utilix-tech/sdk",
3
- "version": "0.79.0",
3
+ "version": "0.80.0",
4
4
  "description": "154+ developer utility tools for Node.js: JSON, encoding, hashing, color, CSS, network and more. Runs locally, no API key required.",
5
5
  "author": "Utilix <hello@utilix.tech>",
6
6
  "license": "MIT",