Skip to content

Commit a0c366a

Browse files
Added RFC 6901 encoding of JSON Pointers
1 parent acc7b6c commit a0c366a

9 files changed

Lines changed: 103 additions & 67 deletions

File tree

dist/ref-parser.js

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
*/
88
'use strict';
99

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');
1314

1415
module.exports = bundle;
1516

@@ -63,7 +64,7 @@ function remap($refs, options) {
6364
function crawl(obj, path, $refs, remapped, options) {
6465
if (obj && typeof(obj) === 'object') {
6566
Object.keys(obj).forEach(function(key) {
66-
var keyPath = path + '/' + key;
67+
var keyPath = Pointer.join(path, key);
6768
var value = obj[key];
6869

6970
if ($Ref.is$Ref(value)) {
@@ -105,12 +106,13 @@ function dereference(basePath, $refs, options) {
105106
});
106107
}
107108

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){
109110
'use strict';
110111

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');
114116

115117
module.exports = dereference;
116118

@@ -138,10 +140,9 @@ function dereference(parser, options) {
138140
function crawl(obj, path, parents, $refs, options) {
139141
if (obj && typeof(obj) === 'object') {
140142
parents.push(obj);
141-
path = util.path.ensureHash(path);
142143

143144
Object.keys(obj).forEach(function(key) {
144-
var keyPath = path + '/' + key;
145+
var keyPath = Pointer.join(path, key);
145146
var value = obj[key];
146147

147148
if ($Ref.isAllowed$Ref(value, options)) {
@@ -194,7 +195,7 @@ function dereference$Ref(obj, key, value) {
194195
}
195196
}
196197

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){
198199
(function (Buffer){
199200
'use strict';
200201

@@ -665,6 +666,8 @@ var $Ref = require('./ref'),
665666
util = require('./util'),
666667
url = require('url'),
667668
ono = require('ono'),
669+
slashes = /\//g,
670+
tildes = /~/g,
668671
escapedSlash = /~1/g,
669672
escapedTilde = /~0/g;
670673

@@ -717,7 +720,7 @@ Pointer.prototype.resolve = function(obj, options) {
717720
for (var i = 0; i < tokens.length; i++) {
718721
if (resolveIf$Ref(this, options)) {
719722
// 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));
721724
}
722725

723726
var token = tokens[i];
@@ -815,6 +818,30 @@ Pointer.parse = function(path) {
815818
return pointer.slice(1);
816819
};
817820

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+
818845
/**
819846
* If the given pointer's {@link Pointer#value} is a JSON reference,
820847
* then the reference is resolved and {@link Pointer#value} is replaced with the resolved value.
@@ -1502,6 +1529,7 @@ function getPaths($refs, types) {
15021529

15031530
var Promise = require('./promise'),
15041531
$Ref = require('./ref'),
1532+
Pointer = require('./pointer'),
15051533
read = require('./read'),
15061534
util = require('./util'),
15071535
url = require('url'),
@@ -1568,8 +1596,8 @@ function crawl(obj, path, pathFromRoot, $refs, options) {
15681596
}
15691597

15701598
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);
15731601
var value = obj[key];
15741602

15751603
if ($Ref.isExternal$Ref(value)) {
@@ -1623,7 +1651,7 @@ function crawl$Ref(path, pathFromRoot, $refs, options) {
16231651
});
16241652
}
16251653

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){
16271655
(function (process){
16281656
'use strict';
16291657

@@ -1743,16 +1771,6 @@ exports.urlToLocalPath = function urlToLocalPath(url) {
17431771
return url;
17441772
};
17451773

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-
17561774
/**
17571775
* Returns the hash (URL fragment), if any, of the given path.
17581776
*

dist/ref-parser.js.map

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)