Skip to content
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

fix(editor): Drop mergeDeep in favor of lodash merge #5943

Merged
merged 1 commit into from
Apr 11, 2023
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
5 changes: 3 additions & 2 deletions packages/editor-ui/src/components/RunDataSchema.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts" setup>
import { computed, ref } from 'vue';
import { merge } from 'lodash-es';
import { INodeUi, Schema } from '@/Interface';
import RunDataSchemaItem from '@/components/RunDataSchemaItem.vue';
import Draggable from '@/components/Draggable.vue';
Expand All @@ -8,7 +9,7 @@ import { useWebhooksStore } from '@/stores/webhooks';
import { runExternalHook } from '@/mixins/externalHooks';
import { telemetry } from '@/plugins/telemetry';
import { IDataObject } from 'n8n-workflow';
import { getSchema, isEmpty, mergeDeep } from '@/utils';
import { getSchema, isEmpty } from '@/utils';
import { i18n } from '@/plugins/i18n';
import MappingPill from './MappingPill.vue';

Expand All @@ -32,7 +33,7 @@ const webhooksStore = useWebhooksStore();

const schema = computed<Schema>(() => {
const [head, ...tail] = props.data;
return getSchema(mergeDeep([head, ...tail, head]));
return getSchema(merge({}, head, ...tail, head));
});

