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 currency formatting with MachineCurrencyFormat #205

Merged
merged 1 commit into from
Jan 13, 2025
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
5 changes: 2 additions & 3 deletions typescript-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,11 @@
"predeploy": "cd example && yarn install && yarn run build",
"prepare": "run-s build",
"start": "microbundle-crl watch --no-compress --format modern,cjs",
"test": "run-s test:unit test:lint test:build",
"test:build": "run-s build",
"test": "run-s test:unit test:lint",
"test:lint": "eslint .",
"test:unit": "cross-env CI=1 react-scripts test --env=jsdom",
"test:watch": "react-scripts test --env=jsdom"
},
"source": "src/index.ts",
"version": "1.9.14"
"version": "1.9.15"
}
10 changes: 10 additions & 0 deletions typescript-react/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

const problematicIndexJsPath = path.resolve(
__dirname,
'../node_modules/symbol-observable/es/index.js',
);
const fixedPath = problematicIndexJsPath.replace(/\.js$/, '.mjs');

try {
console.log('Running tests before proceeding with prebuild script...');
execSync('yarn test', { stdio: 'inherit' });
console.log('Tests passed successfully.');
} catch (error) {
console.error('Tests failed. Exiting prebuild script.');
process.exit(1);
}

console.log('Revenj: running prebuild script');
if (fs.existsSync(problematicIndexJsPath)) {
console.log(
Expand Down
3 changes: 2 additions & 1 deletion typescript-react/src/components/View/FiltersForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ const processOnSubmit = <T, R>(onSubmit: (data: R) => Promise<R>, formUnderKey?:

export class FiltersForm<T> extends React.PureComponent<IForm<T>> {
public render() {
const { children, configuration, formUnderKey, hideSubmit, initialValues } = this.props;
const { children, configuration, formUnderKey, hideSubmit, initialValues, ...rest } = this.props;

const child = (
<ListPresenterComponent>
{
(ctx) => (
<Form
{...rest}
form={ctx!.conceptName}
className={classNames(styles.Form, 'dslFiltersForm')}
initialValues={initialValues as any}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 1`] = `"Must be a whole number"`;

exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 2`] = `"Must be a whole number with up to 10 digits"`;
exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 2`] = `"Must be a whole number between 2,147,483,647 and -2,147,483,648"`;

exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 3`] = `"Must be a whole number with up to 10 digits"`;
exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 3`] = `"Must be a whole number between 2,147,483,647 and -2,147,483,648"`;

exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 4`] = `"Must be a whole number with up to 10 digits"`;
exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 4`] = `"Must be a whole number between 2,147,483,647 and -2,147,483,648"`;

exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 5`] = `"Must be a whole number"`;

exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 6`] = `"Must be a whole number"`;

exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 7`] = `"Must be a whole number"`;

exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 8`] = `"Must be a whole number with up to 10 digits"`;
exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 8`] = `"Must be a whole number between 2,147,483,647 and -2,147,483,648"`;

exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 9`] = `"Must be a whole number with up to 10 digits"`;
exports[`validation isIntegerCreator should return an error when value exists but is not treatable as an integer 9`] = `"Must be a whole number between 2,147,483,647 and -2,147,483,648"`;

exports[`validation isNumberCreator should return an error when value exists but is not treatable as a numeric value 1`] = `"Value must be a number."`;

Expand Down
4 changes: 2 additions & 2 deletions typescript-react/src/util/Formatters/CurrencyFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from './NumberFormatter';

export const CurrencyFormat = '#,##0.00';
export const MachineCurrencyFormat = '#0.00';

export enum CurrencySymbolPlacement {
Prefix = 'Prefix',
Expand All @@ -33,8 +34,7 @@ export class CurrencyFormatterClass {
}

machineFormatCurrency = (number: NumberUtils.Numeric) => {
const format = (this.currencyFormat || CurrencyFormat).replace(',', '');
return formatNumber(new MachineBigNumber(number).toString(), format);
return formatNumber(new MachineBigNumber(number).toString(), MachineCurrencyFormat);
}

formatNumber = (number: NumberUtils.Numeric): string => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,55 @@ describe('Currency Formatter', () => {
expect(machineFormatter('1')).toBe('1.00');
});
});

describe('machineFormatCurrency', () => {
const testCases = [
// Valid integers
{ input: '1', output: '1.00' },
{ input: '12', output: '12.00' },
{ input: '123', output: '123.00' },
{ input: '1234', output: '1234.00' },
{ input: '12345', output: '12345.00' },
{ input: '123456789', output: '123456789.00' },
// Valid zero and negative numbers
{ input: '0', output: '0.00' },
{ input: '-1', output: '-1.00' },
{ input: '-12345', output: '-12345.00' },
// Valid decimals
{ input: '1.1', output: '1.10' },
{ input: '1.123', output: '1.12' },
{ input: '1234.5678', output: '1234.57' },
{ input: '1000000000000.23', output: '1000000000000.23' },
// Input with leading zeros
{ input: '000123', output: '123.00' },
{ input: '0000', output: '0.00' },
// Input with trailing dot
{ input: '123.', output: '123.00' },
{ input: '123.0', output: '123.00' },
// Invalid inputs
{ input: '', output: '' },
{ input: 'abc', output: '' },
{ input: '12abc', output: '' },
{ input: '1.2.3', output: '' },
];

const formats = [
'#0',
'#,##0',
'#.##0',
'#,##0.00',
'#.##0,00',
'#,##,##0',
];

formats.forEach((format) => {
it(`should format correctly according to the ${format} format`, () => {
CurrencyFormatter.setFormat(format);

testCases.forEach(({ input, output }) => {
expect(CurrencyFormatter.machineFormatCurrency(input)).toBe(output);
});
});
});
});
});