mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-12 15:44:06 -08:00
⚡ Improved DateTime-Node
This commit is contained in:
parent
5ea3ef3ec0
commit
c258c96e38
|
@ -1,14 +1,16 @@
|
||||||
|
import * as moment from 'moment-timezone';
|
||||||
|
import { get, set } from 'lodash';
|
||||||
|
|
||||||
import { IExecuteFunctions } from 'n8n-core';
|
import { IExecuteFunctions } from 'n8n-core';
|
||||||
import {
|
import {
|
||||||
|
IDataObject,
|
||||||
|
ILoadOptionsFunctions,
|
||||||
INodeExecutionData,
|
INodeExecutionData,
|
||||||
INodeType,
|
INodeType,
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
INodePropertyOptions,
|
INodePropertyOptions,
|
||||||
ILoadOptionsFunctions,
|
|
||||||
IDataObject,
|
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import * as moment from 'moment-timezone';
|
|
||||||
|
|
||||||
export class DateTime implements INodeType {
|
export class DateTime implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
|
@ -32,15 +34,15 @@ export class DateTime implements INodeType {
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
name: 'Format a Date',
|
name: 'Format a Date',
|
||||||
description: 'Apply to a date a diferent format',
|
description: 'Convert a date to a different format',
|
||||||
value: 'format'
|
value: 'format'
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
default: 'format',
|
default: 'format',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Field Name',
|
displayName: 'Key Name',
|
||||||
name: 'fieldName',
|
name: 'keyName',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
action:[
|
action:[
|
||||||
|
@ -50,6 +52,7 @@ export class DateTime implements INodeType {
|
||||||
},
|
},
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: '',
|
default: '',
|
||||||
|
description: 'The name of the key of which the value should be converted.',
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -64,6 +67,7 @@ export class DateTime implements INodeType {
|
||||||
},
|
},
|
||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
default: false,
|
default: false,
|
||||||
|
description: 'If a predefined format should be selected or custom format entered.',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'To Format',
|
displayName: 'To Format',
|
||||||
|
@ -80,6 +84,8 @@ export class DateTime implements INodeType {
|
||||||
},
|
},
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: '',
|
default: '',
|
||||||
|
placeholder: 'YYYY-MM-DD',
|
||||||
|
description: 'The format to convert the date to.',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'To Format',
|
displayName: 'To Format',
|
||||||
|
@ -133,22 +139,7 @@ export class DateTime implements INodeType {
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
default: 'MM/DD/YYYY',
|
default: 'MM/DD/YYYY',
|
||||||
},
|
description: 'The format to convert the date to.',
|
||||||
{
|
|
||||||
displayName: 'To Timezone',
|
|
||||||
name: 'toTimezone',
|
|
||||||
type: 'options',
|
|
||||||
typeOptions: {
|
|
||||||
loadOptionsMethod: 'getTimezones',
|
|
||||||
},
|
|
||||||
displayOptions: {
|
|
||||||
show: {
|
|
||||||
action:[
|
|
||||||
'format'
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
default: 'UTC',
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Options',
|
displayName: 'Options',
|
||||||
|
@ -172,6 +163,7 @@ export class DateTime implements INodeType {
|
||||||
loadOptionsMethod: 'getTimezones',
|
loadOptionsMethod: 'getTimezones',
|
||||||
},
|
},
|
||||||
default: 'UTC',
|
default: 'UTC',
|
||||||
|
description: 'The timezone to convert from.',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'From Format',
|
displayName: 'From Format',
|
||||||
|
@ -181,10 +173,21 @@ export class DateTime implements INodeType {
|
||||||
description: 'In case the input format is not recognized you can provide the format ',
|
description: 'In case the input format is not recognized you can provide the format ',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Keep Old Date',
|
displayName: 'New Key Name',
|
||||||
name: 'keepOldDate',
|
name: 'newKeyName',
|
||||||
type: 'boolean',
|
type: 'string',
|
||||||
default: false,
|
default: 'newDate',
|
||||||
|
description: 'If set will the new date be added under the new key name and the existing one will not be touched.',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'To Timezone',
|
||||||
|
name: 'toTimezone',
|
||||||
|
type: 'options',
|
||||||
|
typeOptions: {
|
||||||
|
loadOptionsMethod: 'getTimezones',
|
||||||
|
},
|
||||||
|
default: 'UTC',
|
||||||
|
description: 'The timezone to convert to.',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -212,48 +215,84 @@ export class DateTime implements INodeType {
|
||||||
|
|
||||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
const returnData: IDataObject[] = [];
|
|
||||||
const length = items.length as unknown as number;
|
const length = items.length as unknown as number;
|
||||||
let responseData;
|
const returnData: INodeExecutionData[] = [];
|
||||||
|
|
||||||
|
const workflowTimezone = this.getTimezone();
|
||||||
|
let item: INodeExecutionData;
|
||||||
|
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
const action = this.getNodeParameter('action', 0) as string;
|
const action = this.getNodeParameter('action', 0) as string;
|
||||||
|
item = items[i];
|
||||||
|
|
||||||
if (action === 'format') {
|
if (action === 'format') {
|
||||||
const fieldName = this.getNodeParameter('fieldName', i) as string;
|
const keyName = this.getNodeParameter('keyName', i) as string;
|
||||||
const toTimezone = this.getNodeParameter('toTimezone', i) as string;
|
|
||||||
const toFormat = this.getNodeParameter('toFormat', i) as string;
|
const toFormat = this.getNodeParameter('toFormat', i) as string;
|
||||||
const options = this.getNodeParameter('options', i) as IDataObject;
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
||||||
let newDate;
|
let newDate;
|
||||||
let clone = { ...items[i].json };
|
const currentDate = get(item.json, keyName);
|
||||||
if (clone[fieldName] === undefined) {
|
|
||||||
throw new Error(`The field ${fieldName} does not exist on the input data`);
|
if (currentDate === undefined) {
|
||||||
|
throw new Error(`The key ${keyName} does not exist on the input data`);
|
||||||
}
|
}
|
||||||
if (!moment(clone[fieldName] as string | number).isValid()) {
|
if (!moment(currentDate as string | number).isValid()) {
|
||||||
throw new Error('The date input format is not recognized, please set the "From Format" field');
|
throw new Error('The date input format could not be recognized. Please set the "From Format" field');
|
||||||
}
|
}
|
||||||
if (Number.isInteger(clone[fieldName] as number)) {
|
if (Number.isInteger(currentDate as number)) {
|
||||||
newDate = moment.unix(clone[fieldName] as number).tz(toTimezone).format(toFormat);
|
newDate = moment.unix(currentDate as number);
|
||||||
} else {
|
} else {
|
||||||
newDate = moment(clone[fieldName] as string).tz(toTimezone).format(toFormat);
|
if (options.fromTimezone || options.toTimezone) {
|
||||||
if (options.fromTimezone) {
|
const fromTimezone = options.fromTimezone || workflowTimezone;
|
||||||
newDate = moment.tz(clone[fieldName] as string, options.fromTimezone as string).tz(toTimezone).format(toFormat);
|
if (options.fromFormat) {
|
||||||
}
|
newDate = moment.tz(currentDate as string, options.fromFormat as string, fromTimezone as string);
|
||||||
if (options.fromFormat) {
|
} else {
|
||||||
newDate = moment(clone[fieldName] as string, options.fromFormat as string).tz(toTimezone).format(toFormat);
|
newDate = moment.tz(currentDate as string, fromTimezone as string);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (options.fromFormat) {
|
||||||
|
newDate = moment(currentDate as string, options.fromFormat as string);
|
||||||
|
} else {
|
||||||
|
newDate = moment(currentDate as string);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!options.keepOldDate) {
|
|
||||||
clone[fieldName] = newDate;
|
if (options.toTimezone || options.fromTimezone) {
|
||||||
} else {
|
// If either a source or a target timezone got defined the
|
||||||
clone['newDate'] = newDate;
|
// timezone of the date has to be changed. If a target-timezone
|
||||||
|
// is set use it else fall back to workflow timezone.
|
||||||
|
newDate = newDate.tz(options.toTimezone as string || workflowTimezone);
|
||||||
}
|
}
|
||||||
responseData = clone;
|
|
||||||
}
|
newDate = newDate.format(toFormat);
|
||||||
if (Array.isArray(responseData)) {
|
|
||||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
let newItem: INodeExecutionData;
|
||||||
} else {
|
if (keyName.includes('.')) {
|
||||||
returnData.push(responseData as IDataObject);
|
// Uses dot notation so copy all data
|
||||||
|
newItem = {
|
||||||
|
json: JSON.parse(JSON.stringify(items[i].json)),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// Does not use dot notation so shallow copy is enough
|
||||||
|
newItem = {
|
||||||
|
json: { ...items[i].json },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.binary !== undefined) {
|
||||||
|
newItem.binary = item.binary;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.newKeyName) {
|
||||||
|
set(newItem, `json.${options.newKeyName}`, newDate);
|
||||||
|
} else {
|
||||||
|
set(newItem, `json.${keyName}`, newDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
returnData.push(newItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return [this.helpers.returnJsonArray(returnData)];
|
|
||||||
|
return this.prepareOutputData(returnData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue