n8n/packages/nodes-base/nodes/RenameKeys/RenameKeys.node.ts

236 lines
6.3 KiB
TypeScript
Raw Normal View History

2019-06-23 03:35:23 -07:00
import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
2019-06-23 03:35:23 -07:00
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
get,
set,
unset,
} from 'lodash';
import { options } from 'rhea';
2019-06-23 03:35:23 -07:00
interface IRenameKey {
currentKey: string;
newKey: string;
}
export class RenameKeys implements INodeType {
description: INodeTypeDescription = {
displayName: 'Rename Keys',
name: 'renameKeys',
icon: 'fa:edit',
2019-06-23 03:35:23 -07:00
group: ['transform'],
version: 1,
description: 'Renames keys',
2019-06-23 03:35:23 -07:00
defaults: {
name: 'Rename Keys',
color: '#772244',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'Keys',
name: 'keys',
placeholder: 'Add new key',
description: 'Adds a key which should be renamed',
2019-06-23 03:35:23 -07:00
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
sortable: true,
2019-06-23 03:35:23 -07:00
},
default: {},
options: [
{
displayName: 'Key',
name: 'key',
values: [
{
displayName: 'Current Key Name',
name: 'currentKey',
type: 'string',
default: '',
placeholder: 'currentKey',
refactor: Apply `eslint-plugin-n8n-nodes-base` autofixable rules (#3174) * :zap: Initial setup * :shirt: Update `.eslintignore` * :shirt: Autofix node-param-default-missing (#3173) * :fire: Remove duplicate key * :shirt: Add exceptions * :package: Update package-lock.json * :shirt: Apply `node-class-description-inputs-wrong-trigger-node` (#3176) * :shirt: Apply `node-class-description-inputs-wrong-regular-node` (#3177) * :shirt: Apply `node-class-description-outputs-wrong` (#3178) * :shirt: Apply `node-execute-block-double-assertion-for-items` (#3179) * :shirt: Apply `node-param-default-wrong-for-collection` (#3180) * :shirt: Apply node-param-default-wrong-for-boolean (#3181) * Autofixed default missing * Autofixed booleans, worked well * :zap: Fix params * :rewind: Undo exempted autofixes * :package: Update package-lock.json * :shirt: Apply node-class-description-missing-subtitle (#3182) * :zap: Fix missing comma * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3184) * :shirt: Add exception for `node-class-description-missing-subtitle` * :shirt: Apply `node-param-default-wrong-for-multi-options` (#3185) * :shirt: Apply `node-param-collection-type-unsorted-items` (#3186) * Missing coma * :shirt: Apply `node-param-default-wrong-for-simplify` (#3187) * :shirt: Apply `node-param-description-comma-separated-hyphen` (#3190) * :shirt: Apply `node-param-description-empty-string` (#3189) * :shirt: Apply `node-param-description-excess-inner-whitespace` (#3191) * Rule looks good * Add whitespace rule in eslint config * :zao: fix * :shirt: Apply `node-param-description-identical-to-display-name` (#3193) * :shirt: Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195) * :rewind: Revert ":zao: fix" This reverts commit ef8a76f3dfedffd1bdccf3178af8a8d90cf5a55c. * :shirt: Apply `node-param-description-missing-for-simplify` (#3196) * :shirt: Apply `node-param-description-missing-final-period` (#3194) * Rule working as intended * Add rule to eslint * :shirt: Apply node-param-description-missing-for-return-all (#3197) * :zap: Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: agobrech <ael.gobrecht@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com>
2022-04-22 09:29:51 -07:00
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".',
2019-06-23 03:35:23 -07:00
},
{
displayName: 'New Key Name',
name: 'newKey',
type: 'string',
default: '',
placeholder: 'newKey',
refactor: Apply more `eslint-plugin-n8n-nodes-base` autofixable rules (#3432) * :zap: Update `lintfix` script * :shirt: Remove unneeded lint exceptions * :shirt: Run baseline `lintfix` * :shirt: Apply `node-param-description-miscased-url` (#3441) * :shirt: Apply `rule node-param-placeholder-miscased-id` (#3443) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-name-wrong-for-upsert` (#3446) * :shirt: Apply `node-param-min-value-wrong-for-limit` (#3442) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * Apply `node-param-display-name-wrong-for-dynamic-options` (#3454) * :hammer: fix * :zap: Fix `Assigned To` fields Co-authored-by: Michael Kret <michael.k@radency.com> * :shirt: Apply `rule node-param-default-wrong-for-number` (#3453) * :shirt: Apply `node-param-default-wrong-for-string` (#3452) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * Apply `node-param-display-name-miscased` (#3449) * :hammer: fix * :hammer: exceptions * :zap: review fixes * :shirt: Apply `node-param-description-lowercase-first-char` (#3451) * :zap: fix * :zap: review fixes * :zap: fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-wrong-for-dynamic-options` (#3456) * Rule working as intended * Add rule * :fire: Remove repetitions * :shirt: Add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Small fix for `node-param-description-wrong-for-dynamic-options` * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3460) * :shirt: Apply `node-param-description-line-break-html-tag` (#3462) * :shirt: Run baseline `lintfix` * :shirt: Apply `node-param-options-type-unsorted-items` (#3459) * :zap: fix * :hammer: exceptions * Add exception for Salesmate and Zoom Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :zap: Restore `lintfix` command Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com>
2022-06-03 10:23:49 -07:00
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".',
2019-06-23 03:35:23 -07:00
},
2020-10-22 06:46:03 -07:00
],
2019-06-23 03:35:23 -07:00
},
],
2020-10-22 06:46:03 -07:00
},
{
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 expressiond',
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)',
},
],
},
],
},
],
},
],
},
2020-10-22 06:46:03 -07:00
],
2019-06-23 03:35:23 -07:00
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
2019-06-23 03:35:23 -07:00
let item: INodeExecutionData;
let newItem: INodeExecutionData;
2019-06-23 03:35:23 -07:00
let renameKeys: IRenameKey[];
let value: any; // tslint:disable-line:no-any
2019-06-23 03:35:23 -07:00
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
renameKeys = this.getNodeParameter('keys.key', itemIndex, []) as IRenameKey[];
const regexReplacements = this.getNodeParameter('additionalOptions.regexReplacement.replacements', itemIndex, []) as IDataObject[];
2019-06-23 03:35:23 -07:00
item = items[itemIndex];
// Copy the whole JSON data as data on any level can be renamed
newItem = {
json: JSON.parse(JSON.stringify(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;
}
2019-06-23 03:35:23 -07:00
renameKeys.forEach((renameKey) => {
if (renameKey.currentKey === '' || renameKey.newKey === '' || renameKey.currentKey === renameKey.newKey) {
// Ignore all which do not have all the values set or if the new key is equal to the current key
2019-06-23 03:35:23 -07:00
return;
}
value = get(item.json, renameKey.currentKey as string);
if (value === undefined) {
return;
}
set(newItem.json, renameKey.newKey, value);
2019-06-23 03:35:23 -07:00
unset(newItem.json, renameKey.currentKey as string);
2019-06-23 03:35:23 -07:00
});
regexReplacements.forEach(replacement => {
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, depth: number) => {
for (const key in obj) {
if (Array.isArray(obj)) {
// Don't rename array object references
if (depth !== 0) {
renameObjectKeys(obj[key] as IDataObject, depth - 1);
}
} else if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object' && depth !== 0) {
renameObjectKeys(obj[key] as IDataObject, depth - 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);
});
returnData.push(newItem);
2019-06-23 03:35:23 -07:00
}
return [returnData];
2019-06-23 03:35:23 -07:00
}
}