const isDataEmpty = computed(() => {
Expand Down
176 changes: 1 addition & 175 deletions packages/editor-ui/src/utils/__tests__/typesUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jp from 'jsonpath';
import { isEmpty, intersection, mergeDeep, getSchema, isValidDate } from '@/utils';
import { isEmpty, intersection, getSchema, isValidDate } from '@/utils';
import { Schema } from '@/Interface';

describe('Types Utils', () => {
Expand Down Expand Up @@ -43,180 +43,6 @@ describe('Types Utils', () => {
});
});

describe('mergeDeep', () => {
test.each([
[
[
[1, 2],
[3, 4],
],
{},
[3, 4],
],
[
[
[1, 2],
[3, 4],
],
{ concatArrays: true },
[1, 2, 3, 4],
],
[
[
[1, 2],
[3, 4],
],
{ overwriteArrays: true },
[3, 4],
],
[
[
[1, 2, 3],
[4, 5],
],
{},
[4, 5, 3],
],
[
[
[1, 2, 3],
[4, 5],
],
{ concatArrays: true },
[1, 2, 3, 4, 5],
],
[
[
[1, 2, 3],
[4, 5],
],
{ overwriteArrays: true },
[4, 5],
],
[
[
[1, 2],
[3, 4, 5],
],
{},
[3, 4, 5],
],
[
[
[1, 2],
[3, 4, 5],
],
{ concatArrays: true },
[1, 2, 3, 4, 5],
],
[
[
[1, 2],
[3, 4, 5],
],
{ overwriteArrays: true },
[3, 4, 5],
],
[[{ a: 1, b: [1, 2, { d: 2 }] }, {}], {}, { a: 1, b: [1, 2, { d: 2 }] }],
[[{ a: 1, b: [1, 2, { d: 2 }] }, {}], { concatArrays: true }, { a: 1, b: [1, 2, { d: 2 }] }],
[
[{ a: 1, b: [1, 2, { d: 2 }] }, {}],
{ overwriteArrays: true },
{ a: 1, b: [1, 2, { d: 2 }] },
],
[[[{ a: 1, b: [1, 2, { d: 2 }] }], []], {}, [{ a: 1, b: [1, 2, { d: 2 }] }]],
[
[[{ a: 1, b: [1, 2, { d: 2 }] }], []],
{ concatArrays: true },
[{ a: 1, b: [1, 2, { d: 2 }] }],
],
[[[{ a: 1, b: [1, 2, { d: 2 }] }], []], { overwriteArrays: true }, []],
[
[
{ a: 1, b: [1, 2, 3] },
{ a: 2, b: [4, 5, 6, 7], c: '2' },
{ a: 3, b: [8, 9], d: '3' },
],
{},
{ a: 3, b: [8, 9, 6, 7], c: '2', d: '3' },
],
[
[
{ a: 1, b: [1, 2, 3] },
{ a: 2, b: [4, 5, 6, 7], c: '2' },
{ a: 3, b: [8, 9], d: '3' },
],
{ concatArrays: true },
{ a: 3, b: [1, 2, 3, 4, 5, 6, 7, 8, 9], c: '2', d: '3' },
],
[
[
{ a: 1, b: [1, 2, 3] },
{ a: 2, b: [4, 5, 6, 7], c: '2' },
{ a: 3, b: [8, 9], d: '3' },
],
{ overwriteArrays: true },
{ a: 3, b: [8, 9], c: '2', d: '3' },
],
[
[
{ a: 1, b: [{ x: 'a' }] },
{ a: 2, b: [{ y: 'b' }], c: '2' },
{ a: 3, b: [{ z: 'c' }], d: '3' },
],
{},
{ a: 3, b: [{ x: 'a', y: 'b', z: 'c' }], c: '2', d: '3' },
],
[
[
{ a: 1, b: [{ x: 'a' }] },
{ a: 2, b: [{ y: 'b' }], c: '2' },
{ a: 3, b: [{ z: 'c' }], d: '3' },
],
{ concatArrays: true },
{ a: 3, b: [{ x: 'a' }, { y: 'b' }, { z: 'c' }], c: '2', d: '3' },
],
[
[
{ a: 1, b: [{ x: 'a' }] },
{ a: 2, b: [{ y: 'b' }], c: '2' },
{ a: 3, b: [{ z: 'c' }], d: '3' },
],
{ overwriteArrays: true },
{ a: 3, b: [{ z: 'c' }], c: '2', d: '3' },
],
[
[
{ a: 1, b: [{ x: 'a' }, { w: 'd' }] },
{ a: 2, b: [{ y: 'b' }], c: '2' },
{ a: 3, b: [{ z: 'c' }], d: '3' },
],
{},
{ a: 3, b: [{ z: 'c' }, { w: 'd' }], c: '2', d: '3' },
],
[
[
{ a: 1, b: [{ x: 'a' }, { w: 'd' }] },
{ a: 2, b: [{ y: 'b' }], c: '2' },
{ a: 3, b: [{ z: 'c' }], d: '3' },
],
{ concatArrays: true },
{ a: 3, b: [{ x: 'a' }, { w: 'd' }, { y: 'b' }, { z: 'c' }], c: '2', d: '3' },
],
[
[
{ a: 1, b: [{ x: 'a' }, { w: 'd' }] },
{ a: 2, b: [{ y: 'b' }], c: '2' },
{ a: 3, b: [{ z: 'c' }], d: '3' },
],
{ overwriteArrays: true },
{ a: 3, b: [{ z: 'c' }], c: '2', d: '3' },
],
])('case %#. input %j, options %j should return %j', (sources, options, expected) => {
expect(mergeDeep([...sources], options)).toEqual(expected);
});
});

describe('getSchema', () => {
test.each([
[, { type: 'undefined', value: 'undefined', path: '' }],
Expand Down
48 changes: 0 additions & 48 deletions packages/editor-ui/src/utils/typesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,54 +164,6 @@ export const isValidDate = (input: string | number | Date): boolean => {
export const getObjectKeys = <T extends object, K extends keyof T>(o: T): K[] =>
Object.keys(o) as K[];

export const mergeDeep = <T extends object | Primitives>(
sources: T[],
options?: Partial<Record<'overwriteArrays' | 'concatArrays', boolean>>,
): T =>
sources.reduce((target, source) => {
if (Array.isArray(target) && Array.isArray(source)) {
const tLength = target.length;
const sLength = source.length;

if (tLength === 0 || options?.overwriteArrays) {
return source;
}

if (sLength === 0) {
return target;
}

if (options?.concatArrays) {
return [...target, ...source];
}

if (tLength === sLength) {
return target.map((item, index) => mergeDeep([item, source[index]], options));
} else if (tLength < sLength) {
return source.map((item, index) => mergeDeep([target[index], item], options));
} else {
return [...source, ...target.slice(sLength)];
}
} else if (isObj(target) && isObj(source)) {
const targetKeys = getObjectKeys(target);
const sourceKeys = getObjectKeys(source);
const allKeys = [...new Set([...targetKeys, ...sourceKeys])];
const mergedObject = Object.create(Object.prototype);
for (const key of allKeys) {
if (targetKeys.includes(key) && sourceKeys.includes(key)) {
mergedObject[key] = mergeDeep([target[key] as T, source[key] as T], options);
} else if (targetKeys.includes(key)) {
mergedObject[key] = target[key];
} else {
mergedObject[key] = source[key];
}
}
return mergedObject;
} else {
return source;
}
}, (Array.isArray(sources[0]) ? [] : {}) as T);

export const getSchema = (input: Optional<Primitives | object>, path = ''): Schema => {
let schema: Schema = { type: 'undefined', value: 'undefined', path };
switch (typeof input) {
Expand Down