Skip to content

Commit 5373840

Browse files
committed
fix: issue#24 do not modify input path-array
1 parent 1943404 commit 5373840

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

lib/split.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function sanitizeAndDecodeProperty(property: string): string {
1919
*/
2020
export function split(pointer: JsonPointer | JsonPath): JsonPath {
2121
if (pointer == null || typeof pointer !== "string" || isRoot(pointer)) {
22-
return Array.isArray(pointer) ? pointer : [];
22+
return Array.isArray(pointer) ? pointer.slice() : [];
2323
}
2424
const sanitize =
2525
pointer.indexOf("#") >= 0

test/unit/get.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ describe("pointer.get", () => {
5353
assert.equal(result, "propertyValue");
5454
});
5555

56+
it("should not modify input path-array", () => {
57+
const path = ["property", "value"];
58+
const result = get({ property: { value: "propertyValue" } }, path);
59+
assert.equal(result, "propertyValue");
60+
61+
assert.deepEqual(path, ["property", "value"]);
62+
});
63+
5664
it("should return 'defaultValue' if property does not exist", () => {
5765
const result = get(
5866
{ property: { value: "propertyValue" } },

test/unit/remove.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ describe("remove", () => {
3939
assert(isObject(result.a.c));
4040
});
4141

42+
it("should not modify input path-array", () => {
43+
const path = ["a", "b"];
44+
const result = remove({ a: { b: {}, c: {} } }, path);
45+
46+
assert.deepEqual(path, ["a", "b"]);
47+
});
48+
4249
it("should ignore invalid paths", () => {
4350
const result = remove({ a: { b: {}, c: {} } }, "/a/d/c");
4451

test/unit/set.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ describe("pointer.set", () => {
179179
assert.equal(result.array[1].valid, true)
180180
});
181181

182+
it("should not modify input path-array", () => {
183+
const path = ["array", "[1]", "valid"];
184+
const result = set<{ array?: any[] }>(
185+
{},
186+
path,
187+
true
188+
);
189+
190+
assert.deepEqual(path, ["array", "[1]", "valid"]);
191+
});
192+
182193
describe("# (uri fragment)", () => {
183194
it("should add value on the given path", () => {
184195
const result = set<{ path?: { to?: { property?: boolean } } }>(

test/unit/split.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ describe("pointer.split", () => {
4141
assert.deepEqual(result, ["a", "b"]);
4242
});
4343

44+
it("should return copy of input array", () => {
45+
const path = ["a", "b"];
46+
const result = split(["a", "b"]);
47+
48+
assert.deepEqual(result, path);
49+
assert.notEqual(result, path);
50+
});
51+
4452
it("should return path properties in array", () => {
4553
const result = split("/my/path");
4654

0 commit comments

Comments
 (0)