Use @superset-ui/number-format and @superset-ui/time-format for formatting. (#6470)

refactor: proxy all d3 number and time formatting calls
This commit is contained in:
Krist Wongsuphasawat
2018-12-04 13:24:07 -08:00
committed by GitHub
parent cc3a625a4b
commit fcec748b62
29 changed files with 162 additions and 346 deletions

View File

@@ -1,60 +1,10 @@
import {
tickMultiFormat,
formatDate,
formatDateVerbose,
fDuration,
now,
epochTimeXHoursAgo,
epochTimeXDaysAgo,
epochTimeXYearsAgo,
} from '../../../src/modules/dates';
describe('tickMultiFormat', () => {
it('is a function', () => {
expect(typeof tickMultiFormat).toBe('function');
});
});
describe('formatDate', () => {
it('is a function', () => {
expect(typeof formatDate).toBe('function');
});
it('shows only year when 1st day of the year', () => {
expect(formatDate(new Date('2020-01-01'))).toBe('2020');
});
it('shows only month when 1st of month', () => {
expect(formatDate(new Date('2020-03-01'))).toBe('March');
});
it('does not show day of week when it is Sunday', () => {
expect(formatDate(new Date('2020-03-15'))).toBe('Mar 15');
});
it('shows weekday when it is not Sunday (and no ms/sec/min/hr)', () => {
expect(formatDate(new Date('2020-03-03'))).toBe('Tue 03');
});
});
describe('formatDateVerbose', () => {
it('is a function', () => {
expect(typeof formatDateVerbose).toBe('function');
});
it('shows only year when 1st day of the year', () => {
expect(formatDateVerbose(new Date('2020-01-01'))).toBe('2020');
});
it('shows month and year when 1st of month', () => {
expect(formatDateVerbose(new Date('2020-03-01'))).toBe('Mar 2020');
});
it('shows weekday when any day of the month', () => {
expect(formatDateVerbose(new Date('2020-03-03'))).toBe('Tue Mar 3');
expect(formatDateVerbose(new Date('2020-03-15'))).toBe('Sun Mar 15');
});
});
} from '../../../src/modules/dates';
describe('fDuration', () => {
it('is a function', () => {

View File

@@ -1,9 +1,5 @@
import {
formatSelectOptionsForRange,
d3format,
d3FormatPreset,
d3TimeFormatPreset,
defaultNumberFormatter,
mainMetric,
roundDecimal,
} from '../../../src/modules/utils';
@@ -25,54 +21,6 @@ describe('utils', () => {
});
});
describe('d3format', () => {
it('returns a string formatted number as specified', () => {
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('1237 (Invalid format: .2efd2.ef.2.e)');
});
});
describe('d3FormatPreset', () => {
it('is a function', () => {
expect(typeof d3FormatPreset).toBe('function');
});
it('returns a working formatter', () => {
expect(d3FormatPreset('.3s')(3000000)).toBe('3.00M');
});
});
describe('d3TimeFormatPreset', () => {
it('is a function', () => {
expect(typeof d3TimeFormatPreset).toBe('function');
});
it('returns a working formatter', () => {
expect(d3FormatPreset('smart_date')(0)).toBe('1970');
expect(d3FormatPreset('%%GIBBERISH')(0)).toBe('0 (Invalid format: %%GIBBERISH)');
});
});
describe('defaultNumberFormatter', () => {
expect(defaultNumberFormatter(10)).toBe('10');
expect(defaultNumberFormatter(1)).toBe('1');
expect(defaultNumberFormatter(1.0)).toBe('1');
expect(defaultNumberFormatter(10.0)).toBe('10');
expect(defaultNumberFormatter(10001)).toBe('10.0k');
expect(defaultNumberFormatter(10100)).toBe('10.1k');
expect(defaultNumberFormatter(111000000)).toBe('111M');
expect(defaultNumberFormatter(0.23)).toBe('230m');
expect(defaultNumberFormatter(-10)).toBe('-10');
expect(defaultNumberFormatter(-1)).toBe('-1');
expect(defaultNumberFormatter(-1.0)).toBe('-1');
expect(defaultNumberFormatter(-10.0)).toBe('-10');
expect(defaultNumberFormatter(-10001)).toBe('-10.0k');
expect(defaultNumberFormatter(-10101)).toBe('-10.1k');
expect(defaultNumberFormatter(-111000000)).toBe('-111M');
expect(defaultNumberFormatter(-0.23)).toBe('-230m');
});
describe('mainMetric', () => {
it('is null when no options', () => {
expect(mainMetric([])).toBeUndefined();

View File

@@ -1,11 +1,26 @@
import { formatLabel, tryNumify } from '../../../../src/visualizations/nvd3/utils';
import { getTimeOrNumberFormatter, formatLabel, tryNumify } from '../../../../src/visualizations/nvd3/utils';
describe('nvd3/utils', () => {
const verboseMap = {
foo: 'Foo',
bar: 'Bar',
};
describe('getTimeOrNumberFormatter(format)', () => {
it('is a function', () => {
expect(typeof getTimeOrNumberFormatter).toBe('function');
});
it('returns a date formatter if format is smart_date', () => {
const time = new Date(Date.UTC(2018, 10, 21, 22, 11));
expect(getTimeOrNumberFormatter('smart_date')(time)).toBe('10:11');
});
it('returns a number formatter otherwise', () => {
expect(getTimeOrNumberFormatter('.3s')(3000000)).toBe('3.00M');
expect(getTimeOrNumberFormatter()(3000100)).toBe('3.00M');
});
});
describe('formatLabel()', () => {
const verboseMap = {
foo: 'Foo',
bar: 'Bar',
};
it('formats simple labels', () => {
expect(formatLabel('foo')).toBe('foo');
expect(formatLabel(['foo'])).toBe('foo');
@@ -22,6 +37,7 @@ describe('nvd3/utils', () => {
expect(formatLabel(['foo', 'bar', 'baz', '2 hours offset'], verboseMap)).toBe('Foo, Bar, baz, 2 hours offset');
});
});
describe('tryNumify()', () => {
it('tryNumify works as expected', () => {
expect(tryNumify(5)).toBe(5);