From ae091e1f0d124623242ada0522ec02c05bbd30c2 Mon Sep 17 00:00:00 2001 From: Lesley Dreyer Date: Tue, 26 Mar 2024 17:59:37 -0700 Subject: [PATCH] [skip ci] try just allow date math everywhere --- .../data-mate/test/column/column-date-spec.ts | 1 + .../data-mate/test/data-frame-search-spec.ts | 44 +++++++++++++++++++ packages/utils/src/dates.ts | 11 +++-- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/packages/data-mate/test/column/column-date-spec.ts b/packages/data-mate/test/column/column-date-spec.ts index e87dc6cc618..14bff001e94 100644 --- a/packages/data-mate/test/column/column-date-spec.ts +++ b/packages/data-mate/test/column/column-date-spec.ts @@ -256,6 +256,7 @@ describe('Column (Date Types)', () => { }); it('should fail when transforming using toDate', () => { + // FIXME won't throw expect(() => { dataFrameAdapter( functionConfigRepository.toDate, diff --git a/packages/data-mate/test/data-frame-search-spec.ts b/packages/data-mate/test/data-frame-search-spec.ts index cf5a0a65cb0..cddc1ff4b85 100644 --- a/packages/data-mate/test/data-frame-search-spec.ts +++ b/packages/data-mate/test/data-frame-search-spec.ts @@ -191,6 +191,9 @@ describe('DataFrame->search', () => { date: '1999-12-01T00:00:00.000Z', location: '33.435967, -111.867710' }, + { + date: 'now-1y/d', + }, ], { name: 'special', metadata: { @@ -537,6 +540,16 @@ describe('DataFrame->search', () => { ]); }); + it('should be able to match using a date math', () => { + const resultFrame = specialDataFrame + .search('date:"now-1y/d"') + .select('date'); + + expect(resultFrame.toJSON()).toEqual([ + { date: getNowMinus1Yr() }, + ]); + }); + it('should be able to match using a date values >= range', () => { const resultFrame = specialDataFrame .search('date:>="2000-01-04T00:00:00.000Z"') @@ -545,6 +558,19 @@ describe('DataFrame->search', () => { expect(resultFrame.toJSON()).toEqual([ { date: '2000-01-04T00:00:00.000Z' }, { date: '2002-01-02T00:00:00.000Z' }, + { date: getNowMinus1Yr() }, + ]); + }); + + it('should be able to match using a date math >= range', () => { + const resultFrame = specialDataFrame + .search('date:>"1999-12-01||+2d"') + .select('date'); + + expect(resultFrame.toJSON()).toEqual([ + { date: '2000-01-04T00:00:00.000Z' }, + { date: '2002-01-02T00:00:00.000Z' }, + { date: getNowMinus1Yr() }, ]); }); @@ -558,6 +584,18 @@ describe('DataFrame->search', () => { ]); }); + it('should be able to match using a date math range', () => { + const resultFrame = specialDataFrame + .search('date:["2000-01-04T04:12:22.123Z||/y" TO now]') + .select('date'); + + expect(resultFrame.toJSON()).toEqual([ + { date: '2000-01-04T00:00:00.000Z' }, + { date: '2002-01-02T00:00:00.000Z' }, + { date: getNowMinus1Yr() }, + ]); + }); + it('should be able to match using a geo point', () => { const resultFrame = specialDataFrame .search('location:geoDistance(point:"33.435518,-111.873616" distance:5000m)') @@ -585,3 +623,9 @@ describe('DataFrame->search', () => { ]); }); }); + +function getNowMinus1Yr() { + const _now = new Date(new Date().setUTCHours(0, 0, 0, 0)); + const years = _now.getUTCFullYear(); + return new Date(_now.setUTCFullYear(years - 1)).toISOString(); +} diff --git a/packages/utils/src/dates.ts b/packages/utils/src/dates.ts index 2dfa8c79753..3d48ebe5692 100644 --- a/packages/utils/src/dates.ts +++ b/packages/utils/src/dates.ts @@ -117,11 +117,9 @@ function _getValidDate(val: unknown): Date | false { /** * Coerces value into a valid date, returns false if it is invalid. - * If a relativeNow date is passed in it will have added support for - * providing a date by dateMath (i.e. now+1h, now-1m) - * and relative (i.e. 5 days, -1 year) + * Has added support for converting from date math (i.e. now+1h, now-1m, now+2d/y, 2021-01-01||+2d) */ -export function getValidDate(val: unknown, relativeNow?: Date): Date | false { +export function getValidDate(val: unknown, relativeNow = new Date()): Date | false { const validDate = _getValidDate(val); if (validDate) return validDate; @@ -149,7 +147,7 @@ function parseRelativeDate(input: unknown, now: Date): Date|false { */ function parseDateMath(value: string, now: Date): Date|false { try { - return getValidDate(new Date(parser.parse(value, now))); + return _getValidDate(new Date(parser.parse(value, now))); } catch (err) { return false; } @@ -235,7 +233,7 @@ export function getValidDateOrNumberOrThrow(val: unknown): Date|number { if (typeof val === 'number' && !Number.isInteger(val)) return val; if (isDateTuple(val)) return val[0]; - const date = getValidDate(val as any); + const date = getValidDate(val); if (date === false) { throw new TypeError(`Expected ${val} (${getTypeOf(val)}) to be in a standard date format`); } @@ -251,6 +249,7 @@ export function isValidDateInstance(val: unknown): val is Date { export function getTime(val?: DateInputTypes): number | false { if (val == null) return Date.now(); const result = getValidDate(val); + console.log('===res', val, result); if (result === false) return false; return result.getTime(); }