Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export const pointerSegments = function* (pointer) {
throw Error("Invalid JSON Pointer");
}

if (/~(?![01])/.test(pointer)) {
throw Error("Invalid JSON Pointer");
}

let segmentStart = 1;
let segmentEnd = 0;

Expand Down
60 changes: 60 additions & 0 deletions lib/pointerSegments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, expect, test } from "vitest";
import { pointerSegments } from "./index.js";


describe("JsonPointer", () => {
describe("pointerSegments", () => {
/** @type [string, string[]][] */
const tests = [
["", []],
["/", [""]],
["/foo", ["foo"]],
["/foo/bar", ["foo", "bar"]],
["/foo/0", ["foo", "0"]],
["/a~1b", ["a/b"]],
["/m~0n", ["m~n"]],
["/~00", ["~0"]],
["/~01", ["~1"]],
["/~10", ["/0"]],
["/~11", ["/1"]],
["/~01~10", ["~1/0"]],
["/~00/~11", ["~0", "/1"]],
["/ ", [" "]],
["/c%d", ["c%d"]],
["/e^f", ["e^f"]],
["/g|h", ["g|h"]],
["/i\\j", ["i\\j"]],
["/k\"l", ["k\"l"]]
];

tests.forEach(([pointer, expected]) => {
test(`${JSON.stringify(pointer)} => ${JSON.stringify(expected)}`, () => {
expect([...pointerSegments(pointer)]).to.eql(expected);
});
});
});

describe("a pointer that doesn't start with '/'", () => {
test("should throw an error", () => {
expect(() => [...pointerSegments("foo")]).to.throw(Error, "Invalid JSON Pointer");
});
});

describe("a pointer with an invalid escape sequence", () => {
/** @type string[] */
const tests = [
"/~",
"/~2",
"/~a",
"/a~",
"/~~",
"/~0~"
];

tests.forEach((pointer) => {
test(`${JSON.stringify(pointer)} should throw an error`, () => {
expect(() => [...pointerSegments(pointer)]).to.throw(Error, "Invalid JSON Pointer");
});
});
});
});