[bugfix] Display raw value in addition to ERROR (#6417)

* Display raw value in addition to ERROR

* update unit test
This commit is contained in:
Krist Wongsuphasawat 2018-11-26 10:44:42 -08:00 committed by Grace Guo
parent 7eb46843cb
commit 3c6e882d28
2 changed files with 5 additions and 6 deletions

View File

@ -29,7 +29,7 @@ describe('utils', () => {
expect(d3format('.3s', 1234)).toBe('1.23k');
expect(d3format('.3s', 1237)).toBe('1.24k');
expect(d3format('', 1237)).toBe('1.24k');
expect(d3format('.2efd2.ef.2.e', 1237)).toBe('ERROR');
expect(d3format('.2efd2.ef.2.e', 1237)).toBe('1237 (Invalid format: .2efd2.ef.2.e)');
});
});
@ -48,7 +48,7 @@ describe('utils', () => {
});
it('returns a working formatter', () => {
expect(d3FormatPreset('smart_date')(0)).toBe('1970');
expect(d3FormatPreset('%%GIBBERISH')(0)).toBe('ERROR');
expect(d3FormatPreset('%%GIBBERISH')(0)).toBe('0 (Invalid format: %%GIBBERISH)');
});
});

View File

@ -6,7 +6,6 @@ import { timeFormat as d3TimeFormat } from 'd3-time-format';
import { formatDate, UTC } from './dates';
const siFormatter = d3Format('.3s');
const ERROR_STRING = 'ERROR';
export function defaultNumberFormatter(n) {
let si = siFormatter(n);
@ -28,7 +27,7 @@ export function d3FormatPreset(format) {
} catch (e) {
// eslint-disable-next-line no-console
console.warn(e);
return () => ERROR_STRING;
return value => `${value} (Invalid format: ${format})`;
}
}
return defaultNumberFormatter;
@ -57,13 +56,13 @@ export function d3format(format, number) {
} catch (e) {
// eslint-disable-next-line no-console
console.warn(e);
return ERROR_STRING;
return `${number} (Invalid format: ${format})`;
}
}
try {
return formatters[format](number);
} catch (e) {
return ERROR_STRING;
return `${number} (Invalid format: ${format})`;
}
}