Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var visit = function(sub) {
if (sub && sub.id === ptr) return sub
if (typeof sub !== 'object' || !sub) return null
return Object.keys(sub).reduce(function(res, k) {
return res || visit(sub[k])
}, null)
}
var res = visit(obj)
if (res) return res
ptr = ptr.replace(/^#/, '')
ptr = ptr.replace(/\/$/, '')
try {
return jsonpointer.get(obj, decodeURI(ptr))
} catch (err) {
var end = ptr.indexOf('#')
var other
// external reference
if (end !== 0) {
// fragment doesn't exist.
if (end === -1) {
other = additionalSchemas[ptr]
} else {
var ext = ptr.slice(0, end)
other = additionalSchemas[ext]
var fragment = ptr.slice(end).replace(/^#/, '')
try {
return jsonpointer.get(other, fragment)
} catch (err) {}
}
return diff.reduce((accumulator, { path }) => {
// We don't want to show the last root object, as these tend to just go directly
// to a single value in the patch. This is fine, but not useful when showing a before/after
const pathSteps = path.split('/')
const backAStepPath = pathSteps.length <= 2 ? path : pathSteps.slice(0, pathSteps.length - 1).join('/')
const diff = {
after: jsonpointer.get(after, backAStepPath) || null,
before: jsonpointer.get(before, backAStepPath) || null,
}
const emptyValueOfCounterpart = other => {
if (Array.isArray(other)) {
return []
} else if (isobject(diff.after)) {
return {}
}
return null
}
const beforeValue = diff.before || emptyValueOfCounterpart(diff.after)
const afterValue = diff.after || emptyValueOfCounterpart(diff.before)
// If they both are arrays, add some extra metadata about what was
// added or removed. This makes it really easy to act on specific
// changes to JSON DSLs
if (Array.isArray(afterValue) && Array.isArray(beforeValue)) {
try {
return jsonpointer.get(obj, decodeURI(ptr))
} catch (err) {
var end = ptr.indexOf('#')
var other
// external reference
if (end !== 0) {
// fragment doesn't exist.
if (end === -1) {
other = additionalSchemas[ptr]
} else {
var ext = ptr.slice(0, end)
other = additionalSchemas[ext]
var fragment = ptr.slice(end).replace(/^#/, '')
try {
return jsonpointer.get(other, fragment)
} catch (err) {}
}
} else {
other = additionalSchemas[ptr]
}
return other || null
}
}
let href = null;
let internalLink = null;
// eslint-disable-next-line default-case
switch (linkType) {
case 'self':
href = getNestedObject(link, ['_links', 'site', 'href']);
internalLink = getNestedObject(link, ['_links', 'site', 'type']);
break;
case 'ref':
if (!link || !isString(link)) {
break;
}
// eslint-disable-next-line no-case-declarations
const linkedContent = jsonpointer.get(pageModel, link);
if (linkedContent) {
href = getNestedObject(linkedContent, ['_links', 'site', 'href']);
internalLink = getNestedObject(linkedContent, ['_links', 'site', 'type']);
}
break;
case 'href':
href = link;
internalLink = 'internal';
break;
}
// linkText is a function insteaf of a string, so that additional HTML can be included inside the anchor tag
if (href && internalLink && typeof linkText === 'function') {
return internalLink === 'internal' && typeof externalCreateLinkFunction === 'function'
? externalCreateLinkFunction(href, linkText, className)
try {
return jsonpointer.get(obj, decodeURI(ptr))
} catch (err) {
var end = ptr.indexOf('#')
var other
// external reference
if (end !== 0) {
// fragment doesn't exist.
if (end === -1) {
other = additionalSchemas[ptr]
} else {
var ext = ptr.slice(0, end)
other = additionalSchemas[ext]
var fragment = ptr.slice(end).replace(/^#/, '')
try {
return jsonpointer.get(other, fragment)
} catch (err) {}
}
} else {
other = additionalSchemas[ptr]
}
return other || null
}
}
try {
return jsonpointer.get(obj, decodeURI(ptr))
} catch (err) {
var end = ptr.indexOf('#')
var other
// external reference
if (end !== 0) {
// fragment doesn't exist.
if (end === -1) {
other = additionalSchemas[ptr]
} else {
var ext = ptr.slice(0, end)
other = additionalSchemas[ext]
var fragment = ptr.slice(end).replace(/^#/, '')
try {
return jsonpointer.get(other, fragment)
} catch (err) {}
}
} else {
other = additionalSchemas[ptr]
}
return other || null
}
}
try {
return jsonpointer.get(obj, decodeURI(ptr))
} catch (err) {
var end = ptr.indexOf('#')
var other
// external reference
if (end !== 0) {
// fragment doesn't exist.
if (end === -1) {
other = additionalSchemas[ptr]
} else {
var ext = ptr.slice(0, end)
other = additionalSchemas[ext]
var fragment = ptr.slice(end).replace(/^#/, '')
try {
return jsonpointer.get(other, fragment)
} catch (err) {}
}
} else {
other = additionalSchemas[ptr]
}
return other || null
}
}
try {
return jsonpointer.get(obj, decodeURI(ptr))
} catch (err) {
var end = ptr.indexOf('#')
var other
// external reference
if (end !== 0) {
// fragment doesn't exist.
if (end === -1) {
other = additionalSchemas[ptr]
} else {
var ext = ptr.slice(0, end)
other = additionalSchemas[ext]
var fragment = ptr.slice(end).replace(/^#/, '')
try {
return jsonpointer.get(other, fragment)
} catch (err) {}
}
} else {
other = additionalSchemas[ptr]
}
return other || null
}
}
try {
return jsonpointer.get(obj, decodeURI(ptr))
} catch (err) {
var end = ptr.indexOf('#')
var other
// external reference
if (end !== 0) {
// fragment doesn't exist.
if (end === -1) {
other = additionalSchemas[ptr]
} else {
var ext = ptr.slice(0, end)
other = additionalSchemas[ext]
var fragment = ptr.slice(end).replace(/^#/, '')
try {
return jsonpointer.get(other, fragment)
} catch (err) {}
}
} else {
other = additionalSchemas[ptr]
}
return other || null
}
}
const manuelstofer = require("json-pointer"); // https://github.com/manuelstofer/json-pointer
const flitbit = require("json-ptr"); // https://github.com/flitbit/json-ptr
const doc = {
foo: [1, 2, 3, 4], // eslint-disable-line no-magic-numbers
baz: [
{
qux: "hello",
},
],
};
const pointer = "/baz/0/qux";
const compiled = ooPointer.compile(pointer);
const janlCompiled = jsonpointer.compile(pointer);
const evaluate = jsonpointerjs.get(doc);
const ptr = flitbit.create(pointer);
const suite = benchmark.Suite("pointer");
suite
.add("find", function() {
ooPointer.find(doc, pointer);
})
.add("compiled", function() {
compiled(doc);
})
.add("compile + compiled", function() {
ooPointer.compile(pointer)(doc);