first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"node": "n8n-nodes-base.renameKeys",
|
||||
"nodeVersion": "1.0",
|
||||
"codexVersion": "1.0",
|
||||
"categories": ["Core Nodes"],
|
||||
"resources": {
|
||||
"primaryDocumentation": [
|
||||
{
|
||||
"url": "https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.renamekeys/"
|
||||
}
|
||||
]
|
||||
},
|
||||
"subcategories": {
|
||||
"Core Nodes": ["Data Transformation"]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
import get from 'lodash/get';
|
||||
import set from 'lodash/set';
|
||||
import unset from 'lodash/unset';
|
||||
import { NodeConnectionTypes, deepCopy } from 'n8n-workflow';
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
IDataObject,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
interface IRenameKey {
|
||||
currentKey: string;
|
||||
newKey: string;
|
||||
}
|
||||
|
||||
export class RenameKeys implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Rename Keys',
|
||||
name: 'renameKeys',
|
||||
icon: 'fa:edit',
|
||||
iconColor: 'crimson',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'Update item field names',
|
||||
defaults: {
|
||||
name: 'Rename Keys',
|
||||
color: '#772244',
|
||||
},
|
||||
inputs: [NodeConnectionTypes.Main],
|
||||
outputs: [NodeConnectionTypes.Main],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Keys',
|
||||
name: 'keys',
|
||||
placeholder: 'Add new key',
|
||||
description: 'Adds a key which should be renamed',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValues: true,
|
||||
sortable: true,
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Key',
|
||||
name: 'key',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Current Key Name',
|
||||
name: 'currentKey',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'currentKey',
|
||||
requiresDataPath: 'single',
|
||||
description:
|
||||
'The current name of the key. It is also possible to define deep keys by using dot-notation like for example: "level1.level2.currentKey".',
|
||||
},
|
||||
{
|
||||
displayName: 'New Key Name',
|
||||
name: 'newKey',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'newKey',
|
||||
description:
|
||||
'The name the key should be renamed to. It is also possible to define deep keys by using dot-notation like for example: "level1.level2.newKey".',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Additional Options',
|
||||
name: 'additionalOptions',
|
||||
type: 'collection',
|
||||
default: {},
|
||||
placeholder: 'Add option',
|
||||
options: [
|
||||
{
|
||||
displayName: 'Regex',
|
||||
name: 'regexReplacement',
|
||||
placeholder: 'Add new regular expression',
|
||||
description: 'Adds a regular expression',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValues: true,
|
||||
sortable: true,
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Replacement',
|
||||
name: 'replacements',
|
||||
values: [
|
||||
{
|
||||
displayName:
|
||||
'Be aware that by using regular expression previously renamed keys can be affected',
|
||||
name: 'regExNotice',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Regular Expression',
|
||||
name: 'searchRegex',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'e.g. [N-n]ame',
|
||||
description: 'Regex to match the key name',
|
||||
hint: 'Learn more and test RegEx <a href="https://regex101.com/">here</a>',
|
||||
},
|
||||
{
|
||||
displayName: 'Replace With',
|
||||
name: 'replaceRegex',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'replacedName',
|
||||
description:
|
||||
"The name the key/s should be renamed to. It's possible to use regex captures e.g. $1, $2, ...",
|
||||
},
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
default: {},
|
||||
placeholder: 'Add Regex Option',
|
||||
options: [
|
||||
{
|
||||
displayName: 'Case Insensitive',
|
||||
name: 'caseInsensitive',
|
||||
type: 'boolean',
|
||||
description: 'Whether to use case insensitive match',
|
||||
default: false,
|
||||
},
|
||||
{
|
||||
displayName: 'Max Depth',
|
||||
name: 'depth',
|
||||
type: 'number',
|
||||
default: -1,
|
||||
description: 'Maximum depth to replace keys',
|
||||
hint: 'Specify number for depth level (-1 for unlimited, 0 for top level only)',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
hints: [
|
||||
{
|
||||
type: 'warning',
|
||||
message:
|
||||
'Complex regex patterns like nested quantifiers .*+, ()*+, or multiple wildcards may cause performance issues. Consider using simpler patterns like [a-z]+ or \\w+ for better performance.',
|
||||
displayCondition:
|
||||
'={{ $parameter.additionalOptions.regexReplacement.replacements && $parameter.additionalOptions.regexReplacement.replacements.some(r => r.searchRegex && /(\\.\\*\\+|\\)\\*\\+|\\+\\*|\\*.*\\*|\\+.*\\+|\\?.*\\?|\\{[0-9]+,\\}|\\*{2,}|\\+{2,}|\\?{2,}|[a-zA-Z0-9]{4,}[\\*\\+]|\\([^)]*\\|[^)]*\\)[\\*\\+])/.test(r.searchRegex)) }}',
|
||||
whenToDisplay: 'always',
|
||||
location: 'outputPane',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const returnData: INodeExecutionData[] = [];
|
||||
|
||||
let item: INodeExecutionData;
|
||||
let newItem: INodeExecutionData;
|
||||
let renameKeys: IRenameKey[];
|
||||
let value: any;
|
||||
|
||||
const renameKey = (key: IRenameKey) => {
|
||||
if (key.currentKey === '' || key.newKey === '' || key.currentKey === key.newKey) {
|
||||
// Ignore all which do not have all the values set or if the new key is equal to the current key
|
||||
return;
|
||||
}
|
||||
value = get(item.json, key.currentKey);
|
||||
if (value === undefined) {
|
||||
return;
|
||||
}
|
||||
set(newItem.json, key.newKey, value);
|
||||
|
||||
unset(newItem.json, key.currentKey);
|
||||
};
|
||||
|
||||
const regexReplaceKey = (replacement: IDataObject) => {
|
||||
const { searchRegex, replaceRegex, options } = replacement;
|
||||
const { depth, caseInsensitive } = options as IDataObject;
|
||||
|
||||
const flags = (caseInsensitive as boolean) ? 'i' : undefined;
|
||||
|
||||
const regex = new RegExp(searchRegex as string, flags);
|
||||
|
||||
const renameObjectKeys = (obj: IDataObject, objDepth: number) => {
|
||||
for (const key in obj) {
|
||||
if (Array.isArray(obj)) {
|
||||
// Don't rename array object references
|
||||
if (objDepth !== 0) {
|
||||
renameObjectKeys(obj[key] as IDataObject, objDepth - 1);
|
||||
}
|
||||
} else if (obj.hasOwnProperty(key)) {
|
||||
if (typeof obj[key] === 'object' && objDepth !== 0) {
|
||||
renameObjectKeys(obj[key] as IDataObject, objDepth - 1);
|
||||
}
|
||||
if (key.match(regex)) {
|
||||
const newKey = key.replace(regex, replaceRegex as string);
|
||||
if (newKey !== key) {
|
||||
obj[newKey] = obj[key];
|
||||
delete obj[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
};
|
||||
newItem.json = renameObjectKeys(newItem.json, depth as number);
|
||||
};
|
||||
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
||||
try {
|
||||
renameKeys = this.getNodeParameter('keys.key', itemIndex, []) as IRenameKey[];
|
||||
const regexReplacements = this.getNodeParameter(
|
||||
'additionalOptions.regexReplacement.replacements',
|
||||
itemIndex,
|
||||
[],
|
||||
) as IDataObject[];
|
||||
|
||||
item = items[itemIndex];
|
||||
|
||||
// Copy the whole JSON data as data on any level can be renamed
|
||||
newItem = {
|
||||
json: deepCopy(item.json),
|
||||
pairedItem: {
|
||||
item: itemIndex,
|
||||
},
|
||||
};
|
||||
|
||||
if (item.binary !== undefined) {
|
||||
// Reference binary data if any exists. We can reference it
|
||||
// as this nodes does not change it
|
||||
newItem.binary = item.binary;
|
||||
}
|
||||
|
||||
renameKeys.forEach(renameKey);
|
||||
|
||||
regexReplacements.forEach(regexReplaceKey);
|
||||
|
||||
returnData.push(newItem);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
const executionErrorData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray({ error: error.message }),
|
||||
{ itemData: { item: itemIndex } },
|
||||
);
|
||||
returnData.push(...executionErrorData);
|
||||
continue;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return [returnData];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { NodeTestHarness } from '@nodes-testing/node-test-harness';
|
||||
|
||||
describe('Test Rename Keys Node', () => {
|
||||
new NodeTestHarness().setupTests();
|
||||
});
|
||||
@@ -0,0 +1,302 @@
|
||||
{
|
||||
"name": "node-268-node-unit-test-rename-keys",
|
||||
"nodes": [
|
||||
{
|
||||
"parameters": {},
|
||||
"id": "2057ea8b-4834-425f-8ca5-d810c20628fe",
|
||||
"name": "When clicking \"Execute Workflow\"",
|
||||
"type": "n8n-nodes-base.manualTrigger",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
240,
|
||||
320
|
||||
]
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"keys": {
|
||||
"key": [
|
||||
{
|
||||
"currentKey": "=propertyName",
|
||||
"newKey": "test"
|
||||
}
|
||||
]
|
||||
},
|
||||
"additionalOptions": {}
|
||||
},
|
||||
"id": "c44432e0-47bd-43e9-aefe-819c01920dc0",
|
||||
"name": "Rename Keys",
|
||||
"type": "n8n-nodes-base.renameKeys",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
700,
|
||||
200
|
||||
]
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"values": {
|
||||
"number": [
|
||||
{}
|
||||
]
|
||||
},
|
||||
"options": {}
|
||||
},
|
||||
"id": "2e4f420d-1aac-408f-8585-4c332a68ae48",
|
||||
"name": "Set",
|
||||
"type": "n8n-nodes-base.set",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
500,
|
||||
200
|
||||
]
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"values": {
|
||||
"number": [
|
||||
{
|
||||
"name": "propertyName.newPropertyName",
|
||||
"value": 21
|
||||
}
|
||||
]
|
||||
},
|
||||
"options": {}
|
||||
},
|
||||
"id": "4a8bcf08-d16b-462b-9bb9-f1ab28c0cc88",
|
||||
"name": "Set with dot notation",
|
||||
"type": "n8n-nodes-base.set",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
500,
|
||||
380
|
||||
]
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"keys": {
|
||||
"key": [
|
||||
{
|
||||
"currentKey": "=propertyName.newPropertyName",
|
||||
"newKey": "test.dot"
|
||||
}
|
||||
]
|
||||
},
|
||||
"additionalOptions": {}
|
||||
},
|
||||
"id": "3efdd843-74aa-478c-a1d3-66bd36e49b24",
|
||||
"name": "Rename Keys with dot notation",
|
||||
"type": "n8n-nodes-base.renameKeys",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
700,
|
||||
380
|
||||
]
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"values": {
|
||||
"number": [
|
||||
{
|
||||
"name": "propertyName.newPropertyName",
|
||||
"value": 21
|
||||
}
|
||||
]
|
||||
},
|
||||
"options": {}
|
||||
},
|
||||
"id": "3fae3aad-89ad-40c7-a49e-1ae1c4ddf999",
|
||||
"name": "Set with undefined",
|
||||
"type": "n8n-nodes-base.set",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
500,
|
||||
560
|
||||
]
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"keys": {
|
||||
"key": [
|
||||
{
|
||||
"currentKey": "=propertyName.newPropertyName"
|
||||
}
|
||||
]
|
||||
},
|
||||
"additionalOptions": {}
|
||||
},
|
||||
"id": "7d6f85d3-1847-409b-a0b4-ebbfbaa0dd19",
|
||||
"name": "Rename Keys with undefined",
|
||||
"type": "n8n-nodes-base.renameKeys",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
700,
|
||||
560
|
||||
]
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"values": {
|
||||
"number": [
|
||||
{
|
||||
"name": "propertyName.newPropertyName"
|
||||
},
|
||||
{
|
||||
"name": "regex"
|
||||
}
|
||||
]
|
||||
},
|
||||
"options": {}
|
||||
},
|
||||
"id": "748143c8-cf83-4585-9f1c-1ef23911accb",
|
||||
"name": "Set with dot notation1",
|
||||
"type": "n8n-nodes-base.set",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
500,
|
||||
760
|
||||
]
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"additionalOptions": {
|
||||
"regexReplacement": {
|
||||
"replacements": [
|
||||
{
|
||||
"searchRegex": "reg",
|
||||
"replaceRegex": "test",
|
||||
"options": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"id": "e47b7001-8270-43e7-a6ac-6bede047cfac",
|
||||
"name": "Rename Keys with Regex",
|
||||
"type": "n8n-nodes-base.renameKeys",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
700,
|
||||
760
|
||||
]
|
||||
}
|
||||
],
|
||||
"pinData": {
|
||||
"Rename Keys": [
|
||||
{
|
||||
"json": {
|
||||
"test": 0
|
||||
}
|
||||
}
|
||||
],
|
||||
"Rename Keys with dot notation": [
|
||||
{
|
||||
"json": {
|
||||
"propertyName": {},
|
||||
"test": {
|
||||
"dot": 21
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"Rename Keys with undefined": [
|
||||
{
|
||||
"json": {
|
||||
"propertyName": {
|
||||
"newPropertyName": 21
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"Rename Keys with Regex": [
|
||||
{
|
||||
"json": {
|
||||
"propertyName": {
|
||||
"newPropertyName": 0
|
||||
},
|
||||
"testex": 0
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"connections": {
|
||||
"When clicking \"Execute Workflow\"": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "Set",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
},
|
||||
{
|
||||
"node": "Set with dot notation",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
},
|
||||
{
|
||||
"node": "Set with undefined",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
},
|
||||
{
|
||||
"node": "Set with dot notation1",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
},
|
||||
"Set": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "Rename Keys",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
},
|
||||
"Set with dot notation": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "Rename Keys with dot notation",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
},
|
||||
"Set with undefined": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "Rename Keys with undefined",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
},
|
||||
"Set with dot notation1": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "Rename Keys with Regex",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
"active": false,
|
||||
"settings": {},
|
||||
"versionId": "cd3cd0a4-12ec-44a9-8cca-f0c4acd4b0cd",
|
||||
"id": "231",
|
||||
"meta": {
|
||||
"instanceId": "27cc9b56542ad45b38725555722c50a1c3fee1670bbb67980558314ee08517c4"
|
||||
},
|
||||
"tags": []
|
||||
}
|
||||
Reference in New Issue
Block a user