Skip to content
Closed
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 (pointer.search(/~(?![01])/) !== -1) {
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[]][] */

Check failure on line 7 in lib/pointerSegments.test.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 4 spaces but found 8
const tests = [

Check failure on line 8 in lib/pointerSegments.test.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 4 spaces but found 8
["", []],

Check failure on line 9 in lib/pointerSegments.test.js

View workflow job for this annotation

GitHub Actions / build

Multiple spaces found before '['

Check failure on line 9 in lib/pointerSegments.test.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 6 spaces but found 12
["/", [""]],

Check failure on line 10 in lib/pointerSegments.test.js

View workflow job for this annotation

GitHub Actions / build

Multiple spaces found before '['

Check failure on line 10 in lib/pointerSegments.test.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 6 spaces but found 12
["/foo", ["foo"]],

Check failure on line 11 in lib/pointerSegments.test.js

View workflow job for this annotation

GitHub Actions / build

Multiple spaces found before '['

Check failure on line 11 in lib/pointerSegments.test.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 6 spaces but found 12
["/foo/bar", ["foo", "bar"]],

Check failure on line 12 in lib/pointerSegments.test.js

View workflow job for this annotation

GitHub Actions / build

Multiple spaces found before '['

Check failure on line 12 in lib/pointerSegments.test.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 6 spaces but found 12
["/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");
});
});
});
});