From 191f904dfbfb271b875385d8cad43a8dfa7ad496 Mon Sep 17 00:00:00 2001 From: joaoGabriel55 Date: Thu, 10 Nov 2022 20:22:15 -0300 Subject: [PATCH] First commit --- docs/04-api.md | 46 +++++++++++++++++++++++++++++++++++++ src/currency.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ test/test.js | 49 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 154 insertions(+), 1 deletion(-) diff --git a/docs/04-api.md b/docs/04-api.md index 85923ef3..ee951cbc 100644 --- a/docs/04-api.md +++ b/docs/04-api.md @@ -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 )` diff --git a/src/currency.js b/src/currency.js index 1edc89fe..c2da597b 100644 --- a/src/currency.js +++ b/src/currency.js @@ -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. diff --git a/test/test.js b/test/test.js index ce1d38ca..8f3213a7 100644 --- a/test/test.js +++ b/test/test.js @@ -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); -}); \ No newline at end of file +}); + +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); +});