@hyperjump/json-schema 0.18.4 → 0.18.5

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.
@@ -650,7 +650,70 @@ const safeResolveUrl$1 = (contextUrl, url) => {
650
650
  return resolvedUrl;
651
651
  };
652
652
 
653
- var common$1 = { jsonTypeOf: jsonTypeOf$2, splitUrl: splitUrl$4, safeResolveUrl: safeResolveUrl$1 };
653
+ const CHAR_BACKWARD_SLASH = 47;
654
+
655
+ const pathRelative$1 = (from, to) => {
656
+ if (from === to) {
657
+ return "";
658
+ }
659
+
660
+ let toStart = 1;
661
+ const fromLen = from.length - 1;
662
+ const toLen = to.length - toStart;
663
+
664
+ // Compare paths to find the longest common path from root
665
+ const length = fromLen < toLen ? fromLen : toLen;
666
+ let lastCommonSep = -1;
667
+ let i = 0;
668
+ for (; i < length; i++) {
669
+ const fromCode = from.charCodeAt(i + 1);
670
+ if (fromCode !== to.charCodeAt(toStart + i)) {
671
+ break;
672
+ } else if (fromCode === CHAR_BACKWARD_SLASH) {
673
+ lastCommonSep = i;
674
+ }
675
+ }
676
+
677
+ if (toLen > length) {
678
+ if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
679
+ return to.slice(toStart + i + 1);
680
+ }
681
+ if (i === 0) {
682
+ return to.slice(toStart + i);
683
+ }
684
+ }
685
+ if (fromLen > length) {
686
+ if (from.charCodeAt(i + 1) === CHAR_BACKWARD_SLASH) {
687
+ lastCommonSep = i;
688
+ } else if (length === 0) {
689
+ lastCommonSep = 0;
690
+ }
691
+ }
692
+
693
+ let out = "";
694
+ // Generate the relative path based on the path difference between `to` and `from`
695
+ for (i = lastCommonSep + 2; i <= from.length; ++i) {
696
+ if (i === from.length || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) {
697
+ out += out.length === 0 ? ".." : "/..";
698
+ }
699
+ }
700
+
701
+ toStart += lastCommonSep;
702
+
703
+ // Lastly, append the rest of the destination (`to`) path that comes after
704
+ // the common path parts
705
+ if (out.length > 0) {
706
+ return `${out}${to.slice(toStart, to.length)}`;
707
+ }
708
+
709
+ if (to.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) {
710
+ ++toStart;
711
+ }
712
+
713
+ return to.slice(toStart, to.length);
714
+ };
715
+
716
+ var common$1 = { jsonTypeOf: jsonTypeOf$2, splitUrl: splitUrl$4, safeResolveUrl: safeResolveUrl$1, pathRelative: pathRelative$1 };
654
717
 
655
718
  const nil$2 = "";
656
719
 
@@ -1156,7 +1219,7 @@ lib$2.allValues;
1156
1219
 
1157
1220
  var fetch_browser = fetch;
1158
1221
 
1159
- const { jsonTypeOf, splitUrl: splitUrl$3, safeResolveUrl } = common$1;
1222
+ const { jsonTypeOf, splitUrl: splitUrl$3, safeResolveUrl, pathRelative } = common$1;
1160
1223
 
1161
1224
 
1162
1225
 
@@ -1386,10 +1449,73 @@ const map = justCurryIt((fn, doc) => lib$2.pipeline([
1386
1449
 
1387
1450
  const length = (doc) => value(doc).length;
1388
1451
 
1452
+ const toSchemaDefaultOptions = {
1453
+ parentId: "",
1454
+ parentDialect: "",
1455
+ includeEmbedded: true
1456
+ };
1457
+ const toSchema = (schemaDoc, options = {}) => {
1458
+ const fullOptions = { ...toSchemaDefaultOptions, ...options };
1459
+
1460
+ const schema = JSON.parse(JSON.stringify(schemaDoc.schema, (key, value) => {
1461
+ if (!reference.isReference(value)) {
1462
+ return value;
1463
+ }
1464
+
1465
+ const refValue = reference.value(value);
1466
+ const embeddedDialect = refValue.$schema || schemaDoc.schemaVersion;
1467
+ const embeddedToken = getConfig(embeddedDialect, "embeddedToken");
1468
+ if (!fullOptions.includeEmbedded && embeddedToken in refValue) {
1469
+ return;
1470
+ } else {
1471
+ return reference.value(value);
1472
+ }
1473
+ }));
1474
+
1475
+ const dynamicAnchorToken = getConfig(schemaDoc.schemaVersion, "dynamicAnchorToken");
1476
+ Object.entries(schemaDoc.dynamicAnchors)
1477
+ .forEach(([anchor, uri]) => {
1478
+ const pointer = splitUrl$3(uri)[1];
1479
+ lib$3.assign(pointer, schema, {
1480
+ [dynamicAnchorToken]: anchor,
1481
+ ...lib$3.get(pointer, schema)
1482
+ });
1483
+ });
1484
+
1485
+ const anchorToken = getConfig(schemaDoc.schemaVersion, "anchorToken");
1486
+ Object.entries(schemaDoc.anchors)
1487
+ .filter(([anchor]) => anchor !== "")
1488
+ .forEach(([anchor, pointer]) => {
1489
+ lib$3.assign(pointer, schema, {
1490
+ [anchorToken]: anchor,
1491
+ ...lib$3.get(pointer, schema)
1492
+ });
1493
+ });
1494
+
1495
+ const baseToken = getConfig(schemaDoc.schemaVersion, "baseToken");
1496
+ const id = relativeUri(fullOptions.parentId, schemaDoc.id);
1497
+ const dialect = fullOptions.parentDialect === schemaDoc.schemaVersion ? "" : schemaDoc.schemaVersion;
1498
+ return {
1499
+ ...(id && { [baseToken]: id }),
1500
+ ...(dialect && { $schema: dialect }),
1501
+ ...schema
1502
+ };
1503
+ };
1504
+
1505
+ const relativeUri = (from, to) => {
1506
+ if (to.startsWith("file://")) {
1507
+ const pathToSchema = from.slice(7, from.lastIndexOf("/"));
1508
+ return from === "" ? "" : pathRelative(pathToSchema, to.slice(7));
1509
+ } else {
1510
+ return to;
1511
+ }
1512
+ };
1513
+
1389
1514
  var schema$5 = {
1390
1515
  setConfig, getConfig,
1391
1516
  add: add$1, get, markValidated,
1392
- uri, value, getAnchorPointer, typeOf, has, step, keys, entries, map, length
1517
+ uri, value, getAnchorPointer, typeOf, has, step, keys, entries, map, length,
1518
+ toSchema
1393
1519
  };
1394
1520
  schema$5.setConfig;
1395
1521
  schema$5.getConfig;
@@ -1406,6 +1532,7 @@ schema$5.keys;
1406
1532
  schema$5.entries;
1407
1533
  schema$5.map;
1408
1534
  schema$5.length;
1535
+ schema$5.toSchema;
1409
1536
 
1410
1537
  class InvalidSchemaError$1 extends Error {
1411
1538
  constructor(output) {