fix: IsWeekend not checking if DateTime (#5221) (no-changelog)

This commit is contained in:
Valya 2023-01-23 13:28:17 +00:00 committed by GitHub
parent 3ea83d872e
commit 683492155e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 22 deletions

View file

@ -139,7 +139,10 @@ function isBetween(date: Date | DateTime, extraArgs: unknown[]): boolean {
return secondDate > date && date > firstDate; 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; return DateTime.fromJSDate(date).isInDST;
} }
@ -154,11 +157,14 @@ function isInLast(date: Date | DateTime, extraArgs: unknown[]): boolean {
return dateInThePast <= thisDate && thisDate <= DateTime.now(); return dateInThePast <= thisDate && thisDate <= DateTime.now();
} }
function isWeekend(date: Date): boolean { function isWeekend(date: Date | DateTime): boolean {
enum DAYS { enum DAYS {
saturday = 6, saturday = 6,
sunday = 7, sunday = 7,
} }
if (isDateTime(date)) {
return [DAYS.saturday, DAYS.sunday].includes(date.weekday);
}
return [DAYS.saturday, DAYS.sunday].includes(DateTime.fromJSDate(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 }); return DateTime.fromJSDate(date).toLocaleString(dateFormat, { locale });
} }
function toTimeFromNow(date: Date): string { function toTimeFromNow(date: Date | DateTime): string {
let diffObj: Duration; let diffObj: Duration;
if (isDateTime(date)) { if (isDateTime(date)) {
diffObj = date.diffNow(); diffObj = date.diffNow();

View file

@ -2,16 +2,16 @@
* @jest-environment jsdom * @jest-environment jsdom
*/ */
import { extend } from '@/Extensions'; import { DateTime } from 'luxon';
import { dateExtensions } from '@/Extensions/DateExtensions'; import { evaluate, getLocalISOString, TEST_TIMEZONE } from './Helpers';
import { evaluate, getLocalISOString } from './Helpers';
describe('Data Transformation Functions', () => { describe('Data Transformation Functions', () => {
describe('Date Data Transformation Functions', () => { describe('Date Data Transformation Functions', () => {
test('.isWeekend() should work correctly on a date', () => { test('.isWeekend() should work correctly on a date', () => {
expect(evaluate('={{DateTime.now().isWeekend()}}')).toEqual( expect(evaluate('={{ DateTime.local(2023, 1, 20).isWeekend() }}')).toBe(false);
extend(new Date(), 'isWeekend', []), 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', () => { 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', () => { test('.beginningOf("week") should work correctly on a date', () => {
expect(evaluate('={{(new Date).beginningOf("week")}}')).toEqual( expect(evaluate('={{ DateTime.local(2023, 1, 20).beginningOf("week") }}')).toEqual(
dateExtensions.functions.beginningOf(new Date(), ['week']), DateTime.local(2023, 1, 16, { zone: TEST_TIMEZONE }).toJSDate(),
); );
}); });
test('.endOfMonth() should work correctly on a date', () => { test('.endOfMonth() should work correctly on a date', () => {
expect(evaluate('={{ DateTime.now().endOfMonth() }}')).toEqual( expect(evaluate('={{ DateTime.local(2023, 1, 16).endOfMonth() }}')).toEqual(
dateExtensions.functions.endOfMonth(new Date()), DateTime.local(2023, 1, 31, 23, 59, 59, 999, { zone: TEST_TIMEZONE }).toJSDate(),
); );
}); });
test('.extract("day") should work correctly on a date', () => { test('.extract("day") should work correctly on a date', () => {
expect(evaluate('={{ DateTime.now().extract("day") }}')).toEqual( expect(evaluate('={{ DateTime.local(2023, 1, 20).extract("day") }}')).toEqual(20);
dateExtensions.functions.extract(new Date(), ['day']),
);
}); });
test('.format("yyyy LLL dd") should work correctly on a date', () => { test('.format("yyyy LLL dd") should work correctly on a date', () => {
expect(evaluate('={{ DateTime.now().format("yyyy LLL dd") }}')).toEqual( expect(evaluate('={{ DateTime.local(2023, 1, 16).format("yyyy LLL dd") }}')).toEqual(
dateExtensions.functions.format(new Date(), ['yyyy LLL dd']), '2023 Jan 16',
);
expect(evaluate('={{ DateTime.now().format("yyyy LLL dd") }}')).not.toEqual(
dateExtensions.functions.format(new Date(), ["HH 'hours and' mm 'minutes'"]),
); );
}); });

View file

@ -1,6 +1,8 @@
import { Expression, INodeExecutionData, Workflow } from '../../src'; import { Expression, INodeExecutionData, Workflow } from '../../src';
import * as Helpers from '../Helpers'; import * as Helpers from '../Helpers';
export const TEST_TIMEZONE = 'America/New_York';
export const nodeTypes = Helpers.NodeTypes(); export const nodeTypes = Helpers.NodeTypes();
export const workflow = new Workflow({ export const workflow = new Workflow({
nodes: [ nodes: [
@ -28,7 +30,7 @@ export const evaluate = (value: string, values?: INodeExecutionData[]) =>
'node', 'node',
values ?? [], values ?? [],
'manual', 'manual',
'America/New_York', TEST_TIMEZONE,
{}, {},
); );