mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
datemath: Fixes state mutation by parseDateMath function (#74180)
This commit is contained in:
parent
2a2689a7c2
commit
ad00200a39
@ -101,7 +101,8 @@ exports[`better eslint`] = {
|
||||
],
|
||||
"packages/grafana-data/src/datetime/datemath.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, "Unexpected any. Specify a different type.", "2"]
|
||||
],
|
||||
"packages/grafana-data/src/datetime/durationutil.ts:5381": [
|
||||
[0, 0, 0, "Do not use any type assertions.", "0"]
|
||||
|
@ -181,6 +181,12 @@ describe('DateMath', () => {
|
||||
const date = dateMath.parseDateMath(' - 2d', dateTime([2014, 1, 5]));
|
||||
expect(date!.valueOf()).toEqual(dateTime([2014, 1, 3]).valueOf());
|
||||
});
|
||||
|
||||
it('should not mutate dateTime passed in', () => {
|
||||
const dateInput = dateTime([2014, 1, 5]);
|
||||
dateMath.parseDateMath(' - 2d', dateInput);
|
||||
expect(dateInput.valueOf()).toEqual(dateTime([2014, 1, 5]).valueOf());
|
||||
});
|
||||
});
|
||||
|
||||
describe('isMathString', () => {
|
||||
|
@ -110,7 +110,7 @@ export function parseDateMath(
|
||||
fiscalYearStartMonth = 0
|
||||
): DateTime | undefined {
|
||||
const strippedMathString = mathString.replace(/\s/g, '');
|
||||
const dateTime = time;
|
||||
const result = dateTime(time);
|
||||
let i = 0;
|
||||
const len = strippedMathString.length;
|
||||
|
||||
@ -118,7 +118,7 @@ export function parseDateMath(
|
||||
const c = strippedMathString.charAt(i++);
|
||||
let type;
|
||||
let num;
|
||||
let unit;
|
||||
let unitString: string;
|
||||
let isFiscal = false;
|
||||
|
||||
if (c === '/') {
|
||||
@ -152,34 +152,37 @@ export function parseDateMath(
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
unit = strippedMathString.charAt(i++);
|
||||
|
||||
if (unit === 'f') {
|
||||
unit = strippedMathString.charAt(i++);
|
||||
unitString = strippedMathString.charAt(i++);
|
||||
|
||||
if (unitString === 'f') {
|
||||
unitString = strippedMathString.charAt(i++);
|
||||
isFiscal = true;
|
||||
}
|
||||
|
||||
const unit = unitString as DurationUnit;
|
||||
|
||||
if (!includes(units, unit)) {
|
||||
return undefined;
|
||||
} else {
|
||||
if (type === 0) {
|
||||
if (isFiscal) {
|
||||
roundToFiscal(fiscalYearStartMonth, dateTime, unit, roundUp);
|
||||
roundToFiscal(fiscalYearStartMonth, result, unit, roundUp);
|
||||
} else {
|
||||
if (roundUp) {
|
||||
dateTime.endOf(unit);
|
||||
result.endOf(unit);
|
||||
} else {
|
||||
dateTime.startOf(unit);
|
||||
result.startOf(unit);
|
||||
}
|
||||
}
|
||||
} else if (type === 1) {
|
||||
dateTime.add(num, unit);
|
||||
result.add(num, unit);
|
||||
} else if (type === 2) {
|
||||
dateTime.subtract(num, unit);
|
||||
result.subtract(num, unit);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dateTime;
|
||||
return result;
|
||||
}
|
||||
|
||||
export function roundToFiscal(fyStartMonth: number, dateTime: any, unit: string, roundUp: boolean | undefined) {
|
||||
|
Loading…
Reference in New Issue
Block a user