mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Feat rounding operation
This commit is contained in:
parent
7bbb0ba289
commit
d0038812d7
|
@ -15,6 +15,7 @@ import { CurrentDateDescription } from './CurrentDateDescription';
|
||||||
import { AddToDateDescription } from './AddToDateDescription';
|
import { AddToDateDescription } from './AddToDateDescription';
|
||||||
import { SubtractFromDateDescription } from './SubtractFromDateDescription';
|
import { SubtractFromDateDescription } from './SubtractFromDateDescription';
|
||||||
import { FormatDateDescription } from './FormatDateDescription';
|
import { FormatDateDescription } from './FormatDateDescription';
|
||||||
|
import { RoundDateDescription } from './RoundDateDescription';
|
||||||
|
|
||||||
export class DateTimeV2 implements INodeType {
|
export class DateTimeV2 implements INodeType {
|
||||||
description: INodeTypeDescription;
|
description: INodeTypeDescription;
|
||||||
|
@ -71,6 +72,7 @@ export class DateTimeV2 implements INodeType {
|
||||||
...AddToDateDescription,
|
...AddToDateDescription,
|
||||||
...SubtractFromDateDescription,
|
...SubtractFromDateDescription,
|
||||||
...FormatDateDescription,
|
...FormatDateDescription,
|
||||||
|
...RoundDateDescription,
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -155,6 +157,28 @@ export class DateTimeV2 implements INodeType {
|
||||||
} catch {
|
} catch {
|
||||||
throw new NodeOperationError(this.getNode(), 'Invalid date format');
|
throw new NodeOperationError(this.getNode(), 'Invalid date format');
|
||||||
}
|
}
|
||||||
|
} else if (operation === 'roundDate') {
|
||||||
|
const date = this.getNodeParameter('date', i) as string;
|
||||||
|
const mode = this.getNodeParameter('mode', i) as string;
|
||||||
|
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
|
||||||
|
try {
|
||||||
|
const momentDate = moment.tz(date, workflowTimezone);
|
||||||
|
if (mode === 'roundDown') {
|
||||||
|
const toNearest = this.getNodeParameter('toNearest', i) as string;
|
||||||
|
responseData.push({
|
||||||
|
[outputFieldName]: momentDate.startOf(toNearest as moment.unitOfTime.StartOf),
|
||||||
|
});
|
||||||
|
} else if (mode === 'roundUp') {
|
||||||
|
const to = this.getNodeParameter('to', i) as string;
|
||||||
|
responseData.push({
|
||||||
|
[outputFieldName]: momentDate
|
||||||
|
.add(1, to as moment.unitOfTime.DurationConstructor)
|
||||||
|
.startOf(to as moment.unitOfTime.StartOf),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
throw new NodeOperationError(this.getNode(), 'Invalid date format');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const executionData = this.helpers.constructExecutionMetaData(
|
const executionData = this.helpers.constructExecutionMetaData(
|
||||||
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
||||||
|
|
|
@ -89,7 +89,8 @@ export const FormatDateDescription: INodeProperties[] = [
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
hint: 'List of special tokens <a target="_blank" href="https://moment.github.io/luxon/#/formatting?id=table-of-tokens">More info</a>',
|
hint: 'List of special tokens <a target="_blank" href="https://moment.github.io/luxon/#/formatting?id=table-of-tokens">More info</a>',
|
||||||
default: 'yyyy-MM-dd',
|
default: '',
|
||||||
|
placeholder: 'yyyy-MM-dd',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Output Field Name',
|
displayName: 'Output Field Name',
|
||||||
|
|
121
packages/nodes-base/nodes/DateTime/V2/RoundDateDescription.ts
Normal file
121
packages/nodes-base/nodes/DateTime/V2/RoundDateDescription.ts
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
import type { INodeProperties } from 'n8n-workflow';
|
||||||
|
|
||||||
|
export const RoundDateDescription: INodeProperties[] = [
|
||||||
|
{
|
||||||
|
displayName:
|
||||||
|
"You can also do this using an expression, e.g. <code>{{ your_date.endOfMonth() }}</code>. <a target='_blank' href='https://docs.n8n.io/code-examples/expressions/luxon/'>More info</a>",
|
||||||
|
name: 'notice',
|
||||||
|
type: 'notice',
|
||||||
|
default: '',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
operation: ['roundDate'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Date',
|
||||||
|
name: 'date',
|
||||||
|
type: 'string',
|
||||||
|
description: 'The date that you want to round',
|
||||||
|
default: '',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
operation: ['roundDate'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Mode',
|
||||||
|
name: 'mode',
|
||||||
|
type: 'options',
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'Round Down',
|
||||||
|
value: 'roundDown',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Round Up',
|
||||||
|
value: 'roundUp',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
default: 'roundDown',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
operation: ['roundDate'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'To Nearest',
|
||||||
|
name: 'toNearest',
|
||||||
|
type: 'options',
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'Day',
|
||||||
|
value: 'day',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Hour',
|
||||||
|
value: 'hour',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Minute',
|
||||||
|
value: 'minute',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Month',
|
||||||
|
value: 'month',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Second',
|
||||||
|
value: 'second',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Week',
|
||||||
|
value: 'week',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Year',
|
||||||
|
value: 'year',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
default: 'month',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
operation: ['roundDate'],
|
||||||
|
mode: ['roundDown'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'To',
|
||||||
|
name: 'to',
|
||||||
|
type: 'options',
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'End of Month',
|
||||||
|
value: 'month',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
default: 'month',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
operation: ['roundDate'],
|
||||||
|
mode: ['roundUp'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Output Field Name',
|
||||||
|
name: 'outputFieldName',
|
||||||
|
type: 'string',
|
||||||
|
default: 'roundedDate',
|
||||||
|
description: 'Name of the field to put the output in',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
operation: ['roundDate'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
Loading…
Reference in a new issue