Socket
Socket
Sign inDemoInstall

json-ptr

Package Overview
Dependencies
Maintainers
1
Versions
91
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-ptr - npm Package Compare versions

Comparing version 1.2.1-ts.64a7978.0 to 1.2.1-ts.c739a2e.0

dist/types.d.ts

125

dist/__test__/ptr.spec.js

@@ -726,86 +726,61 @@ "use strict";

});
describe('#list', function () {
describe('#list...', function () {
const data = {
a: 1,
b: {
c: 2,
},
d: {
e: [
{
a: 3,
},
{
b: 4,
},
{
c: 5,
},
],
},
b: { c: 2 },
d: { e: [{ a: 3 }, { b: 4 }, { c: 5 }] },
f: null,
};
const tests = [
{
name: '#list(data)',
args: [data, undefined],
ptr: 'pointer',
res: [
['', data],
['/a', data.a],
['/b', data.b],
['/d', data.d],
['/f', data.f],
['/b/c', data.b.c],
['/d/e', data.d.e],
['/d/e/0', data.d.e[0]],
['/d/e/1', data.d.e[1]],
['/d/e/2', data.d.e[2]],
['/d/e/0/a', data.d.e[0].a],
['/d/e/1/b', data.d.e[1].b],
['/d/e/2/c', data.d.e[2].c],
],
},
{
name: '#list(data, true)',
args: [data, true],
ptr: 'fragmentId',
res: [
['#', data],
['#/a', data.a],
['#/b', data.b],
['#/d', data.d],
['#/f', data.f],
['#/b/c', data.b.c],
['#/d/e', data.d.e],
['#/d/e/0', data.d.e[0]],
['#/d/e/1', data.d.e[1]],
['#/d/e/2', data.d.e[2]],
['#/d/e/0/a', data.d.e[0].a],
['#/d/e/1/b', data.d.e[1].b],
['#/d/e/2/c', data.d.e[2].c],
],
},
const pointerList = [
['', data],
['/a', data.a],
['/b', data.b],
['/d', data.d],
['/f', data.f],
['/b/c', data.b.c],
['/d/e', data.d.e],
['/d/e/0', data.d.e[0]],
['/d/e/1', data.d.e[1]],
['/d/e/2', data.d.e[2]],
['/d/e/0/a', data.d.e[0].a],
['/d/e/1/b', data.d.e[1].b],
['/d/e/2/c', data.d.e[2].c],
];
tests.forEach(function (test) {
describe(test.name, function () {
let items;
before(function () {
items = __1.JsonPointer.list(test.args[0], test.args[1]);
const fragmentList = [
['#', data],
['#/a', data.a],
['#/b', data.b],
['#/d', data.d],
['#/f', data.f],
['#/b/c', data.b.c],
['#/d/e', data.d.e],
['#/d/e/0', data.d.e[0]],
['#/d/e/1', data.d.e[1]],
['#/d/e/2', data.d.e[2]],
['#/d/e/0/a', data.d.e[0].a],
['#/d/e/1/b', data.d.e[1].b],
['#/d/e/2/c', data.d.e[2].c],
];
describe('listPointers(target)', function () {
const items = __1.JsonPointer.listPointers(data);
pointerList.forEach(function (tt, n) {
it(`item ${n} has pointer ${tt[0]}`, () => {
chai_1.expect(items[n].pointer).to.equal(tt[0]);
});
test.res.forEach(function (tt, n) {
it('item ' + n + ' has ' + test.ptr + " '" + tt[0] + "'", function () {
if (test.ptr === 'fragmentId') {
chai_1.expect(items[n].fragmentId).to.equal(tt[0]);
}
else {
chai_1.expect(items[n].pointer).to.equal(tt[0]);
}
});
it('item ' + n + ' has correct value', function () {
chai_1.expect(items[n].value).to.equal(tt[1]);
});
it(`item ${n} has value ${tt[1]}`, () => {
chai_1.expect(items[n].value).to.equal(tt[1]);
});
});
});
describe('listFragmentIds(target)', function () {
const items = __1.JsonPointer.listFragmentIds(data);
fragmentList.forEach(function (tt, n) {
it(`item ${n} has fragmentId ${tt[0]}`, () => {
chai_1.expect(items[n].fragmentId).to.equal(tt[0]);
});
it(`item ${n} has value ${tt[1]}`, () => {
chai_1.expect(items[n].value).to.equal(tt[1]);
});
});
});
});

@@ -812,0 +787,0 @@ });

1

dist/index.d.ts

@@ -0,2 +1,3 @@

export * from './types';
export * from './util';
export * from './pointer';

@@ -0,5 +1,6 @@

import { JsonStringPointer, UriFragmentIdentifierPointer, Pointer, PathSegments, JsonStringPointerListItem, UriFragmentIdentifierPointerListItem } from './types';
/**
* Signature of visitor functions, used with [[JsonPointer.visit]] method. Visitors are callbacks invoked for every segment/branch of a target's object graph.
*/
export declare type Visitor = (ptr: string, val: any) => void;
export declare type Visitor = (ptr: JsonStringPointer, val: unknown) => void;
declare const $ptr: unique symbol;

@@ -9,25 +10,2 @@ declare const $frg: unique symbol;

/**
* A simple dictionary interface used while calculating pointers present in an object graph.
*/
export interface Dict {
[ptr: string]: any;
}
/**
* A simple interface used to list pointers and their values in an object graph.
*/
export interface PointerPair {
/**
* Contains the location of the value in the evaluated object graph. Present unless `fragmentId` is requested during the operation.
*/
pointer?: string;
/**
* Contains the location (as a fragmentId) of the value in the evaluated object graph. Present if `fragmentId` is requested during the operation.
*/
fragmentId?: string;
/**
* The value at the pointer's location in the object graph.
*/
value: any;
}
/**
* Represents a JSON Pointer, capable of getting and setting values on object graphs at the pointer's dereferenced location.

@@ -47,3 +25,3 @@ *

*/
static create(ptr: string | string[]): JsonPointer;
static create(ptr: Pointer | PathSegments): JsonPointer;
/**

@@ -54,3 +32,3 @@ * Determines if the `target` object has a value at the pointer's location in the object graph.

*/
static has<T>(target: T, ptr: string | string[] | JsonPointer): boolean;
static has(target: unknown, ptr: Pointer | PathSegments | JsonPointer): boolean;
/**

@@ -61,3 +39,3 @@ * Gets the value at the specified pointer's location in the object graph. If there is no value, then the result is `undefined`.

*/
static get<T, R>(target: T, ptr: string | string[] | JsonPointer): R;
static get(target: unknown, ptr: Pointer | PathSegments | JsonPointer): unknown;
/**

@@ -70,3 +48,3 @@ * Set the value at the specified pointer's location in the object graph.

*/
static set<T, V, R>(target: T, ptr: string | string[] | JsonPointer, val: V, force?: boolean): R;
static set(target: unknown, ptr: Pointer | PathSegments | JsonPointer, val: unknown, force?: boolean): unknown;
/**

@@ -76,3 +54,3 @@ * Decodes the specified pointer into path segments.

*/
static decode(ptr: string): string[];
static decode(ptr: Pointer): PathSegments;
/**

@@ -84,25 +62,29 @@ * Evaluates the target's object graph, calling the specified visitor for every unique pointer location discovered while walking the graph.

*/
static visit<T>(target: T, visitor: Visitor, fragmentId?: boolean): void;
static visit(target: unknown, visitor: Visitor, fragmentId?: boolean): void;
/**
* Evaluates the target's object graph, returning a [[PointerPair]] for each location in the graph.
* Evaluates the target's object graph, returning a [[JsonStringPointerListItem]] for each location in the graph.
* @param target the target of the operation
* @param fragmentId indicates whether the results are populated with fragment identifiers rather than regular pointers
*/
static list<T>(target: T, fragmentId?: boolean): PointerPair[];
static listPointers(target: unknown): JsonStringPointerListItem[];
/**
* Evaluates the target's object graph, returning a [[Dict]] populated with pointers and the corresponding values from the graph.
* Evaluates the target's object graph, returning a [[UriFragmentIdentifierPointerListItem]] for each location in the graph.
* @param target the target of the operation
*/
static listFragmentIds(target: unknown): UriFragmentIdentifierPointerListItem[];
/**
* Evaluates the target's object graph, returning a Record&lt;Pointer, unknown> populated with pointers and the corresponding values from the graph.
* @param target the target of the operation
* @param fragmentId indicates whether the results are populated with fragment identifiers rather than regular pointers
*/
static flatten<T>(target: T, fragmentId?: boolean): Dict;
static flatten(target: unknown, fragmentId?: boolean): Record<Pointer, unknown>;
/**
* Evaluates the target's object graph, returning a Map&lt;string,any> populated with pointers and the corresponding values form the graph.
* Evaluates the target's object graph, returning a Map&lt;Pointer,unknown> populated with pointers and the corresponding values form the graph.
* @param target the target of the operation
* @param fragmentId indicates whether the results are populated with fragment identifiers rather than regular pointers
*/
static map<T>(target: T, fragmentId?: boolean): Map<string, any>;
static map(target: unknown, fragmentId?: boolean): Map<Pointer, unknown>;
/**
* The pointer's decoded path through the object graph.
*/
path: string[];
readonly path: PathSegments;
/**

@@ -112,3 +94,3 @@ * Creates a new instance.

*/
constructor(ptr: string | string[]);
constructor(ptr: Pointer | PathSegments);
/**

@@ -118,3 +100,3 @@ * Gets the value the specified target's object graph at this pointer's location.

*/
get<T, V>(target: T): V;
get(target: unknown): unknown;
/**

@@ -128,3 +110,3 @@ * Set's the specified value in the specified target's object graph at this pointer's location.

*/
set<T, V>(target: T, value: V, force?: boolean): any;
set(target: unknown, value: unknown, force?: boolean): unknown;
/**

@@ -134,3 +116,3 @@ * Determines if the specified target's object graph has a value at the pointer's location.

*/
has<T>(target: T): boolean;
has(target: unknown): boolean;
/**

@@ -140,19 +122,24 @@ * Creates a new instance by concatenating the specified pointer's path with this pointer's path.

*/
concat(ptr: JsonPointer | string | string[]): JsonPointer;
concat(ptr: JsonPointer | Pointer | PathSegments): JsonPointer;
/**
* This pointer's JSON Pointer encoded string representation.
*/
get pointer(): string;
get pointer(): JsonStringPointer;
/**
* This pointer's URI fragment identifier encoded string representation.
*/
get uriFragmentIdentifier(): string;
get uriFragmentIdentifier(): UriFragmentIdentifierPointer;
/**
* Produces this pointer's JSON Pointer encoded string representation.
*/
toString(): string;
}
export declare class JsonReference {
static isReference<T>(candidate: T): boolean;
static isReference(candidate: unknown): candidate is JsonReference;
pointer: JsonPointer;
$ref: string;
constructor(ptr: JsonPointer | string | string[]);
resolve<T, R>(target: T): R;
$ref: UriFragmentIdentifierPointer;
constructor(ptr: JsonPointer | Pointer | PathSegments);
resolve(target: unknown): unknown;
toString(): string;
}
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("./util");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let descendingVisit = null;
function isObject(value) {
return typeof value === 'object' && value !== null;
}
function descendingVisit(target, visitor, encoder, cycle = false) {
const distinctObjects = new Map();
const q = [];
let cursor2 = 0;
q.push({
obj: target,
path: [],
});
visitor(encoder([]), target);
while (cursor2 < q.length) {
const cursor = q[cursor2++];
if (isObject(cursor.obj)) {
if (Array.isArray(cursor.obj)) {
let j = -1;
const len2 = cursor.obj.length;
while (++j < len2) {
const it = cursor.obj[j];
const path = cursor.path.concat([j + '']);
if (isObject(it)) {
if (cycle && distinctObjects.has(it)) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define,@typescript-eslint/no-non-null-assertion
visitor(encoder(path), new JsonReference(distinctObjects.get(it)));
continue;
}
q.push({
obj: it,
path: path,
});
if (cycle) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
distinctObjects.set(it, new JsonPointer(util_1.encodeUriFragmentIdentifier(path)));
}
}
visitor(encoder(path), it);
}
}
else {
const keys = Object.keys(cursor.obj);
const len3 = keys.length;
let i = -1;
while (++i < len3) {
const it = cursor.obj[keys[i]];
const path = cursor.path.concat(keys[i]);
if (isObject(it)) {
if (cycle && distinctObjects.has(it)) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define,@typescript-eslint/no-non-null-assertion
visitor(encoder(path), new JsonReference(distinctObjects.get(it)));
continue;
}
q.push({
obj: it,
path: path,
});
if (cycle) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
distinctObjects.set(it, new JsonPointer(util_1.encodeUriFragmentIdentifier(path)));
}
}
visitor(encoder(path), it);
}
}
}
}
}
const $ptr = Symbol('pointer');

@@ -60,3 +125,3 @@ const $frg = Symbol('fragmentId');

*/
static set(target, ptr, val, force) {
static set(target, ptr, val, force = false) {
if (typeof ptr === 'string' || Array.isArray(ptr)) {

@@ -80,27 +145,33 @@ ptr = new JsonPointer(ptr);

*/
static visit(target, visitor, fragmentId) {
static visit(target, visitor, fragmentId = false) {
descendingVisit(target, visitor, fragmentId ? util_1.encodeUriFragmentIdentifier : util_1.encodePointer);
}
/**
* Evaluates the target's object graph, returning a [[PointerPair]] for each location in the graph.
* Evaluates the target's object graph, returning a [[JsonStringPointerListItem]] for each location in the graph.
* @param target the target of the operation
* @param fragmentId indicates whether the results are populated with fragment identifiers rather than regular pointers
*/
static list(target, fragmentId) {
static listPointers(target) {
const res = [];
descendingVisit(target, fragmentId
? (fragmentId, value) => {
res.push({ fragmentId, value });
}
: (pointer, value) => {
res.push({ pointer, value });
}, fragmentId ? util_1.encodeUriFragmentIdentifier : util_1.encodePointer);
descendingVisit(target, (pointer, value) => {
res.push({ pointer, value });
}, util_1.encodePointer);
return res;
}
/**
* Evaluates the target's object graph, returning a [[Dict]] populated with pointers and the corresponding values from the graph.
* Evaluates the target's object graph, returning a [[UriFragmentIdentifierPointerListItem]] for each location in the graph.
* @param target the target of the operation
*/
static listFragmentIds(target) {
const res = [];
descendingVisit(target, (fragmentId, value) => {
res.push({ fragmentId, value });
}, util_1.encodeUriFragmentIdentifier);
return res;
}
/**
* Evaluates the target's object graph, returning a Record&lt;Pointer, unknown> populated with pointers and the corresponding values from the graph.
* @param target the target of the operation
* @param fragmentId indicates whether the results are populated with fragment identifiers rather than regular pointers
*/
static flatten(target, fragmentId) {
static flatten(target, fragmentId = false) {
const res = {};

@@ -113,9 +184,7 @@ descendingVisit(target, (p, v) => {

/**
* Evaluates the target's object graph, returning a Map&lt;string,any> populated with pointers and the corresponding values form the graph.
* Evaluates the target's object graph, returning a Map&lt;Pointer,unknown> populated with pointers and the corresponding values form the graph.
* @param target the target of the operation
* @param fragmentId indicates whether the results are populated with fragment identifiers rather than regular pointers
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static map(target, fragmentId) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static map(target, fragmentId = false) {
const res = new Map();

@@ -143,4 +212,3 @@ descendingVisit(target, res.set.bind(res), fragmentId ? util_1.encodeUriFragmentIdentifier : util_1.encodePointer);

*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
set(target, value, force) {
set(target, value, force = false) {
return util_1.setValueAtPath(target, value, this.path, force);

@@ -180,10 +248,10 @@ }

}
/**
* Produces this pointer's JSON Pointer encoded string representation.
*/
toString() {
return this.pointer;
}
}
exports.JsonPointer = JsonPointer;
/**
* Produces this pointer's JSON Pointer encoded string representation.
*/
JsonPointer.prototype.toString = function toString() {
return this.pointer;
};
class JsonReference {

@@ -203,72 +271,7 @@ constructor(ptr) {

}
toString() {
return this.$ref;
}
}
exports.JsonReference = JsonReference;
JsonReference.prototype.toString = function toString() {
return this.$ref;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
descendingVisit = (target, visitor, encoder, cycle) => {
let distinctObjects;
const q = [];
let cursor2 = 0;
q.push({
obj: target,
path: [],
});
if (cycle) {
distinctObjects = Object.create(null);
}
visitor(encoder([]), target);
while (cursor2 < q.length) {
const cursor = q[cursor2++];
const typeT = typeof cursor.obj;
if (typeT === 'object' && cursor.obj !== null) {
if (Array.isArray(cursor.obj)) {
let j = -1;
const len2 = cursor.obj.length;
while (++j < len2) {
const it = cursor.obj[j];
const path = cursor.path.concat([j + '']);
if (typeof it === 'object' && it !== null) {
if (cycle && distinctObjects[it]) {
visitor(encoder(path), new JsonReference(distinctObjects[it]));
continue;
}
q.push({
obj: it,
path: path,
});
if (cycle) {
distinctObjects[it] = new JsonPointer(util_1.encodeUriFragmentIdentifier(path));
}
}
visitor(encoder(path), it);
}
}
else {
const keys = Object.keys(cursor.obj);
const len3 = keys.length;
let i = -1;
while (++i < len3) {
const it = cursor.obj[keys[i]];
const path = cursor.path.concat(keys[i]);
if (typeof it === 'object' && it !== null) {
if (cycle && distinctObjects[it]) {
visitor(encoder(path), new JsonReference(distinctObjects[it]));
continue;
}
q.push({
obj: it,
path: path,
});
if (cycle) {
distinctObjects[it] = new JsonPointer(util_1.encodeUriFragmentIdentifier(path));
}
}
visitor(encoder(path), it);
}
}
}
}
};
//# sourceMappingURL=pointer.js.map

@@ -0,20 +1,19 @@

import { JsonStringPointer, UriFragmentIdentifierPointer, Pointer, PathSegments, Decoder } from './types';
export declare function replace(source: string, find: string, repl: string): string;
export declare type Decoder = (ptr: string) => string[];
export declare type Encoder = (ptr: string[]) => string;
export declare function decodeFragmentSegments(segments: string[]): string[];
export declare function encodeFragmentSegments(segments: string[]): string[];
export declare function decodePointerSegments(segments: string[]): string[];
export declare function encodePointerSegments(segments: string[]): string[];
export declare function decodePointer(ptr: string): string[];
export declare function encodePointer(path: string[]): string;
export declare function decodeUriFragmentIdentifier(ptr: string): string[];
export declare function encodeUriFragmentIdentifier(path: string[]): string;
export declare function toArrayIndexReference(arr: string[], idx: string): number;
export declare function hasValueAtPath<T>(target: T, path: string[]): boolean;
export declare function getValueAtPath<T>(target: T, path: string[]): any;
export declare type Dereference = <T, V>(it: T) => V;
export declare function compilePointerDereference(path: string[]): Dereference;
export declare function setValueAtPath<V>(target: any, val: V, path: string[], force: boolean): any;
export declare function looksLikeFragment(ptr: string): boolean;
export declare function pickDecoder(ptr: string): Decoder;
export declare function decodePtrInit(ptr: string | string[]): string[];
export declare function decodeFragmentSegments(segments: PathSegments): PathSegments;
export declare function encodeFragmentSegments(segments: PathSegments): PathSegments;
export declare function decodePointerSegments(segments: PathSegments): PathSegments;
export declare function encodePointerSegments(segments: PathSegments): PathSegments;
export declare function decodePointer(ptr: Pointer): PathSegments;
export declare function encodePointer(path: PathSegments): JsonStringPointer;
export declare function decodeUriFragmentIdentifier(ptr: UriFragmentIdentifierPointer): PathSegments;
export declare function encodeUriFragmentIdentifier(path: PathSegments): UriFragmentIdentifierPointer;
export declare function toArrayIndexReference(arr: readonly unknown[], idx: string): number;
export declare function hasValueAtPath(target: unknown, path: PathSegments): boolean;
export declare function getValueAtPath(target: unknown, path: PathSegments): unknown;
export declare type Dereference = (it: unknown) => unknown;
export declare function compilePointerDereference(path: PathSegments): Dereference;
export declare function setValueAtPath(target: unknown, val: unknown, path: PathSegments, force?: boolean): unknown;
export declare function looksLikeFragment(ptr: Pointer): boolean;
export declare function pickDecoder(ptr: Pointer): Decoder;
export declare function decodePtrInit(ptr: Pointer | PathSegments): PathSegments;

@@ -139,6 +139,6 @@ "use strict";

function hasValueAtPath(target, path) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let it;
let len;
let cursor;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let step;

@@ -156,2 +156,3 @@ let p;

if (isNaN(n) || !isFinite(n)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
it = it[step];

@@ -178,3 +179,2 @@ continue;

exports.hasValueAtPath = hasValueAtPath;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getValueAtPath(target, path) {

@@ -185,3 +185,2 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any

let cursor;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let step;

@@ -199,2 +198,3 @@ let p;

if (isNaN(n) || !isFinite(n)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
it = it[step];

@@ -224,6 +224,5 @@ continue;

if (path.length === 0) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (it) => it;
}
body = path.reduce((body, p, i) => {
body = path.reduce((body, _, i) => {
return body + " && \n\ttypeof((it = it['" + replace(path[i], '\\', '\\\\') + "'])) !== 'undefined'";

@@ -236,4 +235,3 @@ }, "if (typeof(it) !== 'undefined'");

exports.compilePointerDereference = compilePointerDereference;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function setValueAtPath(target, val, path, force) {
function setValueAtPath(target, val, path, force = false) {
if (path.length === 0) {

@@ -245,2 +243,3 @@ throw new Error('Cannot set the root object; assign it directly.');

}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let it = target;

@@ -306,3 +305,3 @@ const len = path.length;

function looksLikeFragment(ptr) {
return ptr && ptr.length && ptr[0] === '#';
return (ptr === null || ptr === void 0 ? void 0 : ptr.length) > 0 && ptr[0] === '#';
}

@@ -309,0 +308,0 @@ exports.looksLikeFragment = looksLikeFragment;

{
"name": "json-ptr",
"version": "1.2.1-ts.64a7978.0",
"version": "1.2.1-ts.c739a2e.0",
"author": "Phillip Clark <phillip@flitbit.com>",

@@ -33,23 +33,23 @@ "description": "A complete implementation of JSON Pointer (RFC 6901) for nodejs and modern browsers.",

"@types/bent": "^7.0.1",
"@types/chai": "^4.2.9",
"@types/mocha": "^7.0.1",
"@typescript-eslint/eslint-plugin": "^2.19.2",
"@typescript-eslint/parser": "^2.19.2",
"bent": "^7.0.6",
"@types/chai": "^4.2.11",
"@types/mocha": "^7.0.2",
"@typescript-eslint/eslint-plugin": "^2.25.0",
"@typescript-eslint/parser": "^2.25.0",
"bent": "^7.1.2",
"chai": "^4.2.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-json": "^2.0.1",
"eslint-plugin-mocha": "^6.2.2",
"eslint-plugin-json": "^2.1.1",
"eslint-plugin-mocha": "^6.3.0",
"eslint-plugin-prettier": "^3.1.2",
"lodash": "^4.17.15",
"mocha": "^7.0.1",
"mocha": "^7.1.1",
"nyc": "^15.0.0",
"prettier": "^1.19.1",
"prettier": "^2.0.2",
"rimraf": "^3.0.2",
"source-map-support": "^0.5.16",
"ts-node": "^8.6.2",
"typedoc": "^0.16.10",
"typescript": "^3.7.5"
"ts-node": "^8.8.1",
"typedoc": "^0.17.3",
"typescript": "^3.8.3"
},

@@ -56,0 +56,0 @@ "nyc": {

@@ -1,2 +0,8 @@

import { JsonPointer, encodePointer, encodeUriFragmentIdentifier, PointerPair } from '..';
import {
JsonPointer,
encodePointer,
encodeUriFragmentIdentifier,
JsonStringPointerListItem,
UriFragmentIdentifierPointerListItem,
} from '..';
import { expect } from 'chai';

@@ -6,4 +12,4 @@

describe('JsonPointer', function() {
describe('when working with the example data from the rfc', function() {
describe('JsonPointer', function () {
describe('when working with the example data from the rfc', function () {
const data = {

@@ -22,11 +28,11 @@ foo: ['bar', 'baz'],

describe('with a JSON pointer to the root ``', function() {
describe('with a JSON pointer to the root ``', function () {
const p = create('');
it('#get should resolve to the object itself', function() {
it('#get should resolve to the object itself', function () {
expect(p.get(data)).to.eql(data);
});
it('#set should throw', function() {
expect(function() {
it('#set should throw', function () {
expect(function () {
p.set(data, {

@@ -38,11 +44,11 @@ this: 'should cause an exception',

it('should have an empty path', function() {
it('should have an empty path', function () {
expect(p.path).to.have.length(0);
});
it('should have a pointer that is empty', function() {
it('should have a pointer that is empty', function () {
expect(p.pointer).to.eql('');
});
it('should have a URI fragment identifier that is empty', function() {
it('should have a URI fragment identifier that is empty', function () {
expect(p.uriFragmentIdentifier).to.eql('#');

@@ -52,11 +58,11 @@ });

describe('a URI fragment identifier to the root #', function() {
describe('a URI fragment identifier to the root #', function () {
const p = create('#');
it('#get should resolve to the object itself', function() {
it('#get should resolve to the object itself', function () {
expect(p.get(data)).to.equal(data);
});
it('#set should throw', function() {
expect(function() {
it('#set should throw', function () {
expect(function () {
p.set(data, {

@@ -68,11 +74,11 @@ this: 'should cause an exception',

it('should have an empty path', function() {
it('should have an empty path', function () {
expect(p.path).to.have.length(0);
});
it('should have a pointer that is empty', function() {
it('should have a pointer that is empty', function () {
expect(p.pointer).to.eql('');
});
it('should have a URI fragment identifier that is empty', function() {
it('should have a URI fragment identifier that is empty', function () {
expect(p.uriFragmentIdentifier).to.eql('#');

@@ -82,10 +88,10 @@ });

describe('with a JSON pointer of `/foo`', function() {
describe('with a JSON pointer of `/foo`', function () {
const p = create('/foo');
it('#get should resolve to data["foo"]', function() {
it('#get should resolve to data["foo"]', function () {
expect(p.get(data)).to.equal(data.foo);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -100,11 +106,11 @@ const updated = {

it('should have a path of [ "foo" ]', function() {
it('should have a path of [ "foo" ]', function () {
expect(p.path).to.eql(['foo']);
});
it('should have the pointer `/foo`', function() {
it('should have the pointer `/foo`', function () {
expect(p.pointer).to.eql('/foo');
});
it('should have the URI fragment identifier `#/foo`', function() {
it('should have the URI fragment identifier `#/foo`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/foo');

@@ -114,10 +120,10 @@ });

describe('a URI fragment identifier of `#/foo`', function() {
describe('a URI fragment identifier of `#/foo`', function () {
const p = create('#/foo');
it('#get should resolve to data["foo"]', function() {
it('#get should resolve to data["foo"]', function () {
expect(p.get(data)).to.equal(data.foo);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -132,11 +138,11 @@ const updated = {

it('should have a path of [ "foo" ]', function() {
it('should have a path of [ "foo" ]', function () {
expect(p.path).to.eql(['foo']);
});
it('should have the pointer `/foo`', function() {
it('should have the pointer `/foo`', function () {
expect(p.pointer).to.eql('/foo');
});
it('should have the URI fragment identifier `#/foo`', function() {
it('should have the URI fragment identifier `#/foo`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/foo');

@@ -146,10 +152,10 @@ });

describe('with a JSON pointer of `/foo/0`', function() {
describe('with a JSON pointer of `/foo/0`', function () {
const p = create('/foo/0');
it('#get should resolve to data.foo[0]', function() {
it('#get should resolve to data.foo[0]', function () {
expect(p.get(data)).to.equal(data.foo[0]);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -164,11 +170,11 @@ const updated = {

it('should have a path of [ "foo", "0" ]', function() {
it('should have a path of [ "foo", "0" ]', function () {
expect(p.path).to.eql(['foo', '0']);
});
it('should have the pointer `/foo/0`', function() {
it('should have the pointer `/foo/0`', function () {
expect(p.pointer).to.eql('/foo/0');
});
it('should have the URI fragment identifier `#/foo/0`', function() {
it('should have the URI fragment identifier `#/foo/0`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/foo/0');

@@ -178,10 +184,10 @@ });

describe('a URI fragment identifier of `#/foo/0`', function() {
describe('a URI fragment identifier of `#/foo/0`', function () {
const p = create('#/foo/0');
it('#get should resolve to data.foo[0]', function() {
it('#get should resolve to data.foo[0]', function () {
expect(p.get(data)).to.equal(data.foo[0]);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -196,11 +202,11 @@ const updated = {

it('should have a path of [ "foo", "0" ]', function() {
it('should have a path of [ "foo", "0" ]', function () {
expect(p.path).to.eql(['foo', '0']);
});
it('should have the pointer `/foo/0`', function() {
it('should have the pointer `/foo/0`', function () {
expect(p.pointer).to.eql('/foo/0');
});
it('should have the URI fragment identifier `#/foo/0`', function() {
it('should have the URI fragment identifier `#/foo/0`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/foo/0');

@@ -210,6 +216,6 @@ });

describe('a URI fragment identifier of `#/newArray/0`', function() {
describe('a URI fragment identifier of `#/newArray/0`', function () {
const p = create('#/newArray/0');
it('#get should resolve to undefined', function() {
it('#get should resolve to undefined', function () {
expect(p.get(data)).to.equal(undefined);

@@ -222,3 +228,3 @@ });

it('#set with force should succeed creating an array and setting the referenced value', function() {
it('#set with force should succeed creating an array and setting the referenced value', function () {
const blank: ItemWithNewArray = {};

@@ -235,11 +241,11 @@ const capture = p.get(data);

it('should have a path of [ "newArray", "0" ]', function() {
it('should have a path of [ "newArray", "0" ]', function () {
expect(p.path).to.eql(['newArray', '0']);
});
it('should have the pointer `/newArray/0`', function() {
it('should have the pointer `/newArray/0`', function () {
expect(p.pointer).to.eql('/newArray/0');
});
it('should have the URI fragment identifier `#/newArray/0`', function() {
it('should have the URI fragment identifier `#/newArray/0`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/newArray/0');

@@ -249,10 +255,10 @@ });

describe('with a JSON pointer of `/`', function() {
describe('with a JSON pointer of `/`', function () {
const p = create('/');
it('#get should resolve to data[""]', function() {
it('#get should resolve to data[""]', function () {
expect(p.get(data)).to.equal(data['']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -267,11 +273,11 @@ const updated = {

it('should have a path of [ "" ]', function() {
it('should have a path of [ "" ]', function () {
expect(p.path).to.eql(['']);
});
it('should have the pointer `/`', function() {
it('should have the pointer `/`', function () {
expect(p.pointer).to.eql('/');
});
it('should have the URI fragment identifier `#/`', function() {
it('should have the URI fragment identifier `#/`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/');

@@ -281,10 +287,10 @@ });

describe('a URI fragment identifier of `#/`', function() {
describe('a URI fragment identifier of `#/`', function () {
const p = create('#/');
it('#get should resolve to data[""]', function() {
it('#get should resolve to data[""]', function () {
expect(p.get(data)).to.equal(data['']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -299,11 +305,11 @@ const updated = {

it('should have a path of [ "" ]', function() {
it('should have a path of [ "" ]', function () {
expect(p.path).to.eql(['']);
});
it('should have the pointer `/`', function() {
it('should have the pointer `/`', function () {
expect(p.pointer).to.eql('/');
});
it('should have the URI fragment identifier `#/`', function() {
it('should have the URI fragment identifier `#/`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/');

@@ -313,10 +319,10 @@ });

describe('with a JSON pointer of `/a~1b`', function() {
describe('with a JSON pointer of `/a~1b`', function () {
const p = create('/a~1b');
it('#get should resolve to data["a/b"]', function() {
it('#get should resolve to data["a/b"]', function () {
expect(p.get(data)).to.equal(data['a/b']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -331,11 +337,11 @@ const updated = {

it('should have a path of [ "a/b" ]', function() {
it('should have a path of [ "a/b" ]', function () {
expect(p.path).to.eql(['a/b']);
});
it('should have the pointer `/a~1b`', function() {
it('should have the pointer `/a~1b`', function () {
expect(p.pointer).to.eql('/a~1b');
});
it('should have the URI fragment identifier `#/a~1b`', function() {
it('should have the URI fragment identifier `#/a~1b`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/a~1b');

@@ -345,10 +351,10 @@ });

describe('a URI fragment identifier of `#/a~1b`', function() {
describe('a URI fragment identifier of `#/a~1b`', function () {
const p = create('#/a~1b');
it('#get should resolve to data["a/b"]', function() {
it('#get should resolve to data["a/b"]', function () {
expect(p.get(data)).to.equal(data['a/b']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -363,11 +369,11 @@ const updated = {

it('should have a path of [ "a/b" ]', function() {
it('should have a path of [ "a/b" ]', function () {
expect(p.path).to.eql(['a/b']);
});
it('should have the pointer `/a~1b`', function() {
it('should have the pointer `/a~1b`', function () {
expect(p.pointer).to.eql('/a~1b');
});
it('should have the URI fragment identifier `#/a~1b`', function() {
it('should have the URI fragment identifier `#/a~1b`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/a~1b');

@@ -377,10 +383,10 @@ });

describe('with a JSON pointer of `/c%d`', function() {
describe('with a JSON pointer of `/c%d`', function () {
const p = create('/c%d');
it('#get should resolve to data["c%d"]', function() {
it('#get should resolve to data["c%d"]', function () {
expect(p.get(data)).to.equal(data['c%d']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -395,11 +401,11 @@ const updated = {

it('should have a path of [ "c%d" ]', function() {
it('should have a path of [ "c%d" ]', function () {
expect(p.path).to.eql(['c%d']);
});
it('should have the pointer `/c%d`', function() {
it('should have the pointer `/c%d`', function () {
expect(p.pointer).to.eql('/c%d');
});
it('should have the URI fragment identifier `#/c%25d`', function() {
it('should have the URI fragment identifier `#/c%25d`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/c%25d');

@@ -409,10 +415,10 @@ });

describe('a URI fragment identifier of `#/c%25d`', function() {
describe('a URI fragment identifier of `#/c%25d`', function () {
const p = create('#/c%25d');
it('#get should resolve to data["c%d"]', function() {
it('#get should resolve to data["c%d"]', function () {
expect(p.get(data)).to.equal(data['c%d']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -427,11 +433,11 @@ const updated = {

it('should have a path of [ "c%d" ]', function() {
it('should have a path of [ "c%d" ]', function () {
expect(p.path).to.eql(['c%d']);
});
it('should have the pointer `/c%d`', function() {
it('should have the pointer `/c%d`', function () {
expect(p.pointer).to.eql('/c%d');
});
it('should have the URI fragment identifier `#/c%25d`', function() {
it('should have the URI fragment identifier `#/c%25d`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/c%25d');

@@ -441,10 +447,10 @@ });

describe('with a JSON pointer of `/e^f`', function() {
describe('with a JSON pointer of `/e^f`', function () {
const p = create('/e^f');
it('#get should resolve to data["e^f"]', function() {
it('#get should resolve to data["e^f"]', function () {
expect(p.get(data)).to.equal(data['e^f']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -459,11 +465,11 @@ const updated = {

it('should have a path of [ "e^f" ]', function() {
it('should have a path of [ "e^f" ]', function () {
expect(p.path).to.eql(['e^f']);
});
it('should have the pointer `/e^f`', function() {
it('should have the pointer `/e^f`', function () {
expect(p.pointer).to.eql('/e^f');
});
it('should have the URI fragment identifier `#/e%5Ef`', function() {
it('should have the URI fragment identifier `#/e%5Ef`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/e%5Ef');

@@ -473,10 +479,10 @@ });

describe('a URI fragment identifier of `#/e%5Ef`', function() {
describe('a URI fragment identifier of `#/e%5Ef`', function () {
const p = create('#/e%5Ef');
it('#get should resolve to data["e^f"]', function() {
it('#get should resolve to data["e^f"]', function () {
expect(p.get(data)).to.equal(data['e^f']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -491,11 +497,11 @@ const updated = {

it('should have a path of [ "e^f" ]', function() {
it('should have a path of [ "e^f" ]', function () {
expect(p.path).to.eql(['e^f']);
});
it('should have the pointer `/e^f`', function() {
it('should have the pointer `/e^f`', function () {
expect(p.pointer).to.eql('/e^f');
});
it('should have the URI fragment identifier `#/e%5Ef`', function() {
it('should have the URI fragment identifier `#/e%5Ef`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/e%5Ef');

@@ -505,10 +511,10 @@ });

describe('with a JSON pointer of `/g|h`', function() {
describe('with a JSON pointer of `/g|h`', function () {
const p = create('/g|h');
it('#get should resolve to data["g|h"]', function() {
it('#get should resolve to data["g|h"]', function () {
expect(p.get(data)).to.equal(data['g|h']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -523,11 +529,11 @@ const updated = {

it('should have a path of [ "g|h" ]', function() {
it('should have a path of [ "g|h" ]', function () {
expect(p.path).to.eql(['g|h']);
});
it('should have the pointer `/g|h`', function() {
it('should have the pointer `/g|h`', function () {
expect(p.pointer).to.eql('/g|h');
});
it('should have the URI fragment identifier `#/g%7Ch`', function() {
it('should have the URI fragment identifier `#/g%7Ch`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/g%7Ch');

@@ -537,10 +543,10 @@ });

describe('a URI fragment identifier of `#/g%7Ch`', function() {
describe('a URI fragment identifier of `#/g%7Ch`', function () {
const p = create('#/g%7Ch');
it('#get should resolve to data["g|h"]', function() {
it('#get should resolve to data["g|h"]', function () {
expect(p.get(data)).to.equal(data['g|h']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -555,11 +561,11 @@ const updated = {

it('should have a path of [ "g|h" ]', function() {
it('should have a path of [ "g|h" ]', function () {
expect(p.path).to.eql(['g|h']);
});
it('should have the pointer `/g|h`', function() {
it('should have the pointer `/g|h`', function () {
expect(p.pointer).to.eql('/g|h');
});
it('should have the URI fragment identifier `#/g%7Ch`', function() {
it('should have the URI fragment identifier `#/g%7Ch`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/g%7Ch');

@@ -569,10 +575,10 @@ });

describe('with a JSON pointer of "/i\\j"', function() {
describe('with a JSON pointer of "/i\\j"', function () {
const p = create('/i\\j');
it('#get should resolve to data["i\\j"]', function() {
it('#get should resolve to data["i\\j"]', function () {
expect(p.get(data)).to.equal(data['i\\j']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -587,11 +593,11 @@ const updated = {

it('should have a path of [ "i\\j" ]', function() {
it('should have a path of [ "i\\j" ]', function () {
expect(p.path).to.eql(['i\\j']);
});
it('should have the pointer `/i\\j`', function() {
it('should have the pointer `/i\\j`', function () {
expect(p.pointer).to.eql('/i\\j');
});
it('should have the URI fragment identifier `#/i%5Cj`', function() {
it('should have the URI fragment identifier `#/i%5Cj`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/i%5Cj');

@@ -601,10 +607,10 @@ });

describe('a URI fragment identifier of `#/i%5Cj`', function() {
describe('a URI fragment identifier of `#/i%5Cj`', function () {
const p = create('#/i%5Cj');
it('#get should resolve to data["i\\j"]', function() {
it('#get should resolve to data["i\\j"]', function () {
expect(p.get(data)).to.equal(data['i\\j']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -619,11 +625,11 @@ const updated = {

it('should have a path of [ "i\\j" ]', function() {
it('should have a path of [ "i\\j" ]', function () {
expect(p.path).to.eql(['i\\j']);
});
it('should have the pointer `/i\\j`', function() {
it('should have the pointer `/i\\j`', function () {
expect(p.pointer).to.eql('/i\\j');
});
it('should have the URI fragment identifier `#/i%5Cj`', function() {
it('should have the URI fragment identifier `#/i%5Cj`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/i%5Cj');

@@ -633,3 +639,3 @@ });

describe("with a JSON pointer of '/k\\\"l'", function() {
describe("with a JSON pointer of '/k\\\"l'", function () {
// eslint-disable-next-line no-useless-escape

@@ -639,3 +645,3 @@ const p = create('/k"l');

// eslint-disable-next-line no-useless-escape
it('#get should resolve to data["k"l"]', function() {
it('#get should resolve to data["k"l"]', function () {
// eslint-disable-next-line no-useless-escape

@@ -645,3 +651,3 @@ expect(p.get(data)).to.equal(data['k"l']);

it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -657,3 +663,3 @@ const updated = {

// eslint-disable-next-line no-useless-escape
it('should have a path of [ "k"l" ]', function() {
it('should have a path of [ "k"l" ]', function () {
// eslint-disable-next-line no-useless-escape

@@ -664,3 +670,3 @@ expect(p.path).to.eql(['k"l']);

// eslint-disable-next-line no-useless-escape
it('should have the pointer `/k"l`', function() {
it('should have the pointer `/k"l`', function () {
// eslint-disable-next-line no-useless-escape

@@ -670,3 +676,3 @@ expect(p.pointer).to.eql('/k"l');

it('should have the URI fragment identifier `#/k%22l`', function() {
it('should have the URI fragment identifier `#/k%22l`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/k%22l');

@@ -676,7 +682,7 @@ });

describe('a URI fragment identifier of `#/k%22l`', function() {
describe('a URI fragment identifier of `#/k%22l`', function () {
const p = create('#/k%22l');
// eslint-disable-next-line no-useless-escape
it('#get should resolve to data["k"l"]', function() {
it('#get should resolve to data["k"l"]', function () {
// eslint-disable-next-line no-useless-escape

@@ -686,3 +692,3 @@ expect(p.get(data)).to.equal(data['k"l']);

it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -698,3 +704,3 @@ const updated = {

// eslint-disable-next-line no-useless-escape
it('should have a path of [ "k"l" ]', function() {
it('should have a path of [ "k"l" ]', function () {
// eslint-disable-next-line no-useless-escape

@@ -705,3 +711,3 @@ expect(p.path).to.eql(['k"l']);

// eslint-disable-next-line no-useless-escape
it('should have the pointer `/k"l`', function() {
it('should have the pointer `/k"l`', function () {
// eslint-disable-next-line no-useless-escape

@@ -711,3 +717,3 @@ expect(p.pointer).to.eql('/k"l');

it('should have the URI fragment identifier `#/k%22l`', function() {
it('should have the URI fragment identifier `#/k%22l`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/k%22l');

@@ -717,10 +723,10 @@ });

describe('with a JSON pointer of `/ `', function() {
describe('with a JSON pointer of `/ `', function () {
const p = create('/ ');
it('#get should resolve to data[" "]', function() {
it('#get should resolve to data[" "]', function () {
expect(p.get(data)).to.equal(data[' ']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -735,11 +741,11 @@ const updated = {

it('should have a path of [ " " ]', function() {
it('should have a path of [ " " ]', function () {
expect(p.path).to.eql([' ']);
});
it('should have the pointer `/ `', function() {
it('should have the pointer `/ `', function () {
expect(p.pointer).to.eql('/ ');
});
it('should have the URI fragment identifier `#/%20`', function() {
it('should have the URI fragment identifier `#/%20`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/%20');

@@ -749,10 +755,10 @@ });

describe('a URI fragment identifier of `#/%20`', function() {
describe('a URI fragment identifier of `#/%20`', function () {
const p = create('#/%20');
it('#get should resolve to data[" "]', function() {
it('#get should resolve to data[" "]', function () {
expect(p.get(data)).to.equal(data[' ']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -767,11 +773,11 @@ const updated = {

it('should have a path of [ " " ]', function() {
it('should have a path of [ " " ]', function () {
expect(p.path).to.eql([' ']);
});
it('should have the pointer `/ `', function() {
it('should have the pointer `/ `', function () {
expect(p.pointer).to.eql('/ ');
});
it('should have the URI fragment identifier `#/%20`', function() {
it('should have the URI fragment identifier `#/%20`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/%20');

@@ -781,10 +787,10 @@ });

describe('with a JSON pointer of `/m~0n`', function() {
describe('with a JSON pointer of `/m~0n`', function () {
const p = create('/m~0n');
it('#get should resolve to data["m~n"]', function() {
it('#get should resolve to data["m~n"]', function () {
expect(p.get(data)).to.equal(data['m~n']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -799,11 +805,11 @@ const updated = {

it('should have a path of [ "m~n" ]', function() {
it('should have a path of [ "m~n" ]', function () {
expect(p.path).to.eql(['m~n']);
});
it('should have the pointer `/m~0n`', function() {
it('should have the pointer `/m~0n`', function () {
expect(p.pointer).to.eql('/m~0n');
});
it('should have the URI fragment identifier `#/m~0n`', function() {
it('should have the URI fragment identifier `#/m~0n`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/m~0n');

@@ -813,10 +819,10 @@ });

describe('a URI fragment identifier of `#/m~0n`', function() {
describe('a URI fragment identifier of `#/m~0n`', function () {
const p = create('#/m~0n');
it('#get should resolve to data["m~n"]', function() {
it('#get should resolve to data["m~n"]', function () {
expect(p.get(data)).to.equal(data['m~n']);
});
it('#set should succeed changing the referenced value', function() {
it('#set should succeed changing the referenced value', function () {
const capture = p.get(data);

@@ -831,11 +837,11 @@ const updated = {

it('should have a path of [ "m~n" ]', function() {
it('should have a path of [ "m~n" ]', function () {
expect(p.path).to.eql(['m~n']);
});
it('should have the pointer `/m~0n`', function() {
it('should have the pointer `/m~0n`', function () {
expect(p.pointer).to.eql('/m~0n');
});
it('should have the URI fragment identifier `#/m~0n`', function() {
it('should have the URI fragment identifier `#/m~0n`', function () {
expect(p.uriFragmentIdentifier).to.eql('#/m~0n');

@@ -845,10 +851,10 @@ });

describe('a special array pointer from draft-ietf-appsawg-json-pointer-08 `/foo/-`', function() {
describe('a special array pointer from draft-ietf-appsawg-json-pointer-08 `/foo/-`', function () {
const p = create('/foo/-');
it('should not resolve via #get', function() {
it('should not resolve via #get', function () {
expect(p.get(data)).to.be.an('undefined');
});
it('should set the next element of the array, repeatedly...', function() {
it('should set the next element of the array, repeatedly...', function () {
p.set(data, 'qux');

@@ -858,3 +864,3 @@ expect(data.foo[2]).to.eql('qux');

it('...3', function() {
it('...3', function () {
p.set(data, 'quux');

@@ -864,3 +870,3 @@ expect(data.foo[3]).to.eql('quux');

it('...4', function() {
it('...4', function () {
p.set(data, 'corge');

@@ -870,3 +876,3 @@ expect(data.foo[4]).to.eql('corge');

it('...5', function() {
it('...5', function () {
p.set(data, 'grault');

@@ -877,5 +883,5 @@ expect(data.foo[5]).to.eql('grault');

describe('an invalid pointer', function() {
it('should fail to parse', function() {
expect(function() {
describe('an invalid pointer', function () {
it('should fail to parse', function () {
expect(function () {
create('a/');

@@ -886,5 +892,5 @@ }).to.throw();

describe('an invalid URI fragment identifier', function() {
it('should fail to parse', function() {
expect(function() {
describe('an invalid URI fragment identifier', function () {
it('should fail to parse', function () {
expect(function () {
create('#a');

@@ -896,3 +902,3 @@ }).to.throw();

describe('when working with complex data', function() {
describe('when working with complex data', function () {
const data = {

@@ -916,7 +922,7 @@ a: 1,

},
f: null as object,
f: null,
'http://schema.org/name': 'Phillip',
};
it('#get should return `undefined` when the requested element is undefined (#/g/h)', function() {
it('#get should return `undefined` when the requested element is undefined (#/g/h)', function () {
const unk = JsonPointer.get(data, '#/g/h');

@@ -926,3 +932,3 @@ expect(unk).to.be.an('undefined');

it('#get should return null when the requested element has a null value (#/f)', function() {
it('#get should return null when the requested element has a null value (#/f)', function () {
const unk = JsonPointer.get(data, '#/f');

@@ -932,3 +938,3 @@ expect(unk).to.eql(null);

it('#get should return the value of a prop named with multiple slahes (#/http:~1~1schema.org~1name)', function() {
it('#get should return the value of a prop named with multiple slahes (#/http:~1~1schema.org~1name)', function () {
const unk = JsonPointer.get(data, '#/http:~1~1schema.org~1name');

@@ -938,3 +944,3 @@ expect(unk).to.eql('Phillip');

it('#get should return the value of a prop named with multiple slahes (/http:~1~1schema.org~1name)', function() {
it('#get should return the value of a prop named with multiple slahes (/http:~1~1schema.org~1name)', function () {
const unk = JsonPointer.get(data, '/http:~1~1schema.org~1name');

@@ -944,3 +950,3 @@ expect(unk).to.eql('Phillip');

it('#set should set the value of a prop named with multiple slahes (#/http:~1~1schema.org~1name)', function() {
it('#set should set the value of a prop named with multiple slahes (#/http:~1~1schema.org~1name)', function () {
JsonPointer.set(data, '#/http:~1~1schema.org~1name', 'Phil');

@@ -951,3 +957,3 @@ const unk = JsonPointer.get(data, '/http:~1~1schema.org~1name');

it('#set should set the value of a prop named with multiple slahes (/http:~1~1schema.org~1name)', function() {
it('#set should set the value of a prop named with multiple slahes (/http:~1~1schema.org~1name)', function () {
JsonPointer.set(data, '/http:~1~1schema.org~1name', 'Phil');

@@ -959,10 +965,10 @@ const unk = JsonPointer.get(data, '/http:~1~1schema.org~1name');

describe('given an sequence of property names ["d", "e~f", "2"]', function() {
describe('given an sequence of property names ["d", "e~f", "2"]', function () {
const path = ['d', 'e~f', '2'];
it('#encodePointer should produce a pointer (/d/e~0f/2)', function() {
it('#encodePointer should produce a pointer (/d/e~0f/2)', function () {
expect(encodePointer(path)).to.eql('/d/e~0f/2');
});
it('#encodeUriFragmentIdentifier should produce a pointer (#/d/e~0f/2)', function() {
it('#encodeUriFragmentIdentifier should produce a pointer (#/d/e~0f/2)', function () {
expect(encodeUriFragmentIdentifier(path)).to.eql('#/d/e~0f/2');

@@ -972,88 +978,64 @@ });

describe('#list', function() {
describe('#list...', function () {
const data = {
a: 1,
b: {
c: 2,
},
d: {
e: [
{
a: 3,
},
{
b: 4,
},
{
c: 5,
},
],
},
f: null as object,
b: { c: 2 },
d: { e: [{ a: 3 }, { b: 4 }, { c: 5 }] },
f: null,
};
const tests = [
{
name: '#list(data)',
args: [data, undefined as boolean],
ptr: 'pointer',
res: [
['', data],
['/a', data.a],
['/b', data.b],
['/d', data.d],
['/f', data.f],
['/b/c', data.b.c],
['/d/e', data.d.e],
['/d/e/0', data.d.e[0]],
['/d/e/1', data.d.e[1]],
['/d/e/2', data.d.e[2]],
['/d/e/0/a', data.d.e[0].a],
['/d/e/1/b', data.d.e[1].b],
['/d/e/2/c', data.d.e[2].c],
],
},
{
name: '#list(data, true)',
args: [data, true],
ptr: 'fragmentId',
res: [
['#', data],
['#/a', data.a],
['#/b', data.b],
['#/d', data.d],
['#/f', data.f],
['#/b/c', data.b.c],
['#/d/e', data.d.e],
['#/d/e/0', data.d.e[0]],
['#/d/e/1', data.d.e[1]],
['#/d/e/2', data.d.e[2]],
['#/d/e/0/a', data.d.e[0].a],
['#/d/e/1/b', data.d.e[1].b],
['#/d/e/2/c', data.d.e[2].c],
],
},
const pointerList = [
['', data],
['/a', data.a],
['/b', data.b],
['/d', data.d],
['/f', data.f],
['/b/c', data.b.c],
['/d/e', data.d.e],
['/d/e/0', data.d.e[0]],
['/d/e/1', data.d.e[1]],
['/d/e/2', data.d.e[2]],
['/d/e/0/a', data.d.e[0].a],
['/d/e/1/b', data.d.e[1].b],
['/d/e/2/c', data.d.e[2].c],
];
tests.forEach(function(test) {
describe(test.name, function() {
let items: PointerPair[];
before(function() {
items = JsonPointer.list(test.args[0], test.args[1] as boolean);
const fragmentList = [
['#', data],
['#/a', data.a],
['#/b', data.b],
['#/d', data.d],
['#/f', data.f],
['#/b/c', data.b.c],
['#/d/e', data.d.e],
['#/d/e/0', data.d.e[0]],
['#/d/e/1', data.d.e[1]],
['#/d/e/2', data.d.e[2]],
['#/d/e/0/a', data.d.e[0].a],
['#/d/e/1/b', data.d.e[1].b],
['#/d/e/2/c', data.d.e[2].c],
];
describe('listPointers(target)', function () {
const items: JsonStringPointerListItem[] = JsonPointer.listPointers(data);
pointerList.forEach(function (tt, n) {
it(`item ${n} has pointer ${tt[0]}`, () => {
expect(items[n].pointer).to.equal(tt[0]);
});
test.res.forEach(function(tt, n) {
it('item ' + n + ' has ' + test.ptr + " '" + tt[0] + "'", function() {
if (test.ptr === 'fragmentId') {
expect(items[n].fragmentId).to.equal(tt[0]);
} else {
expect(items[n].pointer).to.equal(tt[0]);
}
});
it('item ' + n + ' has correct value', function() {
expect(items[n].value).to.equal(tt[1]);
});
it(`item ${n} has value ${tt[1]}`, () => {
expect(items[n].value).to.equal(tt[1]);
});
});
});
describe('listFragmentIds(target)', function () {
const items: UriFragmentIdentifierPointerListItem[] = JsonPointer.listFragmentIds(data);
fragmentList.forEach(function (tt, n) {
it(`item ${n} has fragmentId ${tt[0]}`, () => {
expect(items[n].fragmentId).to.equal(tt[0]);
});
it(`item ${n} has value ${tt[1]}`, () => {
expect(items[n].value).to.equal(tt[1]);
});
});
});
});
});
describe('when data contains an array early in the path', function() {
describe('when data contains an array early in the path', function () {
const data = {

@@ -1063,3 +1045,3 @@ foo: [] as number[],

it('#set(o, val, true) should create the path through the array #/foo/0/wilbur/idiocies', function() {
it('#set(o, val, true) should create the path through the array #/foo/0/wilbur/idiocies', function () {
const p = create('#/foo/0/wilbur/idiocies');

@@ -1071,3 +1053,3 @@ p.set(data, 5, true);

describe('concat pointers', function() {
describe('concat pointers', function () {
const ptr1 = create('/a/b');

@@ -1077,17 +1059,17 @@ const ptr2 = create('/c/d');

it('#concat JsonPointer("/a/b") with array ["a", "b"] should produce ' + result, function() {
it('#concat JsonPointer("/a/b") with array ["a", "b"] should produce ' + result, function () {
expect(ptr1.concat(Array.from(ptr2.path)).pointer).to.eql(result);
});
it('#concat JsonPointer("/a/b") with JsonPointer("/b/c") should produce ' + result, function() {
it('#concat JsonPointer("/a/b") with JsonPointer("/b/c") should produce ' + result, function () {
expect(ptr1.concat(ptr2).pointer).to.eql(result);
});
it('#concat JsonPointer("/a/b") with string "/b/c" should produce ' + result, function() {
it('#concat JsonPointer("/a/b") with string "/b/c" should produce ' + result, function () {
expect(ptr1.concat(ptr2.pointer).pointer).to.eql(result);
});
it('#concat JsonPointer("/a/b") with string "#/b/c" should produce ' + result, function() {
it('#concat JsonPointer("/a/b") with string "#/b/c" should produce ' + result, function () {
expect(ptr1.concat(ptr2.uriFragmentIdentifier).toString()).to.eql(result);
});
});

@@ -0,2 +1,3 @@

export * from './types';
export * from './util';
export * from './pointer';

@@ -9,14 +9,93 @@ import {

pickDecoder,
} from './util';
import {
JsonStringPointer,
UriFragmentIdentifierPointer,
Pointer,
PathSegments,
Encoder,
} from './util';
JsonStringPointerListItem,
UriFragmentIdentifierPointerListItem,
} from './types';
type UnknownObject = { [K in PropertyKey]: unknown };
function isObject(value: unknown): value is UnknownObject {
return typeof value === 'object' && value !== null;
}
/**
* Signature of visitor functions, used with [[JsonPointer.visit]] method. Visitors are callbacks invoked for every segment/branch of a target's object graph.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Visitor = (ptr: string, val: any) => void;
export type Visitor = (ptr: JsonStringPointer, val: unknown) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let descendingVisit: (target: any, visitor: Visitor, encoder: Encoder, cycle?: boolean) => void = null;
interface Item {
obj: unknown;
path: PathSegments;
}
function descendingVisit(target: unknown, visitor: Visitor, encoder: Encoder, cycle = false): void {
const distinctObjects = new Map<object, JsonPointer>();
const q: Item[] = [];
let cursor2 = 0;
q.push({
obj: target,
path: [],
});
visitor(encoder([]), target);
while (cursor2 < q.length) {
const cursor = q[cursor2++];
if (isObject(cursor.obj)) {
if (Array.isArray(cursor.obj)) {
let j = -1;
const len2 = cursor.obj.length;
while (++j < len2) {
const it = cursor.obj[j];
const path = cursor.path.concat([j + '']);
if (isObject(it)) {
if (cycle && distinctObjects.has(it)) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define,@typescript-eslint/no-non-null-assertion
visitor(encoder(path), new JsonReference(distinctObjects.get(it)!));
continue;
}
q.push({
obj: it,
path: path,
});
if (cycle) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
distinctObjects.set(it, new JsonPointer(encodeUriFragmentIdentifier(path)));
}
}
visitor(encoder(path), it);
}
} else {
const keys = Object.keys(cursor.obj);
const len3 = keys.length;
let i = -1;
while (++i < len3) {
const it = cursor.obj[keys[i]];
const path = cursor.path.concat(keys[i]);
if (isObject(it)) {
if (cycle && distinctObjects.has(it)) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define,@typescript-eslint/no-non-null-assertion
visitor(encoder(path), new JsonReference(distinctObjects.get(it)!));
continue;
}
q.push({
obj: it,
path: path,
});
if (cycle) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
distinctObjects.set(it, new JsonPointer(encodeUriFragmentIdentifier(path)));
}
}
visitor(encoder(path), it);
}
}
}
}
}
const $ptr = Symbol('pointer');

@@ -27,29 +106,2 @@ const $frg = Symbol('fragmentId');

/**
* A simple dictionary interface used while calculating pointers present in an object graph.
*/
export interface Dict {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[ptr: string]: any;
}
/**
* A simple interface used to list pointers and their values in an object graph.
*/
export interface PointerPair {
/**
* Contains the location of the value in the evaluated object graph. Present unless `fragmentId` is requested during the operation.
*/
pointer?: string;
/**
* Contains the location (as a fragmentId) of the value in the evaluated object graph. Present if `fragmentId` is requested during the operation.
*/
fragmentId?: string;
/**
* The value at the pointer's location in the object graph.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any;
}
/**
* Represents a JSON Pointer, capable of getting and setting values on object graphs at the pointer's dereferenced location.

@@ -62,4 +114,4 @@ *

export class JsonPointer {
private [$ptr]: string;
private [$frg]: string;
private [$ptr]: JsonStringPointer;
private [$frg]: UriFragmentIdentifierPointer;
private [$get]: Dereference;

@@ -71,3 +123,3 @@

*/
static create(ptr: string | string[]): JsonPointer {
static create(ptr: Pointer | PathSegments): JsonPointer {
return new JsonPointer(ptr);

@@ -81,8 +133,9 @@ }

*/
static has<T>(target: T, ptr: string | string[] | JsonPointer): boolean {
static has(target: unknown, ptr: Pointer | PathSegments | JsonPointer): boolean {
if (typeof ptr === 'string' || Array.isArray(ptr)) {
ptr = new JsonPointer(ptr);
}
return ptr.has(target);
return (ptr as JsonPointer).has(target);
}
/**

@@ -93,7 +146,7 @@ * Gets the value at the specified pointer's location in the object graph. If there is no value, then the result is `undefined`.

*/
static get<T, R>(target: T, ptr: string | string[] | JsonPointer): R {
static get(target: unknown, ptr: Pointer | PathSegments | JsonPointer): unknown {
if (typeof ptr === 'string' || Array.isArray(ptr)) {
ptr = new JsonPointer(ptr);
}
return ptr.get(target);
return (ptr as JsonPointer).get(target);
}

@@ -108,7 +161,7 @@

*/
static set<T, V, R>(target: T, ptr: string | string[] | JsonPointer, val: V, force?: boolean): R {
static set(target: unknown, ptr: Pointer | PathSegments | JsonPointer, val: unknown, force = false): unknown {
if (typeof ptr === 'string' || Array.isArray(ptr)) {
ptr = new JsonPointer(ptr);
}
return ptr.set(target, val, force);
return (ptr as JsonPointer).set(target, val, force);
}

@@ -120,3 +173,3 @@

*/
static decode(ptr: string): string[] {
static decode(ptr: Pointer): PathSegments {
return pickDecoder(ptr)(ptr);

@@ -131,3 +184,3 @@ }

*/
static visit<T>(target: T, visitor: Visitor, fragmentId?: boolean): void {
static visit(target: unknown, visitor: Visitor, fragmentId = false): void {
descendingVisit(target, visitor, fragmentId ? encodeUriFragmentIdentifier : encodePointer);

@@ -137,18 +190,13 @@ }

/**
* Evaluates the target's object graph, returning a [[PointerPair]] for each location in the graph.
* Evaluates the target's object graph, returning a [[JsonStringPointerListItem]] for each location in the graph.
* @param target the target of the operation
* @param fragmentId indicates whether the results are populated with fragment identifiers rather than regular pointers
*/
static list<T>(target: T, fragmentId?: boolean): PointerPair[] {
const res: PointerPair[] = [];
static listPointers(target: unknown): JsonStringPointerListItem[] {
const res: JsonStringPointerListItem[] = [];
descendingVisit(
target,
fragmentId
? (fragmentId, value): void => {
res.push({ fragmentId, value });
}
: (pointer, value): void => {
res.push({ pointer, value });
},
fragmentId ? encodeUriFragmentIdentifier : encodePointer
(pointer, value): void => {
res.push({ pointer, value });
},
encodePointer
);

@@ -159,8 +207,24 @@ return res;

/**
* Evaluates the target's object graph, returning a [[Dict]] populated with pointers and the corresponding values from the graph.
* Evaluates the target's object graph, returning a [[UriFragmentIdentifierPointerListItem]] for each location in the graph.
* @param target the target of the operation
*/
static listFragmentIds(target: unknown): UriFragmentIdentifierPointerListItem[] {
const res: UriFragmentIdentifierPointerListItem[] = [];
descendingVisit(
target,
(fragmentId, value): void => {
res.push({ fragmentId, value });
},
encodeUriFragmentIdentifier
);
return res;
}
/**
* Evaluates the target's object graph, returning a Record&lt;Pointer, unknown> populated with pointers and the corresponding values from the graph.
* @param target the target of the operation
* @param fragmentId indicates whether the results are populated with fragment identifiers rather than regular pointers
*/
static flatten<T>(target: T, fragmentId?: boolean): Dict {
const res: Dict = {};
static flatten(target: unknown, fragmentId = false): Record<Pointer, unknown> {
const res: Record<Pointer, unknown> = {};
descendingVisit(

@@ -177,10 +241,8 @@ target,

/**
* Evaluates the target's object graph, returning a Map&lt;string,any> populated with pointers and the corresponding values form the graph.
* Evaluates the target's object graph, returning a Map&lt;Pointer,unknown> populated with pointers and the corresponding values form the graph.
* @param target the target of the operation
* @param fragmentId indicates whether the results are populated with fragment identifiers rather than regular pointers
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static map<T>(target: T, fragmentId?: boolean): Map<string, any> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const res = new Map<string, any>();
static map(target: unknown, fragmentId = false): Map<Pointer, unknown> {
const res = new Map<Pointer, unknown>();
descendingVisit(target, res.set.bind(res), fragmentId ? encodeUriFragmentIdentifier : encodePointer);

@@ -193,3 +255,3 @@ return res;

*/
public path: string[];
public readonly path: PathSegments;

@@ -200,3 +262,3 @@ /**

*/
constructor(ptr: string | string[]) {
constructor(ptr: Pointer | PathSegments) {
this.path = decodePtrInit(ptr);

@@ -209,3 +271,3 @@ }

*/
get<T, V>(target: T): V {
get(target: unknown): unknown {
if (!this[$get]) {

@@ -225,4 +287,3 @@ this[$get] = compilePointerDereference(this.path);

*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
set<T, V>(target: T, value: V, force?: boolean): any {
set(target: unknown, value: unknown, force = false): unknown {
return setValueAtPath(target, value, this.path, force);

@@ -235,3 +296,3 @@ }

*/
has<T>(target: T): boolean {
has(target: unknown): boolean {
return typeof this.get(target) !== 'undefined';

@@ -244,3 +305,3 @@ }

*/
concat(ptr: JsonPointer | string | string[]): JsonPointer {
concat(ptr: JsonPointer | Pointer | PathSegments): JsonPointer {
return new JsonPointer(this.path.concat(ptr instanceof JsonPointer ? ptr.path : decodePtrInit(ptr)));

@@ -252,3 +313,3 @@ }

*/
get pointer(): string {
get pointer(): JsonStringPointer {
if (!this[$ptr]) {

@@ -263,3 +324,3 @@ this[$ptr] = encodePointer(this.path);

*/
get uriFragmentIdentifier(): string {
get uriFragmentIdentifier(): UriFragmentIdentifierPointer {
if (!this[$frg]) {

@@ -270,13 +331,13 @@ this[$frg] = encodeUriFragmentIdentifier(this.path);

}
/**
* Produces this pointer's JSON Pointer encoded string representation.
*/
toString(): string {
return this.pointer;
}
}
/**
* Produces this pointer's JSON Pointer encoded string representation.
*/
JsonPointer.prototype.toString = function toString(): string {
return this.pointer;
};
export class JsonReference {
static isReference<T>(candidate: T): boolean {
static isReference(candidate: unknown): candidate is JsonReference {
if (!candidate) return false;

@@ -288,5 +349,5 @@ const ref = (candidate as unknown) as JsonReference;

public pointer: JsonPointer;
public $ref: string;
public $ref: UriFragmentIdentifierPointer;
constructor(ptr: JsonPointer | string | string[]) {
constructor(ptr: JsonPointer | Pointer | PathSegments) {
this.pointer = ptr instanceof JsonPointer ? ptr : new JsonPointer(ptr);

@@ -296,80 +357,9 @@ this.$ref = this.pointer.uriFragmentIdentifier;

resolve<T, R>(target: T): R {
resolve(target: unknown): unknown {
return this.pointer.get(target);
}
}
JsonReference.prototype.toString = function toString(): string {
return this.$ref;
};
interface Item {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
obj: any;
path: string[];
toString(): string {
return this.$ref;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
descendingVisit = (target: any, visitor: Visitor, encoder: Encoder, cycle: boolean): void => {
let distinctObjects;
const q: Item[] = [];
let cursor2 = 0;
q.push({
obj: target,
path: [],
});
if (cycle) {
distinctObjects = Object.create(null);
}
visitor(encoder([]), target);
while (cursor2 < q.length) {
const cursor = q[cursor2++];
const typeT = typeof cursor.obj;
if (typeT === 'object' && cursor.obj !== null) {
if (Array.isArray(cursor.obj)) {
let j = -1;
const len2 = cursor.obj.length;
while (++j < len2) {
const it = cursor.obj[j];
const path = cursor.path.concat([j + '']);
if (typeof it === 'object' && it !== null) {
if (cycle && distinctObjects[it]) {
visitor(encoder(path), new JsonReference(distinctObjects[it]));
continue;
}
q.push({
obj: it,
path: path,
});
if (cycle) {
distinctObjects[it] = new JsonPointer(encodeUriFragmentIdentifier(path));
}
}
visitor(encoder(path), it);
}
} else {
const keys = Object.keys(cursor.obj);
const len3 = keys.length;
let i = -1;
while (++i < len3) {
const it = cursor.obj[keys[i]];
const path = cursor.path.concat(keys[i]);
if (typeof it === 'object' && it !== null) {
if (cycle && distinctObjects[it]) {
visitor(encoder(path), new JsonReference(distinctObjects[it]));
continue;
}
q.push({
obj: it,
path: path,
});
if (cycle) {
distinctObjects[it] = new JsonPointer(encodeUriFragmentIdentifier(path));
}
}
visitor(encoder(path), it);
}
}
}
}
};

@@ -0,1 +1,3 @@

import { JsonStringPointer, UriFragmentIdentifierPointer, Pointer, PathSegment, PathSegments, Decoder } from './types';
export function replace(source: string, find: string, repl: string): string {

@@ -17,6 +19,3 @@ let res = '';

export type Decoder = (ptr: string) => string[];
export type Encoder = (ptr: string[]) => string;
export function decodeFragmentSegments(segments: string[]): string[] {
export function decodeFragmentSegments(segments: PathSegments): PathSegments {
let i = -1;

@@ -31,3 +30,3 @@ const len = segments.length;

export function encodeFragmentSegments(segments: string[]): string[] {
export function encodeFragmentSegments(segments: PathSegments): PathSegments {
let i = -1;

@@ -46,3 +45,3 @@ const len = segments.length;

export function decodePointerSegments(segments: string[]): string[] {
export function decodePointerSegments(segments: PathSegments): PathSegments {
let i = -1;

@@ -57,3 +56,3 @@ const len = segments.length;

export function encodePointerSegments(segments: string[]): string[] {
export function encodePointerSegments(segments: PathSegments): PathSegments {
let i = -1;

@@ -72,3 +71,3 @@ const len = segments.length;

export function decodePointer(ptr: string): string[] {
export function decodePointer(ptr: Pointer): PathSegments {
if (typeof ptr !== 'string') {

@@ -86,3 +85,3 @@ throw new TypeError('Invalid type: JSON Pointers are represented as strings.');

export function encodePointer(path: string[]): string {
export function encodePointer(path: PathSegments): JsonStringPointer {
if (path && !Array.isArray(path)) {

@@ -97,3 +96,3 @@ throw new TypeError('Invalid type: path must be an array of segments.');

export function decodeUriFragmentIdentifier(ptr: string): string[] {
export function decodeUriFragmentIdentifier(ptr: UriFragmentIdentifierPointer): PathSegments {
if (typeof ptr !== 'string') {

@@ -114,3 +113,3 @@ throw new TypeError('Invalid type: JSON Pointers are represented as strings.');

export function encodeUriFragmentIdentifier(path: string[]): string {
export function encodeUriFragmentIdentifier(path: PathSegments): UriFragmentIdentifierPointer {
if (path && !Array.isArray(path)) {

@@ -125,3 +124,3 @@ throw new TypeError('Invalid type: path must be an array of segments.');

export function toArrayIndexReference(arr: string[], idx: string): number {
export function toArrayIndexReference(arr: readonly unknown[], idx: string): number {
const len = idx.length;

@@ -146,9 +145,9 @@ let cursor = 0;

export function hasValueAtPath<T>(target: T, path: string[]): boolean {
let it;
let len;
let cursor;
export function hasValueAtPath(target: unknown, path: PathSegments): boolean {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let step: any;
let p;
let it: any;
let len: number;
let cursor: number;
let step: PathSegment;
let p: number;
if (typeof target !== 'undefined') {

@@ -164,3 +163,4 @@ it = target;

if (isNaN(n) || !isFinite(n)) {
it = it[step];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
it = (it as any)[step];
continue;

@@ -184,10 +184,8 @@ }

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getValueAtPath<T>(target: T, path: string[]): any {
export function getValueAtPath(target: unknown, path: PathSegments): unknown {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let it: any;
let len;
let cursor;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let step: any;
let len: number;
let cursor: number;
let step: PathSegment;
let p;

@@ -204,3 +202,4 @@ if (typeof target !== 'undefined') {

if (isNaN(n) || !isFinite(n)) {
it = it[step];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
it = (it as any)[step];
continue;

@@ -224,12 +223,10 @@ }

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Dereference = <T, V>(it: T) => V;
export type Dereference = (it: unknown) => unknown;
export function compilePointerDereference(path: string[]): Dereference {
export function compilePointerDereference(path: PathSegments): Dereference {
let body = "if (typeof(it) !== 'undefined'";
if (path.length === 0) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (it: any): any => it;
return (it): unknown => it;
}
body = path.reduce((body, p, i) => {
body = path.reduce((body, _, i) => {
return body + " && \n\ttypeof((it = it['" + replace(path[i], '\\', '\\\\') + "'])) !== 'undefined'";

@@ -242,4 +239,3 @@ }, "if (typeof(it) !== 'undefined'");

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function setValueAtPath<V>(target: any, val: V, path: string[], force: boolean): any {
export function setValueAtPath(target: unknown, val: unknown, path: PathSegments, force = false): unknown {
if (path.length === 0) {

@@ -251,8 +247,9 @@ throw new Error('Cannot set the root object; assign it directly.');

}
let it = target;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let it: any = target;
const len = path.length;
const end = path.length - 1;
let step: string;
let step: PathSegment;
let cursor = -1;
let rem;
let rem: unknown;
let p: number;

@@ -308,12 +305,12 @@ if (len) {

export function looksLikeFragment(ptr: string): boolean {
return ptr && ptr.length && ptr[0] === '#';
export function looksLikeFragment(ptr: Pointer): boolean {
return ptr?.length > 0 && ptr[0] === '#';
}
export function pickDecoder(ptr: string): Decoder {
export function pickDecoder(ptr: Pointer): Decoder {
return looksLikeFragment(ptr) ? decodeUriFragmentIdentifier : decodePointer;
}
export function decodePtrInit(ptr: string | string[]): string[] {
return Array.isArray(ptr) ? ptr.slice(0) : pickDecoder(ptr)(ptr);
export function decodePtrInit(ptr: Pointer | PathSegments): PathSegments {
return Array.isArray(ptr) ? ptr.slice(0) : pickDecoder(ptr as Pointer)(ptr as Pointer);
}

@@ -5,3 +5,3 @@ {

"target": "es2017",
"noImplicitAny": true,
"strict": true,
"moduleResolution": "node",

@@ -8,0 +8,0 @@ "declaration": true,

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc