Skip to content

Commit

Permalink
feat: renamed turnOffAbbreviations to disableAbbreviations
Browse files Browse the repository at this point in the history
Breaking Change: Renamed "turnOffAbbreviations" to "disableAbbreviations"
  • Loading branch information
cchanxzy committed Dec 6, 2020
1 parent b16f577 commit 7751a43
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Examples:
- 2.5m = 2,500,000
- 3.456B = 3,456,000,000

This can be turned off by passing in `turnOffAbbreviations`.
This can be turned off by passing in `disableAbbreviations`.

## Separators

Expand Down Expand Up @@ -130,7 +130,7 @@ Example if `fixedDecimalLength` was 2:
| decimalSeparator | `string` | locale default | Separator between integer part and fractional part of value |
| groupSeparator | `string` | locale default | Separator between thousand, million and billion |
| intlConfig | `object` | | International locale config |
| turnOffAbbreviations | `boolean` | `false` | Disable abbreviations eg. 1k > 1,000, 2m > 2,000,000 |
| disableAbbreviations | `boolean` | `false` | Disable abbreviations eg. 1k > 1,000, 2m > 2,000,000 |
| disableGroupSeparators | `boolean` | `false` | Disable auto adding the group separator between values, eg. 1000 > 1,000 |

## Format values for display
Expand Down
4 changes: 2 additions & 2 deletions src/components/CurrencyInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const CurrencyInput: FC<CurrencyInputProps> = forwardRef<
intlConfig,
step,
disableGroupSeparators = false,
turnOffAbbreviations = false,
disableAbbreviations = false,
...props
}: CurrencyInputProps,
ref
Expand Down Expand Up @@ -74,7 +74,7 @@ export const CurrencyInput: FC<CurrencyInputProps> = forwardRef<
allowDecimals,
decimalsLimit: decimalsLimit || fixedDecimalLength || 2,
allowNegativeValue,
turnOffAbbreviations,
disableAbbreviations,
prefix,
};

Expand Down
6 changes: 4 additions & 2 deletions src/components/CurrencyInputProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export type CurrencyInputProps = Overwrite<
/**
* Value will always have the specified length of decimals
*
* This formatting happens onBlur
* Eg. 123 -> 1.23
*
* Note: This formatting only happens onBlur
*/
fixedDecimalLength?: number;

Expand Down Expand Up @@ -124,7 +126,7 @@ export type CurrencyInputProps = Overwrite<
*
* Default = false
*/
turnOffAbbreviations?: boolean;
disableAbbreviations?: boolean;

/**
* International locale config, examples:
Expand Down
6 changes: 3 additions & 3 deletions src/components/__tests__/CurrencyInput-abbreviated.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ describe('<CurrencyInput /> component > abbreviated', () => {
expect(view.update().find(`#${id}`).prop('value')).toBe('');
});

describe('turnOffAbbreviations', () => {
it('should not allow abbreviations if turnOffAbbreviations is true', () => {
const view = shallow(<CurrencyInput id={id} onChange={onChangeSpy} turnOffAbbreviations />);
describe('disableAbbreviations', () => {
it('should not allow abbreviations if disableAbbreviations is true', () => {
const view = shallow(<CurrencyInput id={id} onChange={onChangeSpy} disableAbbreviations />);
view.find(`#${id}`).simulate('change', { target: { value: '1k' } });
expect(view.update().find(`#${id}`).prop('value')).toBe('1');

Expand Down
20 changes: 10 additions & 10 deletions src/components/utils/__tests__/cleanValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,21 @@ describe('cleanValue', () => {
expect(
cleanValue({
value: 'k',
turnOffAbbreviations: true,
disableAbbreviations: true,
})
).toEqual('');

expect(
cleanValue({
value: 'm',
turnOffAbbreviations: true,
disableAbbreviations: true,
})
).toEqual('');

expect(
cleanValue({
value: 'b',
turnOffAbbreviations: true,
disableAbbreviations: true,
})
).toEqual('');
});
Expand All @@ -187,45 +187,45 @@ describe('cleanValue', () => {
cleanValue({
value: '$k',
prefix: '$',
turnOffAbbreviations: true,
disableAbbreviations: true,
})
).toEqual('');

expect(
cleanValue({
value: '£m',
prefix: '£',
turnOffAbbreviations: true,
disableAbbreviations: true,
})
).toEqual('');
});

it('should ignore abbreviations if turnOffAbbreviations is true', () => {
it('should ignore abbreviations if disableAbbreviations is true', () => {
expect(
cleanValue({
value: '1k',
turnOffAbbreviations: true,
disableAbbreviations: true,
})
).toEqual('1');

expect(
cleanValue({
value: '-2k',
turnOffAbbreviations: true,
disableAbbreviations: true,
})
).toEqual('-2');

expect(
cleanValue({
value: '25.6m',
turnOffAbbreviations: true,
disableAbbreviations: true,
})
).toEqual('25.6');

expect(
cleanValue({
value: '9b',
turnOffAbbreviations: true,
disableAbbreviations: true,
})
).toEqual('9');
});
Expand Down
8 changes: 4 additions & 4 deletions src/components/utils/cleanValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type CleanValueOptions = {
allowDecimals?: boolean;
decimalsLimit?: number;
allowNegativeValue?: boolean;
turnOffAbbreviations?: boolean;
disableAbbreviations?: boolean;
prefix?: string;
};

Expand All @@ -24,14 +24,14 @@ export const cleanValue = ({
allowDecimals = true,
decimalsLimit = 2,
allowNegativeValue = true,
turnOffAbbreviations = false,
disableAbbreviations = false,
prefix = '',
}: CleanValueOptions): string => {
if (value === '-') {
return value;
}

const abbreviations = turnOffAbbreviations ? [] : ['k', 'm', 'b'];
const abbreviations = disableAbbreviations ? [] : ['k', 'm', 'b'];
const isNegative = new RegExp(`^\\d?-${prefix ? `${escapeRegExp(prefix)}?` : ''}\\d`).test(value);

const [prefixWithValue, preValue] = RegExp(`(\\d+)-?${escapeRegExp(prefix)}`).exec(value) || [];
Expand All @@ -45,7 +45,7 @@ export const cleanValue = ({

let valueOnly = withoutInvalidChars;

if (!turnOffAbbreviations) {
if (!disableAbbreviations) {
// disallow letter without number
if (abbreviations.some((letter) => letter === withoutInvalidChars.toLowerCase())) {
return '';
Expand Down

0 comments on commit 7751a43

Please sign in to comment.