Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kristw committed Feb 25, 2019
1 parent 5dccdad commit e48cfa5
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-magic-numbers */

import { format as d3Format } from 'd3-format';
import NumberFormatter from '../NumberFormatter';

Expand All @@ -12,29 +14,28 @@ export default function createSmartNumberFormatter(
const { description, signed = false, id, label } = config;
const siFormatter = d3Format(`.3~s`);
const float2PointFormatter = d3Format(`.2~f`);
const float3PointFormatter = d3Format(`.3~f`);
const float4PointFormatter = d3Format(`.4~f`);

const getSign = signed ? () => '' : (value: number) => (value > 0 ? '+' : '');
const getSign = signed ? (value: number) => (value > 0 ? '+' : '') : () => '';

function formatValue(value: number) {
if (value === 0) {
return '0';
} else {
const absoluteValue = Math.abs(value);
if (absoluteValue >= 1000) {
// Normal human being are more familiar
// with billion (B) that giga (G)
return siFormatter(value).replace('G', 'B');
} else if (absoluteValue >= 1) {
return float2PointFormatter(value);
} else if (absoluteValue >= 0.001) {
return float3PointFormatter(value);
} else if (absoluteValue > 0.000001) {
return `${siFormatter(value * 100000)}µ`;
}

return siFormatter(value);
}
const absoluteValue = Math.abs(value);
if (absoluteValue >= 1000) {
// Normal human being are more familiar
// with billion (B) that giga (G)
return siFormatter(value).replace('G', 'B');
} else if (absoluteValue >= 1) {
return float2PointFormatter(value);
} else if (absoluteValue >= 0.001) {
return float4PointFormatter(value);
} else if (absoluteValue > 0.000001) {
return `${siFormatter(value * 1000000)}µ`;
}

return siFormatter(value);
}

return new NumberFormatter({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import NumberFormatter from '../../src/NumberFormatter';
import createSmartNumberFormatter from '../../src/factories/createSmartNumberFormatter';

describe('createSmartNumberFormatter(options)', () => {
it('creates an instance of NumberFormatter', () => {
const formatter = createSmartNumberFormatter();
expect(formatter).toBeInstanceOf(NumberFormatter);
});
describe('using default options', () => {
const formatter = createSmartNumberFormatter();
it('formats 0 correctly', () => {
expect(formatter(0)).toBe('0');
});
describe('for positive numbers', () => {
it('formats billion with B in stead of G', () => {
expect(formatter(1000000000)).toBe('1B');
expect(formatter(4560000000)).toBe('4.56B');
});
it('formats numbers that are >= 1,000 & <= 1,000,000,000 as SI format with precision 3', () => {
expect(formatter(1000)).toBe('1k');
expect(formatter(10001)).toBe('10k');
expect(formatter(10100)).toBe('10.1k');
expect(formatter(111000000)).toBe('111M');
});
it('formats number that are >= 1 & < 1,000 as integer or float with at most 2 decimal points', () => {
expect(formatter(1)).toBe('1');
expect(formatter(1.0)).toBe('1');
expect(formatter(10)).toBe('10');
expect(formatter(10.0)).toBe('10');
expect(formatter(10.23432)).toBe('10.23');
expect(formatter(274.2856)).toBe('274.29');
expect(formatter(999)).toBe('999');
});
it('formats numbers that are < 1 & >= 0.001 as float with at most 4 decimal points', () => {
expect(formatter(0.1)).toBe('0.1');
expect(formatter(0.23)).toBe('0.23');
expect(formatter(0.699)).toBe('0.699');
expect(formatter(0.0023)).toBe('0.0023');
expect(formatter(0.002300001)).toBe('0.0023');
});
it('formats numbers that are < 0.001 & >= 0.000001 as micron', () => {
expect(formatter(0.0002300001)).toBe('230µ');
expect(formatter(0.000023)).toBe('23µ');
expect(formatter(0.000001)).toBe('1µ');
});
it('formats numbers that are less than 0.000001 as SI format with precision 3', () => {
expect(formatter(0.0000001)).toBe('100n');
});
});
describe('for negative numbers', () => {
it('formats billion with B in stead of G', () => {
expect(formatter(-1000000000)).toBe('-1B');
expect(formatter(-4560000000)).toBe('-4.56B');
});
it('formats numbers that are >= 1,000 & <= 1,000,000,000 as SI format with precision 3', () => {
expect(formatter(-1000)).toBe('-1k');
expect(formatter(-10001)).toBe('-10k');
expect(formatter(-10100)).toBe('-10.1k');
expect(formatter(-111000000)).toBe('-111M');
});
it('formats number that are >= 1 & < 1,000 as integer or float with at most 2 decimal points', () => {
expect(formatter(-1)).toBe('-1');
expect(formatter(-1.0)).toBe('-1');
expect(formatter(-10)).toBe('-10');
expect(formatter(-10.0)).toBe('-10');
expect(formatter(-10.23432)).toBe('-10.23');
expect(formatter(-274.2856)).toBe('-274.29');
expect(formatter(-999)).toBe('-999');
});
it('formats numbers that are < 1 & >= 0.001 as float with at most 4 decimal points', () => {
expect(formatter(-0.1)).toBe('-0.1');
expect(formatter(-0.23)).toBe('-0.23');
expect(formatter(-0.699)).toBe('-0.699');
expect(formatter(-0.0023)).toBe('-0.0023');
expect(formatter(-0.002300001)).toBe('-0.0023');
});
it('formats numbers that are < 0.001 & >= 0.000001 as micron', () => {
expect(formatter(-0.0002300001)).toBe('-230µ');
expect(formatter(-0.000023)).toBe('-23µ');
expect(formatter(-0.000001)).toBe('-1µ');
});
it('formats numbers that are less than 0.000001 as SI format with precision 3', () => {
expect(formatter(-0.0000001)).toBe('-100n');
});
});
});

describe('when options.signed is true, it adds + for positive numbers', () => {
const formatter = createSmartNumberFormatter({ signed: true });
it('formats 0 correctly', () => {
expect(formatter(0)).toBe('0');
});
describe('for positive numbers', () => {
it('formats billion with B in stead of G', () => {
expect(formatter(1000000000)).toBe('+1B');
expect(formatter(4560000000)).toBe('+4.56B');
});
it('formats numbers that are >= 1,000 & <= 1,000,000,000 as SI format with precision 3', () => {
expect(formatter(1000)).toBe('+1k');
expect(formatter(10001)).toBe('+10k');
expect(formatter(10100)).toBe('+10.1k');
expect(formatter(111000000)).toBe('+111M');
});
it('formats number that are >= 1 & < 1,000 as integer or float with at most 2 decimal points', () => {
expect(formatter(1)).toBe('+1');
expect(formatter(1.0)).toBe('+1');
expect(formatter(10)).toBe('+10');
expect(formatter(10.0)).toBe('+10');
expect(formatter(10.23432)).toBe('+10.23');
expect(formatter(274.2856)).toBe('+274.29');
expect(formatter(999)).toBe('+999');
});
it('formats numbers that are < 1 & >= 0.001 as float with at most 4 decimal points', () => {
expect(formatter(0.1)).toBe('+0.1');
expect(formatter(0.23)).toBe('+0.23');
expect(formatter(0.699)).toBe('+0.699');
expect(formatter(0.0023)).toBe('+0.0023');
expect(formatter(0.002300001)).toBe('+0.0023');
});
it('formats numbers that are < 0.001 & >= 0.000001 as micron', () => {
expect(formatter(0.0002300001)).toBe('+230µ');
expect(formatter(0.000023)).toBe('+23µ');
expect(formatter(0.000001)).toBe('+1µ');
});
it('formats numbers that are less than 0.000001 as SI format with precision 3', () => {
expect(formatter(0.0000001)).toBe('+100n');
});
});
});
});

0 comments on commit e48cfa5

Please sign in to comment.