|
7 | 7 | */ |
8 | 8 | 'use strict'; |
9 | 9 |
|
10 | | -var $Ref = require('./ref'), |
11 | | - util = require('./util'), |
12 | | - url = require('url'); |
| 10 | +var $Ref = require('./ref'), |
| 11 | + Pointer = require('./pointer'), |
| 12 | + util = require('./util'), |
| 13 | + url = require('url'); |
13 | 14 |
|
14 | 15 | module.exports = bundle; |
15 | 16 |
|
@@ -63,7 +64,7 @@ function remap($refs, options) { |
63 | 64 | function crawl(obj, path, $refs, remapped, options) { |
64 | 65 | if (obj && typeof(obj) === 'object') { |
65 | 66 | Object.keys(obj).forEach(function(key) { |
66 | | - var keyPath = path + '/' + key; |
| 67 | + var keyPath = Pointer.join(path, key); |
67 | 68 | var value = obj[key]; |
68 | 69 |
|
69 | 70 | if ($Ref.is$Ref(value)) { |
@@ -105,12 +106,13 @@ function dereference(basePath, $refs, options) { |
105 | 106 | }); |
106 | 107 | } |
107 | 108 |
|
108 | | -},{"./ref":9,"./util":12,"url":90}],2:[function(require,module,exports){ |
| 109 | +},{"./pointer":6,"./ref":9,"./util":12,"url":90}],2:[function(require,module,exports){ |
109 | 110 | 'use strict'; |
110 | 111 |
|
111 | | -var $Ref = require('./ref'), |
112 | | - util = require('./util'), |
113 | | - url = require('url'); |
| 112 | +var $Ref = require('./ref'), |
| 113 | + Pointer = require('./pointer'), |
| 114 | + util = require('./util'), |
| 115 | + url = require('url'); |
114 | 116 |
|
115 | 117 | module.exports = dereference; |
116 | 118 |
|
@@ -138,10 +140,9 @@ function dereference(parser, options) { |
138 | 140 | function crawl(obj, path, parents, $refs, options) { |
139 | 141 | if (obj && typeof(obj) === 'object') { |
140 | 142 | parents.push(obj); |
141 | | - path = util.path.ensureHash(path); |
142 | 143 |
|
143 | 144 | Object.keys(obj).forEach(function(key) { |
144 | | - var keyPath = path + '/' + key; |
| 145 | + var keyPath = Pointer.join(path, key); |
145 | 146 | var value = obj[key]; |
146 | 147 |
|
147 | 148 | if ($Ref.isAllowed$Ref(value, options)) { |
@@ -194,7 +195,7 @@ function dereference$Ref(obj, key, value) { |
194 | 195 | } |
195 | 196 | } |
196 | 197 |
|
197 | | -},{"./ref":9,"./util":12,"url":90}],3:[function(require,module,exports){ |
| 198 | +},{"./pointer":6,"./ref":9,"./util":12,"url":90}],3:[function(require,module,exports){ |
198 | 199 | (function (Buffer){ |
199 | 200 | 'use strict'; |
200 | 201 |
|
@@ -665,6 +666,8 @@ var $Ref = require('./ref'), |
665 | 666 | util = require('./util'), |
666 | 667 | url = require('url'), |
667 | 668 | ono = require('ono'), |
| 669 | + slashes = /\//g, |
| 670 | + tildes = /~/g, |
668 | 671 | escapedSlash = /~1/g, |
669 | 672 | escapedTilde = /~0/g; |
670 | 673 |
|
@@ -717,7 +720,7 @@ Pointer.prototype.resolve = function(obj, options) { |
717 | 720 | for (var i = 0; i < tokens.length; i++) { |
718 | 721 | if (resolveIf$Ref(this, options)) { |
719 | 722 | // The $ref path has changed, so append the remaining tokens to the path |
720 | | - this.path = util.path.ensureHash(this.path) + '/' + tokens.slice(i).join('/'); |
| 723 | + this.path = Pointer.join(this.path, tokens.slice(i)); |
721 | 724 | } |
722 | 725 |
|
723 | 726 | var token = tokens[i]; |
@@ -815,6 +818,30 @@ Pointer.parse = function(path) { |
815 | 818 | return pointer.slice(1); |
816 | 819 | }; |
817 | 820 |
|
| 821 | +/** |
| 822 | + * Creates a JSON pointer path, by joining one or more tokens to a base path. |
| 823 | + * |
| 824 | + * @param {string} base - The base path (e.g. "schema.json#/definitions/person") |
| 825 | + * @param {string|string[]} tokens - The token(s) to append (e.g. ["name", "first"]) |
| 826 | + * @returns {string} |
| 827 | + */ |
| 828 | +Pointer.join = function(base, tokens) { |
| 829 | + // Ensure that the base path contains a hash |
| 830 | + if (base.indexOf('#') === -1) { |
| 831 | + base += '#'; |
| 832 | + } |
| 833 | + |
| 834 | + // Append each token to the base path |
| 835 | + tokens = Array.isArray(tokens) ? tokens : [tokens]; |
| 836 | + for (var i = 0; i < tokens.length; i++) { |
| 837 | + var token = tokens[i]; |
| 838 | + // Encode the token, according to RFC 6901 |
| 839 | + base += '/' + token.replace(tildes, '~0').replace(slashes, '~1'); |
| 840 | + } |
| 841 | + |
| 842 | + return base; |
| 843 | +}; |
| 844 | + |
818 | 845 | /** |
819 | 846 | * If the given pointer's {@link Pointer#value} is a JSON reference, |
820 | 847 | * then the reference is resolved and {@link Pointer#value} is replaced with the resolved value. |
@@ -1502,6 +1529,7 @@ function getPaths($refs, types) { |
1502 | 1529 |
|
1503 | 1530 | var Promise = require('./promise'), |
1504 | 1531 | $Ref = require('./ref'), |
| 1532 | + Pointer = require('./pointer'), |
1505 | 1533 | read = require('./read'), |
1506 | 1534 | util = require('./util'), |
1507 | 1535 | url = require('url'), |
@@ -1568,8 +1596,8 @@ function crawl(obj, path, pathFromRoot, $refs, options) { |
1568 | 1596 | } |
1569 | 1597 |
|
1570 | 1598 | keys.forEach(function(key) { |
1571 | | - var keyPath = path + '/' + key; |
1572 | | - var keyPathFromRoot = pathFromRoot + '/' + key; |
| 1599 | + var keyPath = Pointer.join(path, key); |
| 1600 | + var keyPathFromRoot = Pointer.join(pathFromRoot, key); |
1573 | 1601 | var value = obj[key]; |
1574 | 1602 |
|
1575 | 1603 | if ($Ref.isExternal$Ref(value)) { |
@@ -1623,7 +1651,7 @@ function crawl$Ref(path, pathFromRoot, $refs, options) { |
1623 | 1651 | }); |
1624 | 1652 | } |
1625 | 1653 |
|
1626 | | -},{"./promise":7,"./read":8,"./ref":9,"./util":12,"ono":50,"url":90}],12:[function(require,module,exports){ |
| 1654 | +},{"./pointer":6,"./promise":7,"./read":8,"./ref":9,"./util":12,"ono":50,"url":90}],12:[function(require,module,exports){ |
1627 | 1655 | (function (process){ |
1628 | 1656 | 'use strict'; |
1629 | 1657 |
|
@@ -1743,16 +1771,6 @@ exports.urlToLocalPath = function urlToLocalPath(url) { |
1743 | 1771 | return url; |
1744 | 1772 | }; |
1745 | 1773 |
|
1746 | | -/** |
1747 | | - * Adds a hash to the given path, if it doesn't already have one. |
1748 | | - * |
1749 | | - * @param {string} path |
1750 | | - * @returns {string} |
1751 | | - */ |
1752 | | -exports.ensureHash = function ensureHash(path) { |
1753 | | - return path.indexOf('#') === -1 ? path + '#' : path; |
1754 | | -}; |
1755 | | - |
1756 | 1774 | /** |
1757 | 1775 | * Returns the hash (URL fragment), if any, of the given path. |
1758 | 1776 | * |
|
0 commit comments