mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Explore: Fixes crash when parsing date math string with whitespace (#17446)
Fixes: #16257
This commit is contained in:
parent
a9da7b5f1f
commit
8a602e86f3
@ -129,5 +129,10 @@ describe('DateMath', () => {
|
|||||||
const date = dateMath.parseDateMath('2', dateTime([2014, 1, 5]));
|
const date = dateMath.parseDateMath('2', dateTime([2014, 1, 5]));
|
||||||
expect(date).toEqual(undefined);
|
expect(date).toEqual(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should strip whitespace from string', () => {
|
||||||
|
const date = dateMath.parseDateMath(' - 2d', dateTime([2014, 1, 5]));
|
||||||
|
expect(date!.valueOf()).toEqual(dateTime([2014, 1, 3]).valueOf());
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -87,12 +87,13 @@ export function isValid(text: string | DateTime): boolean {
|
|||||||
*/
|
*/
|
||||||
// TODO: Had to revert Andrejs `time: moment.Moment` to `time: any`
|
// TODO: Had to revert Andrejs `time: moment.Moment` to `time: any`
|
||||||
export function parseDateMath(mathString: string, time: any, roundUp?: boolean): DateTime | undefined {
|
export function parseDateMath(mathString: string, time: any, roundUp?: boolean): DateTime | undefined {
|
||||||
|
const strippedMathString = mathString.replace(/\s/g, '');
|
||||||
const dateTime = time;
|
const dateTime = time;
|
||||||
let i = 0;
|
let i = 0;
|
||||||
const len = mathString.length;
|
const len = strippedMathString.length;
|
||||||
|
|
||||||
while (i < len) {
|
while (i < len) {
|
||||||
const c = mathString.charAt(i++);
|
const c = strippedMathString.charAt(i++);
|
||||||
let type;
|
let type;
|
||||||
let num;
|
let num;
|
||||||
let unit;
|
let unit;
|
||||||
@ -107,19 +108,19 @@ export function parseDateMath(mathString: string, time: any, roundUp?: boolean):
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNaN(parseInt(mathString.charAt(i), 10))) {
|
if (isNaN(parseInt(strippedMathString.charAt(i), 10))) {
|
||||||
num = 1;
|
num = 1;
|
||||||
} else if (mathString.length === 2) {
|
} else if (strippedMathString.length === 2) {
|
||||||
num = mathString.charAt(i);
|
num = strippedMathString.charAt(i);
|
||||||
} else {
|
} else {
|
||||||
const numFrom = i;
|
const numFrom = i;
|
||||||
while (!isNaN(parseInt(mathString.charAt(i), 10))) {
|
while (!isNaN(parseInt(strippedMathString.charAt(i), 10))) {
|
||||||
i++;
|
i++;
|
||||||
if (i > 10) {
|
if (i > 10) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
num = parseInt(mathString.substring(numFrom, i), 10);
|
num = parseInt(strippedMathString.substring(numFrom, i), 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 0) {
|
if (type === 0) {
|
||||||
@ -128,7 +129,7 @@ export function parseDateMath(mathString: string, time: any, roundUp?: boolean):
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unit = mathString.charAt(i++);
|
unit = strippedMathString.charAt(i++);
|
||||||
|
|
||||||
if (!includes(units, unit)) {
|
if (!includes(units, unit)) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
Loading…
Reference in New Issue
Block a user