mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 04:34:06 -08:00
fix: IsWeekend not checking if DateTime (#5221) (no-changelog)
This commit is contained in:
parent
3ea83d872e
commit
683492155e
|
@ -139,7 +139,10 @@ function isBetween(date: Date | DateTime, extraArgs: unknown[]): boolean {
|
|||
return secondDate > date && date > firstDate;
|
||||
}
|
||||
|
||||
function isDst(date: Date): boolean {
|
||||
function isDst(date: Date | DateTime): boolean {
|
||||
if (isDateTime(date)) {
|
||||
return date.isInDST;
|
||||
}
|
||||
return DateTime.fromJSDate(date).isInDST;
|
||||
}
|
||||
|
||||
|
@ -154,11 +157,14 @@ function isInLast(date: Date | DateTime, extraArgs: unknown[]): boolean {
|
|||
return dateInThePast <= thisDate && thisDate <= DateTime.now();
|
||||
}
|
||||
|
||||
function isWeekend(date: Date): boolean {
|
||||
function isWeekend(date: Date | DateTime): boolean {
|
||||
enum DAYS {
|
||||
saturday = 6,
|
||||
sunday = 7,
|
||||
}
|
||||
if (isDateTime(date)) {
|
||||
return [DAYS.saturday, DAYS.sunday].includes(date.weekday);
|
||||
}
|
||||
return [DAYS.saturday, DAYS.sunday].includes(DateTime.fromJSDate(date).weekday);
|
||||
}
|
||||
|
||||
|
@ -200,7 +206,7 @@ function toLocaleString(date: Date | DateTime, extraArgs: unknown[]): string {
|
|||
return DateTime.fromJSDate(date).toLocaleString(dateFormat, { locale });
|
||||
}
|
||||
|
||||
function toTimeFromNow(date: Date): string {
|
||||
function toTimeFromNow(date: Date | DateTime): string {
|
||||
let diffObj: Duration;
|
||||
if (isDateTime(date)) {
|
||||
diffObj = date.diffNow();
|
||||
|
|
|
@ -2,16 +2,16 @@
|
|||
* @jest-environment jsdom
|
||||
*/
|
||||
|
||||
import { extend } from '@/Extensions';
|
||||
import { dateExtensions } from '@/Extensions/DateExtensions';
|
||||
import { evaluate, getLocalISOString } from './Helpers';
|
||||
import { DateTime } from 'luxon';
|
||||
import { evaluate, getLocalISOString, TEST_TIMEZONE } from './Helpers';
|
||||
|
||||
describe('Data Transformation Functions', () => {
|
||||
describe('Date Data Transformation Functions', () => {
|
||||
test('.isWeekend() should work correctly on a date', () => {
|
||||
expect(evaluate('={{DateTime.now().isWeekend()}}')).toEqual(
|
||||
extend(new Date(), 'isWeekend', []),
|
||||
);
|
||||
expect(evaluate('={{ DateTime.local(2023, 1, 20).isWeekend() }}')).toBe(false);
|
||||
expect(evaluate('={{ DateTime.local(2023, 1, 21).isWeekend() }}')).toBe(true);
|
||||
expect(evaluate('={{ DateTime.local(2023, 1, 22).isWeekend() }}')).toBe(true);
|
||||
expect(evaluate('={{ DateTime.local(2023, 1, 23).isWeekend() }}')).toBe(false);
|
||||
});
|
||||
|
||||
test('.toTimeFromNow() should work correctly on a date', () => {
|
||||
|
@ -20,29 +20,24 @@ describe('Data Transformation Functions', () => {
|
|||
});
|
||||
|
||||
test('.beginningOf("week") should work correctly on a date', () => {
|
||||
expect(evaluate('={{(new Date).beginningOf("week")}}')).toEqual(
|
||||
dateExtensions.functions.beginningOf(new Date(), ['week']),
|
||||
expect(evaluate('={{ DateTime.local(2023, 1, 20).beginningOf("week") }}')).toEqual(
|
||||
DateTime.local(2023, 1, 16, { zone: TEST_TIMEZONE }).toJSDate(),
|
||||
);
|
||||
});
|
||||
|
||||
test('.endOfMonth() should work correctly on a date', () => {
|
||||
expect(evaluate('={{ DateTime.now().endOfMonth() }}')).toEqual(
|
||||
dateExtensions.functions.endOfMonth(new Date()),
|
||||
expect(evaluate('={{ DateTime.local(2023, 1, 16).endOfMonth() }}')).toEqual(
|
||||
DateTime.local(2023, 1, 31, 23, 59, 59, 999, { zone: TEST_TIMEZONE }).toJSDate(),
|
||||
);
|
||||
});
|
||||
|
||||
test('.extract("day") should work correctly on a date', () => {
|
||||
expect(evaluate('={{ DateTime.now().extract("day") }}')).toEqual(
|
||||
dateExtensions.functions.extract(new Date(), ['day']),
|
||||
);
|
||||
expect(evaluate('={{ DateTime.local(2023, 1, 20).extract("day") }}')).toEqual(20);
|
||||
});
|
||||
|
||||
test('.format("yyyy LLL dd") should work correctly on a date', () => {
|
||||
expect(evaluate('={{ DateTime.now().format("yyyy LLL dd") }}')).toEqual(
|
||||
dateExtensions.functions.format(new Date(), ['yyyy LLL dd']),
|
||||
);
|
||||
expect(evaluate('={{ DateTime.now().format("yyyy LLL dd") }}')).not.toEqual(
|
||||
dateExtensions.functions.format(new Date(), ["HH 'hours and' mm 'minutes'"]),
|
||||
expect(evaluate('={{ DateTime.local(2023, 1, 16).format("yyyy LLL dd") }}')).toEqual(
|
||||
'2023 Jan 16',
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import { Expression, INodeExecutionData, Workflow } from '../../src';
|
||||
import * as Helpers from '../Helpers';
|
||||
|
||||
export const TEST_TIMEZONE = 'America/New_York';
|
||||
|
||||
export const nodeTypes = Helpers.NodeTypes();
|
||||
export const workflow = new Workflow({
|
||||
nodes: [
|
||||
|
@ -28,7 +30,7 @@ export const evaluate = (value: string, values?: INodeExecutionData[]) =>
|
|||
'node',
|
||||
values ?? [],
|
||||
'manual',
|
||||
'America/New_York',
|
||||
TEST_TIMEZONE,
|
||||
{},
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in a new issue