Skip to content

Commit 98c508a

Browse files
authored
Add support for Array during object merging (#60)
* Add support for Array during object merging * Bump version * Remove test.only * Clone object before merging it
1 parent db5275c commit 98c508a

File tree

11 files changed

+52
-18
lines changed

11 files changed

+52
-18
lines changed

assert/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cgauge/assert",
3-
"version": "0.17.2",
3+
"version": "0.17.3",
44
"description": "Extra assert library",
55
"type": "module",
66
"repository": {

dtc-aws-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cgauge/dtc-aws-plugin",
3-
"version": "0.17.2",
3+
"version": "0.17.3",
44
"description": "AWS plugin for Declarative TestCases",
55
"repository": {
66
"type": "git",

dtc-graphql-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cgauge/dtc-graphql-plugin",
3-
"version": "0.17.2",
3+
"version": "0.17.3",
44
"description": "GraphQl plugin for Declarative TestCases",
55
"repository": {
66
"type": "git",

dtc-mysql-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cgauge/dtc-mysql-plugin",
3-
"version": "0.17.2",
3+
"version": "0.17.3",
44
"description": "MySQL plugin for Declarative TestCases",
55
"repository": {
66
"type": "git",

dtc-playwright-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cgauge/dtc-playwright-plugin",
3-
"version": "0.17.2",
3+
"version": "0.17.3",
44
"description": "Playwright plugin for Declarative TestCases",
55
"repository": {
66
"type": "git",

dtc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cgauge/dtc",
3-
"version": "0.17.2",
3+
"version": "0.17.3",
44
"description": "Declarative TestCases",
55
"repository": {
66
"type": "git",

dtc/src/utils.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ export const retry = async <T>(fn: () => Promise<T>, times = 0, seconds = 1): Pr
3535
throw errors[0]
3636
}
3737

38-
export const merge = (to: any, from: any) => {
39-
to = {...to}
40-
for (const n in from) {
41-
if (typeof to[n] != 'object') {
42-
to[n] = from[n]
43-
} else if (typeof from[n] == 'object') {
44-
to[n] = merge(to[n], from[n])
38+
export const merge = (to: any, from: any): any => {
39+
if (Array.isArray(to) && Array.isArray(from)) {
40+
return to.length > from.length
41+
? to.map((v, i) => merge(v, from[i] ?? v))
42+
: from.map((v: any, i: number) => merge(to[i], v))
43+
} else if (typeof to === 'object' && !Array.isArray(to) && typeof from === 'object' && !Array.isArray(from)) {
44+
to = structuredClone(to)
45+
for (const key in from) {
46+
to[key] = merge(to[key], from[key])
4547
}
48+
return to
4649
}
47-
return to
50+
51+
return from
4852
}

dtc/test/utils.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {test} from 'node:test'
22
import assert from 'node:assert'
3-
import {retry} from '../src/utils'
3+
import {retry, merge} from '../src/utils'
44

55
test('It execute at least one time', async () => {
66
const response = await retry(() => Promise.resolve(true))
@@ -25,4 +25,34 @@ test('It retries when it fails', async () => {
2525
}, 1)
2626

2727
assert.ok(response)
28+
});
29+
30+
([
31+
//objects
32+
[{a: 'a'}, {a: 'b'}, {a: 'b'}],
33+
[{a: 'a', b: 'c'}, {a: 'b'}, {a: 'b', b: 'c'}],
34+
[{a: 'a'}, {a: 'b', b: 'c'}, {a: 'b', b: 'c'}],
35+
[{a: {a: 'a'}}, {a: {a: 'b'}}, {a: {a: 'b'}}],
36+
[{a: {a: 'a'}, b: 'c'}, {a: {a: 'b'}}, {a: {a: 'b'}, b: 'c'}],
37+
//arrays
38+
[[1], [2], [2]],
39+
[[1], [2,3], [2,3]],
40+
[[1,2], [3], [3,2]],
41+
[[[1]], [[2]], [[2]]],
42+
[[[1], 2], [[2]], [[2], 2]],
43+
[[[1]], [[2], 3], [[2], 3]],
44+
//mixed types
45+
[{a: [1]}, {a: 1}, {a: 1}],
46+
[{a: 1}, {a: [1]}, {a: [1]}],
47+
[{a: {b: 'a'}}, {a: [1]}, {a: [1]}],
48+
[[{a: 'a', b: 'c'}], [{a: 'b'}], [{a: 'b', b: 'c'}]],
49+
//primitives
50+
[1, 2, 2],
51+
['a', 'b', 'b'],
52+
]).forEach(([source, target, expected], index) => {
53+
test(`It merge objects (${index})`, async () => {
54+
const result = merge(source, target)
55+
56+
assert.deepStrictEqual(result, expected)
57+
})
2858
})

nock-aws/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cgauge/nock-aws",
3-
"version": "0.17.2",
3+
"version": "0.17.3",
44
"description": "AWS Request mocker based on Nock",
55
"repository": {
66
"type": "git",

type-guard/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cgauge/type-guard",
3-
"version": "0.17.2",
3+
"version": "0.17.3",
44
"description": "Typescript type guards",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)