Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
feat(formatters): Added maxPrecision parameter to count formatters to…
Browse files Browse the repository at this point in the history
… control the amount of decimals places
  • Loading branch information
dominikmessner authored and ffriedl89 committed Jul 20, 2020
1 parent 6d1e93d commit bac398f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 12 deletions.
16 changes: 9 additions & 7 deletions libs/barista-components/formatters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,21 @@ fields, which are described in a table below:
The `formatCount` function provides a way to format numbers as abbreviations
outside the template. The function takes the following parameters:

| Name | Type | Default | Description |
| ----------- | --------------------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------- |
| `input` | `DtFormattedValue | number` | | numeric value to be transformed by the pipe |
| `inputUnit` | `DtUnit | string` | `Unit.COUNT` | input unit, if not default - displayed together with the formatted value; does not yet support plurals and internationalization |
| Name | Type | Default | Description |
| -------------- | --------------------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------- |
| `input` | `DtFormattedValue | number` | | numeric value to be transformed by the pipe |
| `inputUnit` | `DtUnit | string` | `Unit.COUNT` | input unit, if not default - displayed together with the formatted value; does not yet support plurals and internationalization |
| `maxPrecision` | `number` | 3 | maximum amount of digits to be used |

### Percent

The `formatPercent` function provides a way to format percents, adjusting
precision outside the template. The function takes the following parameters:

| Name | Type | Default | Description |
| ------- | -------- | ------- | ------------------------------------------- |
| `input` | `number` | | numeric value to be transformed by the pipe |
| Name | Type | Default | Description |
| -------------- | -------- | ------- | ------------------------------------------- |
| `input` | `number` | | numeric value to be transformed by the pipe |
| `maxPrecision` | `number` | 3 | maximum amount of digits to be used |

### Bits

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import { DtUnit } from '../unit';
* @param input - numeric value to be transformed
* @param inputUnit - input unit, typically defined unit of type DtUnit (DtUnit.COUNT by default), custom strings are also allowed
* value is used only as a reference in case an additional rate pipe is used
* @param maxPrecision - The maximum amount of digits to be used, if provided
*/
export function formatCount(
input: DtFormattedValue | number,
inputUnit: DtUnit | string = DtUnit.COUNT,
maxPrecision?: number,
): DtFormattedValue {
const sourceData: SourceData =
input instanceof DtFormattedValue
Expand All @@ -43,7 +45,7 @@ export function formatCount(
const formattedData = !isNaN(value)
? {
transformedValue: value,
displayValue: adjustNumber(value, true),
displayValue: adjustNumber(value, true, maxPrecision),
displayUnit: inputUnit !== DtUnit.COUNT ? inputUnit : undefined,
displayRateUnit:
input instanceof DtFormattedValue
Expand Down
42 changes: 42 additions & 0 deletions libs/barista-components/formatters/src/count/count.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('DtCount', () => {
interface TestCase {
input: number;
inputUnit: DtUnit | string;
maxPrecision?: number;
output: string;
}

Expand Down Expand Up @@ -115,4 +116,45 @@ describe('DtCount', () => {
expect(pipe.transform(0).toString()).toEqual('0');
});
});

describe('Transforming input with max precision', () => {
[
{
input: 0.50234,
inputUnit: DtUnit.COUNT,
maxPrecision: 0,
output: '< 1',
},
{
input: 1.50234,
inputUnit: DtUnit.COUNT,
maxPrecision: 0,
output: '2',
},
{
input: 20000001,
inputUnit: DtUnit.COUNT,
maxPrecision: 0,
output: '20mil',
},
{
input: 0.50234,
inputUnit: DtUnit.COUNT,
maxPrecision: 5,
output: '0.50234',
},
].forEach((testCase: TestCase) => {
it(`should display ${testCase.input} without unit`, () => {
expect(
pipe
.transform(
testCase.input,
testCase.inputUnit,
testCase.maxPrecision,
)
.toString(),
).toEqual(testCase.output);
});
});
});
});
6 changes: 4 additions & 2 deletions libs/barista-components/formatters/src/count/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ export class DtCount implements PipeTransform {
/**
* @param input - The value to be formatted as an abbreviation
* @param inputUnit - The unit for the input number. Default is DtUnit.COUNT
* @param maxPrecision - The maximum amount of digits to be used, if provided
*/
transform(
// tslint:disable-next-line:no-any
input: any,
inputUnit: DtUnit | string = DtUnit.COUNT,
maxPrecision?: number,
): DtFormattedValue | string {
if (isEmpty(input)) {
return NO_DATA;
}
if (input instanceof DtFormattedValue) {
return formatCount(input, inputUnit);
return formatCount(input, inputUnit, maxPrecision);
}
if (isNumberLike(input)) {
return formatCount(coerceNumberProperty(input), inputUnit);
return formatCount(coerceNumberProperty(input), inputUnit, maxPrecision);
}

return NO_DATA;
Expand Down
10 changes: 10 additions & 0 deletions libs/barista-components/formatters/src/number-formatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ describe('FormatterUtil', () => {
input: 0.123456789,
output: '0.123',
},
{
input: 0.123456789,
maxPrecision: 0,
output: '< 1',
},
{
input: 0.123456789,
maxPrecision: 1,
Expand Down Expand Up @@ -226,6 +231,11 @@ describe('FormatterUtil', () => {
input: 0.0001,
output: '< 0.001',
},
{
input: 0.0001,
maxPrecision: 0,
output: '< 1',
},
{
input: 0.0001,
maxPrecision: 1,
Expand Down
4 changes: 2 additions & 2 deletions libs/barista-components/formatters/src/number-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function adjustPrecision(value: number, maxPrecision?: number): string {
const calcValue = Math.abs(value);
const minValue =
1 /
Math.pow(10, Math.max(maxPrecision || DEFAULT_PRECISION_FOR_MIN_VALUE, 0));
Math.pow(10, Math.max(maxPrecision ?? DEFAULT_PRECISION_FOR_MIN_VALUE, 0));

let digits = 0;
if (calcValue === 0) {
Expand All @@ -71,7 +71,7 @@ function adjustPrecision(value: number, maxPrecision?: number): string {
} else {
return `< ${minValue}`;
}
} else if (maxPrecision) {
} else if (maxPrecision !== undefined) {
digits = maxPrecision;
} else if (calcValue < 1) {
digits = 3;
Expand Down

0 comments on commit bac398f

Please sign in to comment.