Skip to content

Commit

Permalink
fix(isPositive): return false with zero (#728)
Browse files Browse the repository at this point in the history
The function `isPositive` should return `false` when passing Dinero
objects with amount `0` or `-0`, as zero is unsigned—neither negative
nor positive.
  • Loading branch information
sarahdayan authored Feb 19, 2023
1 parent fc99ba9 commit 140fe68
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/api/isPositive.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { Calculator, Dinero } from '../types';
import { greaterThanOrEqual } from '../utils';
import { greaterThan } from '../utils';

export type IsPositiveParams<TAmount> = readonly [
dineroObject: Dinero<TAmount>
];

export function isPositive<TAmount>(calculator: Calculator<TAmount>) {
const greaterThanOrEqualFn = greaterThanOrEqual(calculator);
const greaterThanFn = greaterThan(calculator);

return function _isPositive(...[dineroObject]: IsPositiveParams<TAmount>) {
const { amount } = dineroObject.toJSON();

return greaterThanOrEqualFn(amount, calculator.zero());
return greaterThanFn(amount, calculator.zero());
};
}
18 changes: 12 additions & 6 deletions packages/dinero.js/src/api/__tests__/isNegative.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ describe('isNegative', () => {
expect(isNegative(d)).toBe(false);
});
it('returns false when amount is equal to 0', () => {
const d = dinero({ amount: 0, currency: USD });
const d1 = dinero({ amount: 0, currency: USD });
const d2 = dinero({ amount: -0, currency: USD });

expect(isNegative(d)).toBe(false);
expect(isNegative(d1)).toBe(false);
expect(isNegative(d2)).toBe(false);
});
});
describe('bigint', () => {
Expand All @@ -45,9 +47,11 @@ describe('isNegative', () => {
expect(isNegative(d)).toBe(false);
});
it('returns false when amount is equal to 0', () => {
const d = dinero({ amount: 0n, currency: bigintUSD });
const d1 = dinero({ amount: 0n, currency: bigintUSD });
const d2 = dinero({ amount: -0n, currency: bigintUSD });

expect(isNegative(d)).toBe(false);
expect(isNegative(d1)).toBe(false);
expect(isNegative(d2)).toBe(false);
});
});
describe('Big.js', () => {
Expand All @@ -65,9 +69,11 @@ describe('isNegative', () => {
expect(isNegative(d)).toBe(false);
});
it('returns false when amount is equal to 0', () => {
const d = dinero({ amount: new Big(0), currency: bigjsUSD });
const d1 = dinero({ amount: new Big(0), currency: bigjsUSD });
const d2 = dinero({ amount: new Big(-0), currency: bigjsUSD });

expect(isNegative(d)).toBe(false);
expect(isNegative(d1)).toBe(false);
expect(isNegative(d2)).toBe(false);
});
});
});
24 changes: 15 additions & 9 deletions packages/dinero.js/src/api/__tests__/isPositive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ describe('isPositive', () => {

expect(isPositive(d)).toBe(true);
});
it('returns true when amount is equal to 0', () => {
const d = dinero({ amount: 0, currency: USD });
it('returns false when amount is equal to 0', () => {
const d1 = dinero({ amount: 0, currency: USD });
const d2 = dinero({ amount: -0, currency: USD });

expect(isPositive(d)).toBe(true);
expect(isPositive(d1)).toBe(false);
expect(isPositive(d2)).toBe(false);
});
});
describe('bigint', () => {
Expand All @@ -44,10 +46,12 @@ describe('isPositive', () => {

expect(isPositive(d)).toBe(true);
});
it('returns true when amount is equal to 0', () => {
const d = dinero({ amount: 0n, currency: bigintUSD });
it('returns false when amount is equal to 0', () => {
const d1 = dinero({ amount: 0n, currency: bigintUSD });
const d2 = dinero({ amount: -0n, currency: bigintUSD });

expect(isPositive(d)).toBe(true);
expect(isPositive(d1)).toBe(false);
expect(isPositive(d2)).toBe(false);
});
});
describe('Big.js', () => {
Expand All @@ -64,10 +68,12 @@ describe('isPositive', () => {

expect(isPositive(d)).toBe(true);
});
it('returns true when amount is equal to 0', () => {
const d = dinero({ amount: new Big(0), currency: bigjsUSD });
it('returns false when amount is equal to 0', () => {
const d1 = dinero({ amount: new Big(0), currency: bigjsUSD });
const d2 = dinero({ amount: new Big(-0), currency: bigjsUSD });

expect(isPositive(d)).toBe(true);
expect(isPositive(d1)).toBe(false);
expect(isPositive(d2)).toBe(false);
});
});
});
2 changes: 1 addition & 1 deletion website/data/docs/api/comparisons/is-positive.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ import { USD } from '@dinero.js/currencies';

const d = dinero({ amount: 0, currency: USD });

isPositive(d); // true
isPositive(d); // false
```

1 comment on commit 140fe68

@vercel
Copy link

@vercel vercel bot commented on 140fe68 Feb 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

dinerojs – ./

v2.dinerojs.com
dinerojs-dinerojs.vercel.app
dinerojs-git-main-dinerojs.vercel.app

Please sign in to comment.