Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in formatter applied to histogram #211

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Add new hygen generators for components, models and slices [#171](https://github.com/CartoDB/carto-react-template/pull/171)
- Adapt to new **multi-package** structure for carto-react libs [#206](https://github.com/CartoDB/carto-react-lib/pull/206)
- Fix layer generator with second layer [#204](https://github.com/CartoDB/carto-react-template/pull/204)
- Fix bug in formatter applied to histogram [#211](https://github.com/CartoDB/carto-react-template/pull/211)

## 1.0.0-beta12 (2021-02-08)
- Refactor on basic JSX & JS stuff [#170](https://github.com/CartoDB/carto-react-template/pull/170)
Expand Down
27 changes: 19 additions & 8 deletions template-sample-app/template/src/utils/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import '@formatjs/intl-getcanonicallocales/polyfill';
import '@formatjs/intl-pluralrules/polyfill';
import '@formatjs/intl-pluralrules/locale-data/en';

/*
Note: `notation` & `compactDisplay` properties are not supported yet by Safari.
/*
Note: `notation` & `compactDisplay` properties are not supported yet by Safari.
Those require the use of a polyfill: https://www.npmjs.com/package/@formatjs/intl-numberformat
*/
import '@formatjs/intl-numberformat/polyfill';
Expand All @@ -26,10 +26,21 @@ export const currencyFormatter = (value) => {
};

export const numberFormatter = (value) => {
return Intl.NumberFormat('en-US', {
maximumFractionDigits: 1,
minimumFractionDigits: 0,
notation: 'compact',
compactDisplay: 'short',
}).format(isNaN(value) ? 0 : value);
const _value = parseLogicalOperation(value);
return (
_value.operation +
Intl.NumberFormat('en-US', {
maximumFractionDigits: 1,
minimumFractionDigits: 0,
notation: 'compact',
compactDisplay: 'short',
}).format(_value.value)
);
};

const parseLogicalOperation = (value) => {
if (!isNaN(value)) return { value: value, operation: '' };

const _value = value.replace('>', '');
return isNaN(_value) ? { value: 0, operation: '' } : { value: _value, operation: '> ' };
};
27 changes: 19 additions & 8 deletions template-skeleton/template/src/utils/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import '@formatjs/intl-getcanonicallocales/polyfill';
import '@formatjs/intl-pluralrules/polyfill';
import '@formatjs/intl-pluralrules/locale-data/en';

/*
Note: `notation` & `compactDisplay` properties are not supported yet by Safari.
/*
Note: `notation` & `compactDisplay` properties are not supported yet by Safari.
Those require the use of a polyfill: https://www.npmjs.com/package/@formatjs/intl-numberformat
*/
import '@formatjs/intl-numberformat/polyfill';
Expand All @@ -26,10 +26,21 @@ export const currencyFormatter = (value) => {
};

export const numberFormatter = (value) => {
return Intl.NumberFormat('en-US', {
maximumFractionDigits: 1,
minimumFractionDigits: 0,
notation: 'compact',
compactDisplay: 'short',
}).format(value);
const _value = parseLogicalOperation(value);
return (
_value.operation +
Intl.NumberFormat('en-US', {
maximumFractionDigits: 1,
minimumFractionDigits: 0,
notation: 'compact',
compactDisplay: 'short',
}).format(_value.value)
);
};

const parseLogicalOperation = (value) => {
if (!isNaN(value)) return { value: value, operation: '' };

const _value = value.replace('>', '');
return isNaN(_value) ? { value: 0, operation: '' } : { value: _value, operation: '> ' };
};