Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Added date time parsing for conditional formatting #1120

Merged
merged 3 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions __mocks__/dh-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,11 @@ class DateWrapper extends LongWrapper {

class TimeZone {
static getTimeZone(id) {
if (id == null || id === '') {
// Usually there would be a java.lang.IllegalArgumentException for any invalid id.
// We at least know that '' and undefined, so throw an error.
throw new Error('Unsupported time zone');
}
return { id };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,43 +220,58 @@ describe('getFormatColumns', () => {

describe('isDateConditionValid', () => {
const values = {
valid: '2023-02-23T11:46:31.000000000 NY',
invalid: 'blah',
valid: [
'2023-02-23T11:46:31.000000000 NY',
'2023-02-23T00:00:00 NY',
'2023-02-23 NY',
],
invalid: ['blah', '2023-02-23'],
empty: '',
undefined,
};

it.each([DateCondition.IS_NULL, DateCondition.IS_NOT_NULL])(
'should return true for null check conditions: %s',
const conditions = {
valueNotRequired: [DateCondition.IS_NULL, DateCondition.IS_NOT_NULL],
valueRequired: [
DateCondition.IS_AFTER,
DateCondition.IS_AFTER_OR_EQUAL,
DateCondition.IS_BEFORE_OR_EQUAL,
DateCondition.IS_BEFORE,
DateCondition.IS_EXACTLY,
DateCondition.IS_NOT_EXACTLY,
],
};

describe.each(conditions.valueNotRequired)(
'Not-Required condition: %s',
condition => {
const testValues = [
values.valid,
values.invalid,
it.each([
...values.valid,
...values.invalid,
values.empty,
values.undefined,
];

testValues.forEach(value => {
expect(isDateConditionValid(condition, value)).toBeTruthy();
])('should ignore value when not required: %s', testValue => {
expect(isDateConditionValid(condition, testValue)).toBeTruthy();
});
}
);

it.each([
DateCondition.IS_AFTER,
DateCondition.IS_AFTER_OR_EQUAL,
DateCondition.IS_BEFORE_OR_EQUAL,
DateCondition.IS_BEFORE,
DateCondition.IS_EXACTLY,
DateCondition.IS_NOT_EXACTLY,
])(
'should return false for empty value when condition requires it: %s',
describe.each(conditions.valueRequired)(
'Required condition: %s',
condition => {
const testValues = [values.empty, values.undefined];

testValues.forEach(value => {
expect(isDateConditionValid(condition, value)).toBeFalsy();
});
it.each([
[values.empty, false],
[values.undefined, false],
[values.invalid, false],
[values.valid, true],
] as const)(
'should return true only if value is valid date format: %s, %s',
(testValues, expected) => {
[testValues].flat().forEach(value => {
expect(isDateConditionValid(condition, value)).toEqual(expected);
});
}
);
}
);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Log from '@deephaven/log';
import { Column, CustomColumn } from '@deephaven/jsapi-shim';
import { TableUtils } from '@deephaven/jsapi-utils';
import { DateUtils, TableUtils } from '@deephaven/jsapi-utils';
import {
makeColumnFormatColumn,
makeRowFormatColumn,
Expand Down Expand Up @@ -717,9 +717,25 @@ export function isDateConditionValid(condition: DateCondition, value?: string) {
case DateCondition.IS_NOT_NULL:
return true;

default:
// Proper date validation will be addressed by Issue #1108
return value != null && value !== '';
default: {
const [dt, tz] = (value ?? '').split(' ');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One suggestion - use camel case and full words for variable names. We try to follow the Airbnb style guide – https://github.com/airbnb/javascript#naming-conventions
with a few exceptions. e as the catch block argument is fine, single-character names in loops and maps are also fine, as long as the loop body is fairly short.

In this case, these are local variables and the scope is short so it's probably fine, but I thought I'd mentioned this anyway.


try {
DateUtils.parseDateTimeString(dt);
} catch (e) {
log.debug('Invalid datetime string', dt);
return false;
}

try {
dh.i18n.TimeZone.getTimeZone(tz);
} catch (e) {
log.debug('Invalid timezone string', tz);
return false;
}

return true;
}
}
}

Expand Down