Skip to content

Commit d0b64ed

Browse files
raing3Richard Ainger
and
Richard Ainger
authored
fix: added support for merging items into an existing array (#27)
* fix: added support for merging items into an existing array * chore: added additional tests for single level array merging/replacing/inserting --------- Co-authored-by: Richard Ainger <[email protected]>
1 parent 8ddfa5d commit d0b64ed

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

lib/set.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function addToArray(
6767
getType(target[index]) === "object" &&
6868
getType(value) === "object"
6969
) {
70+
Object.assign(target[index], value);
7071
return [target[index], index, target, `${result[3]}/${index}}`];
7172
}
7273

@@ -118,7 +119,10 @@ function create<T extends WorkingSet>(
118119
.map((r: QueryResult) => {
119120
const container = keyIsArray ? [] : {};
120121
const o = r[0];
121-
if (Array.isArray(o)) {
122+
const containerType = getType(container);
123+
const itemType = getType(o[query]);
124+
125+
if (Array.isArray(o) && itemType !== containerType) {
122126
return addToArray(r, query, container, force);
123127
}
124128
o[query] = o[query] || container;

test/unit/set.test.ts

+60-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint object-property-newline: 0 */
22
import "mocha";
3-
import { expect } from "chai";
4-
import { set, InsertMode } from "../../lib/set";
3+
import {expect} from "chai";
4+
import {InsertMode, set} from "../../lib/set";
55

66
/*
77
??? Syntax
@@ -156,6 +156,64 @@ describe("set", () => {
156156
);
157157
expect(result).to.deep.eq({ list: [1, { value: "t" }, 3] });
158158
});
159+
160+
it("should merge into object nested in single-level array", () => {
161+
const result = set(
162+
[{ text: 'A' }, { text: 'B' }],
163+
"#/1",
164+
{ additionalText: 'C' }
165+
);
166+
expect(result).to.deep.eq([{ text: 'A' }, { text: 'B', additionalText: 'C' } ]);
167+
});
168+
169+
it("should replace into object nested in single-level array", () => {
170+
const result = set(
171+
[{ text: 'A' }, { text: 'B' }],
172+
"#/1",
173+
{ additionalText: 'C' },
174+
InsertMode.REPLACE_ITEMS
175+
);
176+
expect(result).to.deep.eq([{ text: 'A' }, { additionalText: 'C' } ])
177+
});
178+
179+
it("should insert into object nested in single-level array", () => {
180+
const result = set(
181+
[{ text: 'A' }, { text: 'B' }],
182+
"#/1",
183+
{ additionalText: 'C' },
184+
InsertMode.INSERT_ITEMS
185+
);
186+
expect(result).to.deep.eq([{ text: 'A' }, { additionalText: 'C' }, { text: 'B' } ])
187+
});
188+
189+
it("should merge into object nested in multi-level array", () => {
190+
const result = set(
191+
[[{ text: 'A' }, { text: 'B' } ]],
192+
"#/0/1",
193+
{ additionalText: 'C' }
194+
);
195+
expect(result).to.deep.eq([[{ text: 'A' }, { text: 'B', additionalText: 'C' } ]]);
196+
});
197+
198+
it("should replace into object nested in multi-level array", () => {
199+
const result = set(
200+
[[{ text: 'A' }, { text: 'B' } ]],
201+
"#/0/1",
202+
{ additionalText: 'C' },
203+
InsertMode.REPLACE_ITEMS
204+
);
205+
expect(result).to.deep.eq([[{ text: 'A' }, { additionalText: 'C' } ]]);
206+
});
207+
208+
it("should insert into object nested in multi-level array", () => {
209+
const result = set(
210+
[[{ text: 'A' }, { text: 'B' } ]],
211+
"#/0/1",
212+
{ additionalText: 'C' },
213+
InsertMode.INSERT_ITEMS
214+
);
215+
expect(result).to.deep.eq([[{ text: 'A' }, { additionalText: 'C' }, { text: 'B' } ]]);
216+
});
159217
});
160218

161219
describe("queries", () => {

0 commit comments

Comments
 (0)