forked from swiftlang/swift-docc-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-pointer.js
63 lines (56 loc) · 1.61 KB
/
json-pointer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2021 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
const SEPARATOR = '/';
function decode(token) {
// Decode escaped character sequences:
// * replace "~0" with "~"
// * replace "~1" with "/"
return token.replace(/~[0,1]/g, match => (({
'~0': '~',
'~1': '/',
})[match] || match));
}
// This can be used to lazily generate a sequence of the individual decoded
// reference tokens contained within a given JSON pointer, which are separated
// by the "/" character. Within each token, the string "~0" is decoded to "~"
// and the string "~1" is decoded to "/".
//
// Examples:
//
// [...tokenize('')] // []
// [...tokenize('!')] // []
// [...tokenize('/')] // ['']
// [...tokenize('/a/b/c')] // ['a', 'b', 'c']
// [...tokenize('/~0/~1']) // ['~', '/']
//
// for (const token of tokenize('/a/b/c')) {
// // ...
// }
function* tokenize(pointer) {
const start = 1;
if (pointer.length < start || pointer.charAt(0) !== SEPARATOR) {
return;
}
let encodedToken = '';
let i = start;
while (i < pointer.length) {
const char = pointer.charAt(i);
if (char === SEPARATOR) {
yield decode(encodedToken);
encodedToken = '';
} else {
encodedToken += char;
}
i += 1;
}
yield decode(encodedToken);
}
// eslint-disable-next-line import/prefer-default-export
export { tokenize };