Skip to content

#411 Comparison of currency values #413

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions docs/04-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,52 @@ Divides the current currency instance by `number`.
currency(123.45).divide(2); // => "61.73"
```

### lessThan

`currency.lessThan( number|currency )`

Compares if value is less than another currency instance's value

```js
currency(10).lessThan(currency('10.1')); // => true
currency('10').lessThan(10.01); // => true
```

### lessThanOrEqual

`currency.lessThanOrEqual( number|currency )`

Compares if value is less than or equal to another currency instance's value

```js
currency(10).lessThanOrEqual(currency('10.1')); // => true
currency('10').lessThanOrEqual(10.01); // => true
currency('10').lessThanOrEqual(10); // => true
```

### greaterThan

`currency.greaterThan( number|currency )`

Compares if value is greater than another currency instance's value

```js
currency(10).greaterThan(currency('9.99')); // => true
currency('10').greaterThan(9.99); // => true
```

### greaterThanOrEqual

`currency.greaterThanOrEqual( number|currency )`

Compares if value is greater than or equal to another currency instance's value

```js
currency(10).greaterThanOrEqual(currency('9.99')); // => true
currency('10').greaterThanOrEqual(9.99); // => true
currency('9.99').greaterThanOrEqual(9.99); // => true
```

### distribute

`currency.distribute( number )`
Expand Down
60 changes: 60 additions & 0 deletions src/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,66 @@ currency.prototype = {
return currency(intValue /= parse(number, _settings, false), _settings);
},

/**
* Compares if value is less than another currency instance's value
* @param {number|currency} foreign
* @returns {boolean}
*/
lessThan(foreign) {
let { value } = this;
if (foreign instanceof currency) {
let { value: foreignValue } = foreign;

return value < foreignValue;
}
return value < foreign;
},

/**
* Compares if value is less than or equal to another currency instance's value
* @param {number|currency} foreign
* @returns {boolean}
*/
lessThanOrEqual(foreign) {
let { value } = this;
if (foreign instanceof currency) {
let { value: foreignValue } = foreign;

return value <= foreignValue;
}
return value < foreign;
},

/**
* Compares if value is greater than another currency instance's value
* @param {number|currency} foreign
* @returns {boolean}
*/
greaterThan(foreign) {
let { value } = this;
if (foreign instanceof currency) {
let { value: foreignValue } = foreign;

return value >= foreignValue;
}
return value >= foreign;
},

/**
* Compares if value is greater than or equal to another currency instance's value
* @param {number|currency} foreign
* @returns {boolean}
*/
greaterThanOrEqual(foreign) {
let { value } = this;
if (foreign instanceof currency) {
let { value: foreignValue } = foreign;

return value >= foreignValue;
}
return value >= foreign;
},

/**
* Takes the currency amount and distributes the values evenly. Any extra pennies
* left over from the distribution will be stacked onto the first set of entries.
Expand Down
49 changes: 48 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,51 @@ test('should handle fractional cents', t => {
var values = currency(1234.56, { fromCents: true });
t.is(values.intValue, 1235);
t.is(values.value, 12.35);
});
});

test('lessThan', t => {
let c1 = currency('10');
let c2 = currency(10);

t.is(c1.lessThan(currency('10.1')), true);
t.is(c1.lessThan(10.01), true);
t.is(c2.lessThan(currency('10.1')), true);
t.is(c2.lessThan(10.01), true);
});

test('lessThanOrEqual', t => {
let c1 = currency('10');
let c2 = currency(10);

t.is(c1.lessThanOrEqual(currency('10')), true);
t.is(c1.lessThanOrEqual(10.01), true);
t.is(c1.lessThanOrEqual('10.01'), true);

t.is(c2.lessThanOrEqual(currency('10')), true);
t.is(c2.lessThanOrEqual(10.01), true);
t.is(c2.lessThanOrEqual('10.01'), true);
});

test('greaterThan', t => {
let c1 = currency('10');
let c2 = currency(10);

t.is(c1.greaterThan(currency('9.99')), true);
t.is(c1.greaterThan(9.99), true);

t.is(c2.greaterThan(currency('9.99')), true);
t.is(c2.greaterThan(9.99), true);
});

test('greaterThanOrEqual', t => {
let c1 = currency('10');
let c2 = currency(10);

t.is(c1.greaterThanOrEqual(currency('10')), true);
t.is(c1.greaterThanOrEqual(9.99), true);
t.is(c1.greaterThanOrEqual('9.99'), true);

t.is(c2.greaterThanOrEqual(currency('10')), true);
t.is(c2.greaterThanOrEqual(9.99), true);
t.is(c2.greaterThanOrEqual('9.99'), true);
});