@hyperjump/json-schema 0.18.2 → 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.
@@ -62,8 +62,14 @@
62
62
  (function (root, factory){
63
63
 
64
64
  var PubSub = {};
65
- root.PubSub = PubSub;
66
- factory(PubSub);
65
+
66
+ if (root.PubSub) {
67
+ PubSub = root.PubSub;
68
+ console.warn("PubSub already loaded, using existing version");
69
+ } else {
70
+ root.PubSub = PubSub;
71
+ factory(PubSub);
72
+ }
67
73
  // CommonJS and Node.js module support
68
74
  {
69
75
  if (module !== undefined && module.exports) {
@@ -646,7 +652,70 @@
646
652
  return resolvedUrl;
647
653
  };
648
654
 
649
- var common$1 = { jsonTypeOf: jsonTypeOf$2, splitUrl: splitUrl$4, safeResolveUrl: safeResolveUrl$1 };
655
+ const CHAR_BACKWARD_SLASH = 47;
656
+
657
+ const pathRelative$1 = (from, to) => {
658
+ if (from === to) {
659
+ return "";
660
+ }
661
+
662
+ let toStart = 1;
663
+ const fromLen = from.length - 1;
664
+ const toLen = to.length - toStart;
665
+
666
+ // Compare paths to find the longest common path from root
667
+ const length = fromLen < toLen ? fromLen : toLen;
668
+ let lastCommonSep = -1;
669
+ let i = 0;
670
+ for (; i < length; i++) {
671
+ const fromCode = from.charCodeAt(i + 1);
672
+ if (fromCode !== to.charCodeAt(toStart + i)) {
673
+ break;
674
+ } else if (fromCode === CHAR_BACKWARD_SLASH) {
675
+ lastCommonSep = i;
676
+ }
677
+ }
678
+
679
+ if (toLen > length) {
680
+ if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
681
+ return to.slice(toStart + i + 1);
682
+ }
683
+ if (i === 0) {
684
+ return to.slice(toStart + i);
685
+ }
686
+ }
687
+ if (fromLen > length) {
688
+ if (from.charCodeAt(i + 1) === CHAR_BACKWARD_SLASH) {
689
+ lastCommonSep = i;
690
+ } else if (length === 0) {
691
+ lastCommonSep = 0;
692
+ }
693
+ }
694
+
695
+ let out = "";
696
+ // Generate the relative path based on the path difference between `to` and `from`
697
+ for (i = lastCommonSep + 2; i <= from.length; ++i) {
698
+ if (i === from.length || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) {
699
+ out += out.length === 0 ? ".." : "/..";
700
+ }
701
+ }
702
+
703
+ toStart += lastCommonSep;
704
+
705
+ // Lastly, append the rest of the destination (`to`) path that comes after
706
+ // the common path parts
707
+ if (out.length > 0) {
708
+ return `${out}${to.slice(toStart, to.length)}`;
709
+ }
710
+
711
+ if (to.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) {
712
+ ++toStart;
713
+ }
714
+
715
+ return to.slice(toStart, to.length);
716
+ };
717
+
718
+ var common$1 = { jsonTypeOf: jsonTypeOf$2, splitUrl: splitUrl$4, safeResolveUrl: safeResolveUrl$1, pathRelative: pathRelative$1 };
650
719
 
651
720
  const nil$2 = "";
652
721
 
@@ -799,6 +868,10 @@
799
868
  const value$2 = (ref) => ref[$__value];
800
869
 
801
870
  var reference = { cons: cons$1, isReference, href, value: value$2 };
871
+ reference.cons;
872
+ reference.isReference;
873
+ reference.href;
874
+ reference.value;
802
875
 
803
876
  const { jsonTypeOf: jsonTypeOf$1 } = common$1;
804
877
 
@@ -1148,7 +1221,7 @@
1148
1221
 
1149
1222
  var fetch_browser = fetch;
1150
1223
 
1151
- const { jsonTypeOf, splitUrl: splitUrl$3, safeResolveUrl } = common$1;
1224
+ const { jsonTypeOf, splitUrl: splitUrl$3, safeResolveUrl, pathRelative } = common$1;
1152
1225
 
1153
1226
 
1154
1227
 
@@ -1378,10 +1451,73 @@
1378
1451
 
1379
1452
  const length = (doc) => value(doc).length;
1380
1453
 
1454
+ const toSchemaDefaultOptions = {
1455
+ parentId: "",
1456
+ parentDialect: "",
1457
+ includeEmbedded: true
1458
+ };
1459
+ const toSchema = (schemaDoc, options = {}) => {
1460
+ const fullOptions = { ...toSchemaDefaultOptions, ...options };
1461
+
1462
+ const schema = JSON.parse(JSON.stringify(schemaDoc.schema, (key, value) => {
1463
+ if (!reference.isReference(value)) {
1464
+ return value;
1465
+ }
1466
+
1467
+ const refValue = reference.value(value);
1468
+ const embeddedDialect = refValue.$schema || schemaDoc.schemaVersion;
1469
+ const embeddedToken = getConfig(embeddedDialect, "embeddedToken");
1470
+ if (!fullOptions.includeEmbedded && embeddedToken in refValue) {
1471
+ return;
1472
+ } else {
1473
+ return reference.value(value);
1474
+ }
1475
+ }));
1476
+
1477
+ const dynamicAnchorToken = getConfig(schemaDoc.schemaVersion, "dynamicAnchorToken");
1478
+ Object.entries(schemaDoc.dynamicAnchors)
1479
+ .forEach(([anchor, uri]) => {
1480
+ const pointer = splitUrl$3(uri)[1];
1481
+ lib$3.assign(pointer, schema, {
1482
+ [dynamicAnchorToken]: anchor,
1483
+ ...lib$3.get(pointer, schema)
1484
+ });
1485
+ });
1486
+
1487
+ const anchorToken = getConfig(schemaDoc.schemaVersion, "anchorToken");
1488
+ Object.entries(schemaDoc.anchors)
1489
+ .filter(([anchor]) => anchor !== "")
1490
+ .forEach(([anchor, pointer]) => {
1491
+ lib$3.assign(pointer, schema, {
1492
+ [anchorToken]: anchor,
1493
+ ...lib$3.get(pointer, schema)
1494
+ });
1495
+ });
1496
+
1497
+ const baseToken = getConfig(schemaDoc.schemaVersion, "baseToken");
1498
+ const id = relativeUri(fullOptions.parentId, schemaDoc.id);
1499
+ const dialect = fullOptions.parentDialect === schemaDoc.schemaVersion ? "" : schemaDoc.schemaVersion;
1500
+ return {
1501
+ ...(id && { [baseToken]: id }),
1502
+ ...(dialect && { $schema: dialect }),
1503
+ ...schema
1504
+ };
1505
+ };
1506
+
1507
+ const relativeUri = (from, to) => {
1508
+ if (to.startsWith("file://")) {
1509
+ const pathToSchema = from.slice(7, from.lastIndexOf("/"));
1510
+ return from === "" ? "" : pathRelative(pathToSchema, to.slice(7));
1511
+ } else {
1512
+ return to;
1513
+ }
1514
+ };
1515
+
1381
1516
  var schema$5 = {
1382
1517
  setConfig, getConfig,
1383
1518
  add: add$1, get, markValidated,
1384
- uri, value, getAnchorPointer, typeOf, has, step, keys, entries, map, length
1519
+ uri, value, getAnchorPointer, typeOf, has, step, keys, entries, map, length,
1520
+ toSchema
1385
1521
  };
1386
1522
  schema$5.setConfig;
1387
1523
  schema$5.getConfig;
@@ -1398,6 +1534,7 @@
1398
1534
  schema$5.entries;
1399
1535
  schema$5.map;
1400
1536
  schema$5.length;
1537
+ schema$5.toSchema;
1401
1538
 
1402
1539
  class InvalidSchemaError$1 extends Error {
1403
1540
  constructor(output) {
@@ -1726,7 +1863,7 @@
1726
1863
  keywords$1.metaData;
1727
1864
  keywords$1.validate;
1728
1865
 
1729
- var lib$1 = { Core: core$2, Schema: schema$5, Instance: instance, Keywords: keywords$1, InvalidSchemaError: invalidSchemaError };
1866
+ var lib$1 = { Core: core$2, Schema: schema$5, Instance: instance, Reference: reference, Keywords: keywords$1, InvalidSchemaError: invalidSchemaError };
1730
1867
 
1731
1868
  const { Core: Core$v, Schema: Schema$N, Instance: Instance$B } = lib$1;
1732
1869