TimeSeries: fix time comparer not comparing date strings properly (#64622)

* fix time comparer not comparing times properly

* move isDateTime last as it's probably the most expensive check
This commit is contained in:
Ashley Harrison 2023-03-13 09:31:27 +00:00 committed by GitHub
parent d6eea0c7b5
commit 3a1862f37f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 6 deletions

View File

@ -113,15 +113,14 @@ exports[`better eslint`] = {
],
"packages/grafana-data/src/datetime/moment_wrapper.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
[0, 0, 0, "Do not use any type assertions.", "1"],
[0, 0, 0, "Do not use any type assertions.", "2"],
[0, 0, 0, "Do not use any type assertions.", "3"],
[0, 0, 0, "Do not use any type assertions.", "4"],
[0, 0, 0, "Do not use any type assertions.", "5"],
[0, 0, 0, "Do not use any type assertions.", "6"],
[0, 0, 0, "Do not use any type assertions.", "7"],
[0, 0, 0, "Do not use any type assertions.", "8"],
[0, 0, 0, "Do not use any type assertions.", "9"]
[0, 0, 0, "Do not use any type assertions.", "8"]
],
"packages/grafana-data/src/datetime/parser.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"],

View File

@ -89,7 +89,18 @@ export const getLocaleData = (): DateTimeLocale => {
return moment.localeData();
};
export const isDateTime = (value: any): value is DateTime => {
export const isDateTimeInput = (value: unknown): value is DateTimeInput => {
return (
value === null ||
typeof value === 'string' ||
typeof value === 'number' ||
value instanceof Date ||
(Array.isArray(value) && value.every((v) => typeof v === 'string' || typeof v === 'number')) ||
isDateTime(value)
);
};
export const isDateTime = (value: unknown): value is DateTime => {
return moment.isMoment(value);
};

View File

@ -1,6 +1,6 @@
import { isNumber } from 'lodash';
import { dateTime, isDateTime } from '../datetime';
import { dateTime, isDateTimeInput } from '../datetime';
import { Field, FieldType } from '../types/dataFrame';
import { Vector } from '../types/vector';
@ -34,7 +34,7 @@ export const timeComparer = (a: unknown, b: unknown): number => {
return numericComparer(a, b);
}
if (isDateTime(a) && isDateTime(b)) {
if (isDateTimeInput(a) && isDateTimeInput(b)) {
if (dateTime(a).isBefore(b)) {
return -1;
}