mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-21 02:56:40 -08:00
fix(editor): Rename and move isObjectLiteral
utility function (no-changelog) (#7565)
This commit is contained in:
parent
c6b688387c
commit
b94b8b2eb0
|
@ -34,7 +34,7 @@ import type {
|
||||||
|
|
||||||
import { get } from 'lodash-es';
|
import { get } from 'lodash-es';
|
||||||
|
|
||||||
import { isObjectLiteral } from '@/utils';
|
import { isObject } from '@/utils';
|
||||||
import { getCredentialPermissions } from '@/permissions';
|
import { getCredentialPermissions } from '@/permissions';
|
||||||
import { mapStores } from 'pinia';
|
import { mapStores } from 'pinia';
|
||||||
import { useSettingsStore } from '@/stores/settings.store';
|
import { useSettingsStore } from '@/stores/settings.store';
|
||||||
|
@ -65,7 +65,7 @@ export const nodeHelpers = defineComponent({
|
||||||
isCustomApiCallSelected(nodeValues: INodeParameters): boolean {
|
isCustomApiCallSelected(nodeValues: INodeParameters): boolean {
|
||||||
const { parameters } = nodeValues;
|
const { parameters } = nodeValues;
|
||||||
|
|
||||||
if (!isObjectLiteral(parameters)) return false;
|
if (!isObject(parameters)) return false;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
(parameters.resource !== undefined && parameters.resource.includes(CUSTOM_API_CALL_KEY)) ||
|
(parameters.resource !== undefined && parameters.resource.includes(CUSTOM_API_CALL_KEY)) ||
|
||||||
|
|
|
@ -77,7 +77,7 @@ import {
|
||||||
isJsonKeyObject,
|
isJsonKeyObject,
|
||||||
getPairedItemsMapping,
|
getPairedItemsMapping,
|
||||||
stringSizeInBytes,
|
stringSizeInBytes,
|
||||||
isObjectLiteral,
|
isObject,
|
||||||
isEmpty,
|
isEmpty,
|
||||||
makeRestApiRequest,
|
makeRestApiRequest,
|
||||||
unflattenExecutionData,
|
unflattenExecutionData,
|
||||||
|
@ -1065,7 +1065,7 @@ export const useWorkflowsStore = defineStore(STORES.WORKFLOWS, {
|
||||||
const uiStore = useUIStore();
|
const uiStore = useUIStore();
|
||||||
uiStore.stateIsDirty = true;
|
uiStore.stateIsDirty = true;
|
||||||
const newParameters =
|
const newParameters =
|
||||||
!!append && isObjectLiteral(updateInformation.value)
|
!!append && isObject(updateInformation.value)
|
||||||
? { ...node.parameters, ...updateInformation.value }
|
? { ...node.parameters, ...updateInformation.value }
|
||||||
: updateInformation.value;
|
: updateInformation.value;
|
||||||
|
|
||||||
|
|
98
packages/editor-ui/src/utils/__tests__/objectUtils.test.ts
Normal file
98
packages/editor-ui/src/utils/__tests__/objectUtils.test.ts
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
import { isObjectOrArray, isObject, searchInObject } from '@/utils';
|
||||||
|
|
||||||
|
const testData = [1, '', true, null, undefined, new Date(), () => {}].map((value) => [
|
||||||
|
value,
|
||||||
|
typeof value,
|
||||||
|
]);
|
||||||
|
|
||||||
|
describe('objectUtils', () => {
|
||||||
|
describe('isObjectOrArray', () => {
|
||||||
|
it('should return true for objects', () => {
|
||||||
|
assert(isObjectOrArray({}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true for arrays', () => {
|
||||||
|
assert(isObjectOrArray([]));
|
||||||
|
});
|
||||||
|
|
||||||
|
test.each(testData)('should return false for %j (type %s)', (value) => {
|
||||||
|
assert(!isObjectOrArray(value));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('isObject', () => {
|
||||||
|
it('should return true for objects', () => {
|
||||||
|
assert(isObject({}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false for arrays', () => {
|
||||||
|
assert(!isObject([]));
|
||||||
|
});
|
||||||
|
|
||||||
|
test.each(testData)('should return false for %j (type %s)', (value) => {
|
||||||
|
assert(!isObject(value));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('searchInObject', () => {
|
||||||
|
it('should return true if the search string is found in the object', () => {
|
||||||
|
assert(searchInObject({ a: 'b' }, 'b'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false if the search string is not found in the object', () => {
|
||||||
|
assert(!searchInObject({ a: 'b' }, 'c'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true if the search string is not found in the object as a key', () => {
|
||||||
|
assert(searchInObject({ a: 'b' }, 'a'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true if the search string is found in a nested object', () => {
|
||||||
|
assert(searchInObject({ a: { b: 'c' } }, 'c'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true if the search string is found in a nested object as a key', () => {
|
||||||
|
assert(searchInObject({ a: { b: 'c' } }, 'b'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true if the search string is found in an array', () => {
|
||||||
|
assert(searchInObject(['a', 'b'], 'a'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true if the search string is found in a nested array', () => {
|
||||||
|
assert(searchInObject(['a', ['b', 'c']], 'c'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false if the search string is not found in an array', () => {
|
||||||
|
assert(!searchInObject(['a', 'b'], 'c'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false if the search string is not found in a nested array', () => {
|
||||||
|
assert(!searchInObject(['a', ['b', 'c']], 'd'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true if the search string is found in a nested object in an array', () => {
|
||||||
|
assert(searchInObject([{ a: 'b' }], 'b'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true if the search string is found in a nested array in an object', () => {
|
||||||
|
assert(searchInObject({ a: ['b', 'c'] }, 'c'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false if the search string is not found in a nested object in an array', () => {
|
||||||
|
assert(!searchInObject([{ a: 'b' }], 'c'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false if the search string is not found in a nested array in an object', () => {
|
||||||
|
assert(!searchInObject({ a: ['b', 'c'] }, 'd'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true if the search string is found in an object as a key in a nested array', () => {
|
||||||
|
assert(searchInObject({ a: ['b', { c: 'd' }] }, 'c'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true if the search string is found in an object in a nested array', () => {
|
||||||
|
assert(searchInObject({ a: ['b', { c: 'd' }] }, 'd'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -10,3 +10,4 @@ export * from './typesUtils';
|
||||||
export * from './userUtils';
|
export * from './userUtils';
|
||||||
export * from './sourceControlUtils';
|
export * from './sourceControlUtils';
|
||||||
export * from './expressions';
|
export * from './expressions';
|
||||||
|
export * from './objectUtils';
|
||||||
|
|
20
packages/editor-ui/src/utils/objectUtils.ts
Normal file
20
packages/editor-ui/src/utils/objectUtils.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
type ObjectOrArray = Record<string, unknown> | unknown[];
|
||||||
|
|
||||||
|
export function isDateObject(maybeDate: unknown): maybeDate is Date {
|
||||||
|
return maybeDate instanceof Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isObjectOrArray(maybeObject: unknown): maybeObject is ObjectOrArray {
|
||||||
|
return typeof maybeObject === 'object' && maybeObject !== null && !isDateObject(maybeObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isObject(maybeObject: unknown): maybeObject is Record<string, unknown> {
|
||||||
|
return isObjectOrArray(maybeObject) && !Array.isArray(maybeObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const searchInObject = (obj: ObjectOrArray, searchString: string): boolean =>
|
||||||
|
(Array.isArray(obj) ? obj : Object.entries(obj)).some((entry) =>
|
||||||
|
isObjectOrArray(entry)
|
||||||
|
? searchInObject(entry, searchString)
|
||||||
|
: entry?.toString().includes(searchString),
|
||||||
|
);
|
|
@ -1,6 +1,7 @@
|
||||||
import dateformat from 'dateformat';
|
import dateformat from 'dateformat';
|
||||||
import type { IDataObject } from 'n8n-workflow';
|
import type { IDataObject } from 'n8n-workflow';
|
||||||
import { jsonParse } from 'n8n-workflow';
|
import { jsonParse } from 'n8n-workflow';
|
||||||
|
import { isObject } from '@/utils/objectUtils';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Constants and utility functions than can be used to manipulate different data types and objects
|
Constants and utility functions than can be used to manipulate different data types and objects
|
||||||
|
@ -10,15 +11,11 @@ const SI_SYMBOL = ['', 'k', 'M', 'G', 'T', 'P', 'E'];
|
||||||
|
|
||||||
export const omit = (keyToOmit: string, { [keyToOmit]: _, ...remainder }) => remainder;
|
export const omit = (keyToOmit: string, { [keyToOmit]: _, ...remainder }) => remainder;
|
||||||
|
|
||||||
export function isObjectLiteral(maybeObject: unknown): maybeObject is { [key: string]: string } {
|
|
||||||
return typeof maybeObject === 'object' && maybeObject !== null && !Array.isArray(maybeObject);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isJsonKeyObject(item: unknown): item is {
|
export function isJsonKeyObject(item: unknown): item is {
|
||||||
json: unknown;
|
json: unknown;
|
||||||
[otherKeys: string]: unknown;
|
[otherKeys: string]: unknown;
|
||||||
} {
|
} {
|
||||||
if (!isObjectLiteral(item)) return false;
|
if (!isObject(item)) return false;
|
||||||
|
|
||||||
return Object.keys(item).includes('json');
|
return Object.keys(item).includes('json');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue