-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_pointer.js
118 lines (98 loc) · 3.45 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Jan Lehnardt
// https://github.com/janl/node-jsonpointer
// The MIT License (MIT)
// Copyright (c) 2011-2015 Jan Lehnardt <jan@apache.org> & Marc Bachmann <https://github.com/marcbachmann>
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
var hasExcape = /~/
var escapeMatcher = /~[01]/g
function escapeReplacer (m) {
switch (m) {
case '~1': return '/'
case '~0': return '~'
}
throw new Error('Invalid tilde escape: ' + m)
}
function untilde (str) {
if (!hasExcape.test(str)) return str
return str.replace(escapeMatcher, escapeReplacer)
}
function setter (obj, pointer, value) {
var part
var hasNextPart
for (var p = 1, len = pointer.length; p < len;) {
part = untilde(pointer[p++])
hasNextPart = len > p
if (typeof obj[part] === 'undefined') {
// support setting of /-
if (Array.isArray(obj) && part === '-') {
part = obj.length
}
// support nested objects/array when setting values
if (hasNextPart) {
if ((pointer[p] !== '' && pointer[p] < Infinity) || pointer[p] === '-') obj[part] = []
else obj[part] = {}
}
}
if (!hasNextPart) break
obj = obj[part]
}
var oldValue = obj[part]
if (value === undefined) delete obj[part]
else obj[part] = value
return oldValue
}
function compilePointer (pointer) {
if (typeof pointer === 'string') {
pointer = pointer.split('/')
if (pointer[0] === '') return pointer
throw new Error('Invalid JSON pointer.')
} else if (Array.isArray(pointer)) {
return pointer
}
throw new Error('Invalid JSON pointer.')
}
function get (obj, pointer) {
if (typeof obj !== 'object') throw new Error('Invalid input object.')
pointer = compilePointer(pointer)
var len = pointer.length
if (len === 1) return obj
for (var p = 1; p < len;) {
obj = obj[untilde(pointer[p++])]
if (len === p) return obj
if (typeof obj !== 'object') return undefined
}
}
function set (obj, pointer, value) {
if (typeof obj !== 'object') throw new Error('Invalid input object.')
pointer = compilePointer(pointer)
if (pointer.length === 0) throw new Error('Invalid JSON pointer for set.')
return setter(obj, pointer, value)
}
function compile (pointer) {
var compiled = compilePointer(pointer)
return {
get: function (object) {
return get(object, compiled)
},
set: function (object, value) {
return set(object, compiled, value)
}
}
}
exports.get = get
exports.set = set
exports.compile = compile