Skip to content

Commit

Permalink
[skip ci] try just allow date math everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
lesleydreyer committed Mar 27, 2024
1 parent 21080af commit ae091e1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/data-mate/test/column/column-date-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ describe('Column (Date Types)', () => {
});

it('should fail when transforming using toDate', () => {
// FIXME won't throw
expect(() => {
dataFrameAdapter(
functionConfigRepository.toDate,
Expand Down
44 changes: 44 additions & 0 deletions packages/data-mate/test/data-frame-search-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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"')
Expand All @@ -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() },
]);
});

Expand All @@ -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)')
Expand Down Expand Up @@ -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();
}
11 changes: 5 additions & 6 deletions packages/utils/src/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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`);
}
Expand All @@ -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();
}
Expand Down

0 comments on commit ae091e1

Please sign in to comment.