datemath: Fixes state mutation by parseDateMath function (#74180)

This commit is contained in:
Torkel Ödegaard 2023-08-31 17:09:59 +02:00 committed by GitHub
parent 2a2689a7c2
commit ad00200a39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 12 deletions

View File

@ -101,7 +101,8 @@ exports[`better eslint`] = {
], ],
"packages/grafana-data/src/datetime/datemath.ts:5381": [ "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.", "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": [ "packages/grafana-data/src/datetime/durationutil.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"] [0, 0, 0, "Do not use any type assertions.", "0"]

View File

@ -181,6 +181,12 @@ describe('DateMath', () => {
const date = dateMath.parseDateMath(' - 2d', dateTime([2014, 1, 5])); const date = dateMath.parseDateMath(' - 2d', dateTime([2014, 1, 5]));
expect(date!.valueOf()).toEqual(dateTime([2014, 1, 3]).valueOf()); 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', () => { describe('isMathString', () => {

View File

@ -110,7 +110,7 @@ export function parseDateMath(
fiscalYearStartMonth = 0 fiscalYearStartMonth = 0
): DateTime | undefined { ): DateTime | undefined {
const strippedMathString = mathString.replace(/\s/g, ''); const strippedMathString = mathString.replace(/\s/g, '');
const dateTime = time; const result = dateTime(time);
let i = 0; let i = 0;
const len = strippedMathString.length; const len = strippedMathString.length;
@ -118,7 +118,7 @@ export function parseDateMath(
const c = strippedMathString.charAt(i++); const c = strippedMathString.charAt(i++);
let type; let type;
let num; let num;
let unit; let unitString: string;
let isFiscal = false; let isFiscal = false;
if (c === '/') { if (c === '/') {
@ -152,34 +152,37 @@ export function parseDateMath(
return undefined; return undefined;
} }
} }
unit = strippedMathString.charAt(i++);
if (unit === 'f') { unitString = strippedMathString.charAt(i++);
unit = strippedMathString.charAt(i++);
if (unitString === 'f') {
unitString = strippedMathString.charAt(i++);
isFiscal = true; isFiscal = true;
} }
const unit = unitString as DurationUnit;
if (!includes(units, unit)) { if (!includes(units, unit)) {
return undefined; return undefined;
} else { } else {
if (type === 0) { if (type === 0) {
if (isFiscal) { if (isFiscal) {
roundToFiscal(fiscalYearStartMonth, dateTime, unit, roundUp); roundToFiscal(fiscalYearStartMonth, result, unit, roundUp);
} else { } else {
if (roundUp) { if (roundUp) {
dateTime.endOf(unit); result.endOf(unit);
} else { } else {
dateTime.startOf(unit); result.startOf(unit);
} }
} }
} else if (type === 1) { } else if (type === 1) {
dateTime.add(num, unit); result.add(num, unit);
} else if (type === 2) { } 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) { export function roundToFiscal(fyStartMonth: number, dateTime: any, unit: string, roundUp: boolean | undefined) {