Fix ByteSizeValue parser to be consistent with Elasticsearch (#120653) (#120828)

This commit is contained in:
Michael Dokolin 2021-12-08 22:04:16 +01:00 committed by GitHub
parent 1c75b10c8d
commit c46b52e9c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View file

@ -30,6 +30,11 @@ describe('parsing units', () => {
expect(ByteSizeValue.parse('1gb').getValueInBytes()).toBe(1073741824);
});
test('case insensitive units', () => {
expect(ByteSizeValue.parse('1KB').getValueInBytes()).toBe(1024);
expect(ByteSizeValue.parse('1Mb').getValueInBytes()).toBe(1024 * 1024);
});
test('throws an error when unsupported unit specified', () => {
expect(() => ByteSizeValue.parse('1tb')).toThrowErrorMatchingInlineSnapshot(
`"Failed to parse value as byte value. Value must be either number of bytes, or follow the format <count>[b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."`

View file

@ -22,7 +22,7 @@ function renderUnit(value: number, unit: string) {
export class ByteSizeValue {
public static parse(text: string): ByteSizeValue {
const match = /([1-9][0-9]*)(b|kb|mb|gb)/.exec(text);
const match = /([1-9][0-9]*)(b|kb|mb|gb)/i.exec(text);
if (!match) {
const number = Number(text);
if (typeof number !== 'number' || isNaN(number)) {
@ -35,7 +35,7 @@ export class ByteSizeValue {
}
const value = parseInt(match[1], 10);
const unit = match[2];
const unit = match[2].toLowerCase();
return new ByteSizeValue(value * unitMultiplier[unit]);
}