Skip to content

fix: added support for merging items into an existing array #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2024
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
6 changes: 5 additions & 1 deletion lib/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function addToArray(
getType(target[index]) === "object" &&
getType(value) === "object"
) {
Object.assign(target[index], value);
return [target[index], index, target, `${result[3]}/${index}}`];
}

Expand Down Expand Up @@ -118,7 +119,10 @@ function create<T extends WorkingSet>(
.map((r: QueryResult) => {
const container = keyIsArray ? [] : {};
const o = r[0];
if (Array.isArray(o)) {
const containerType = getType(container);
const itemType = getType(o[query]);

if (Array.isArray(o) && itemType !== containerType) {
return addToArray(r, query, container, force);
}
o[query] = o[query] || container;
Expand Down
62 changes: 60 additions & 2 deletions test/unit/set.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint object-property-newline: 0 */
import "mocha";
import { expect } from "chai";
import { set, InsertMode } from "../../lib/set";
import {expect} from "chai";
import {InsertMode, set} from "../../lib/set";

/*
??? Syntax
Expand Down Expand Up @@ -156,6 +156,64 @@ describe("set", () => {
);
expect(result).to.deep.eq({ list: [1, { value: "t" }, 3] });
});

it("should merge into object nested in single-level array", () => {
const result = set(
[{ text: 'A' }, { text: 'B' }],
"#/1",
{ additionalText: 'C' }
);
expect(result).to.deep.eq([{ text: 'A' }, { text: 'B', additionalText: 'C' } ]);
});

it("should replace into object nested in single-level array", () => {
const result = set(
[{ text: 'A' }, { text: 'B' }],
"#/1",
{ additionalText: 'C' },
InsertMode.REPLACE_ITEMS
);
expect(result).to.deep.eq([{ text: 'A' }, { additionalText: 'C' } ])
});

it("should insert into object nested in single-level array", () => {
const result = set(
[{ text: 'A' }, { text: 'B' }],
"#/1",
{ additionalText: 'C' },
InsertMode.INSERT_ITEMS
);
expect(result).to.deep.eq([{ text: 'A' }, { additionalText: 'C' }, { text: 'B' } ])
});

it("should merge into object nested in multi-level array", () => {
const result = set(
[[{ text: 'A' }, { text: 'B' } ]],
"#/0/1",
{ additionalText: 'C' }
);
expect(result).to.deep.eq([[{ text: 'A' }, { text: 'B', additionalText: 'C' } ]]);
});

it("should replace into object nested in multi-level array", () => {
const result = set(
[[{ text: 'A' }, { text: 'B' } ]],
"#/0/1",
{ additionalText: 'C' },
InsertMode.REPLACE_ITEMS
);
expect(result).to.deep.eq([[{ text: 'A' }, { additionalText: 'C' } ]]);
});

it("should insert into object nested in multi-level array", () => {
const result = set(
[[{ text: 'A' }, { text: 'B' } ]],
"#/0/1",
{ additionalText: 'C' },
InsertMode.INSERT_ITEMS
);
expect(result).to.deep.eq([[{ text: 'A' }, { additionalText: 'C' }, { text: 'B' } ]]);
});
});

describe("queries", () => {
Expand Down