-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from dleitee/convertTo
add convertCurrency and fix errors
- Loading branch information
Showing
16 changed files
with
114 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,7 @@ | |
"jest-babel": "^1.0.1" | ||
}, | ||
"dependencies": { | ||
"big.js": "^3.1.3", | ||
"intl": "^1.2.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { Money } from '../' | ||
|
||
export default (handler, wallet) => | ||
export default (handler, money) => | ||
new Money(handler(), { | ||
locale: wallet.locale, | ||
currency: wallet.currency, | ||
currencyFractionals: wallet.currencyFractionals, | ||
locale: money.locale, | ||
currency: money.currency, | ||
currencyFractionals: money.currencyFractionals, | ||
normalized: true, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import { normalize } from './normalization' | ||
|
||
export function sum(newValue) { | ||
return normalize(this.currencyFractionals, newValue) + this.value | ||
return normalize(this.currencyFractionals, newValue).add(this.value) | ||
} | ||
|
||
export function subtract(newValue) { | ||
return this.value - normalize(this.currencyFractionals, newValue) | ||
return this.value.minus(normalize(this.currencyFractionals, newValue)) | ||
} | ||
|
||
export function multiply(factor) { | ||
return this.value * factor | ||
return this.value.times(factor) | ||
} | ||
|
||
export function divide(factor) { | ||
return this.value / factor | ||
return this.value.div(factor) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import { Money } from '../src' | ||
|
||
test('should be returned a Money object', () => { | ||
expect(Money.fromString('100')).toMatchObject({ value: 10000 }) | ||
expect(Money.fromString('100').getValue()).toBe('100.00') | ||
}) | ||
|
||
test('should be returned a Money object with value = 0', () => { | ||
expect(Money.fromString()).toMatchObject({ value: 0, locale: 'en' }) | ||
expect(Money.fromString().getValue()).toBe('0.00') | ||
}) | ||
|
||
test('should be returned a Money object with value = 100 and locale pt-BR', () => { | ||
expect(Money.fromString(1, { locale: 'pt-BR' })).toMatchObject({ value: 100, locale: 'pt-BR' }) | ||
const money = Money.fromString(1, { locale: 'pt-BR' }) | ||
expect(money.getValue()).toBe('1.00') | ||
expect(money.getLocale()).toBe('pt-BR') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,99 @@ | ||
import { Money } from '../src' | ||
|
||
test('[add] should be returned a Money object with value = 200', () => { | ||
const initialValue = 100 | ||
const initialValue = '100.00' | ||
const amount = Money.init(initialValue) | ||
expect(amount.add(100).getValue()).toBe(200) | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.add(100).toString()).toBe('200.00') | ||
expect(amount.toString()).toBe(initialValue) | ||
}) | ||
|
||
test('[add] should be returned a Money object with small values', () => { | ||
const initialValue = 0.2 | ||
const initialValue = '0.20' | ||
const amount = Money.init(initialValue) | ||
expect(amount.add(0.1).getValue()).toBe(0.3) | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.add(0.1).toString()).toBe('0.30') | ||
expect(amount.toString()).toBe(initialValue) | ||
}) | ||
|
||
test('[add] should be returned a Money object with very small values', () => { | ||
const initialValue = 0.0000000002 | ||
const initialValue = '0.0000000002' | ||
const amount = Money.init(initialValue, { currencyFractionals: 10 }) | ||
expect(amount.add(0.0000000001).getValue()).toBe(0.0000000003) | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.add(0.0000000001).toString()).toBe('0.0000000003') | ||
expect(amount.toString()).toBe(initialValue) | ||
}) | ||
|
||
test('[add] should be returned a Money object with very very small values', () => { | ||
const initialValue = 0.00000000000000000009 | ||
const initialValue = '0.00000000000000000009' | ||
const amount = Money.init(initialValue, { currencyFractionals: 20 }) | ||
expect(amount.add('0.00000000000000000009').toString()).toBe('0.00000000000000000018') | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.toString()).toBe(initialValue) | ||
}) | ||
|
||
test('[add] should be returned a Money object with bigest values', () => { | ||
const initialValue = 99999999999999 | ||
const initialValue = '99999999999999' | ||
const amount = Money.init(initialValue) | ||
expect(amount.add(9999999999999).toString()).toBe('109,999,999,999,998.00') | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.toString()).toBe('99,999,999,999,999.00') | ||
}) | ||
|
||
test('[subtract] should be returned a Money object with value = 200', () => { | ||
const initialValue = 300 | ||
const initialValue = '300.00' | ||
const amount = Money.init(initialValue) | ||
expect(amount.subtract(100).getValue()).toBe(200) | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.subtract(100).toString()).toBe('200.00') | ||
expect(amount.toString()).toBe(initialValue) | ||
}) | ||
|
||
test('[subtract] should be returned a Money object with value = 0.1', () => { | ||
const initialValue = 0.3 | ||
const initialValue = '0.30' | ||
const amount = Money.init(initialValue) | ||
expect(amount.subtract(0.2).getValue()).toBe(0.1) | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.subtract(0.2).toString()).toBe('0.10') | ||
expect(amount.toString()).toBe(initialValue) | ||
}) | ||
|
||
test('[subtract] should be returned a Money object with value = 0.1', () => { | ||
const initialValue = 10100.03 | ||
const initialValue = '10100.03' | ||
const amount = Money.init(initialValue) | ||
expect(amount.subtract(0.2).getValue()).toBe(10099.83) | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.subtract(0.2).toString()).toBe('10,099.83') | ||
expect(amount.toString()).toBe('10,100.03') | ||
}) | ||
|
||
test('[multiply] with real values', () => { | ||
const initialValue = 2.02 | ||
const initialValue = '2.02' | ||
const amount = Money.init(initialValue, { currencyFractionals: 2 }) | ||
expect(amount.multiplyBy(3.15).toString()).toBe('6.36') | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.toString()).toBe(initialValue) | ||
}) | ||
|
||
test('[multiply] with smallest values', () => { | ||
const initialValue = 200.000000000002 | ||
const initialValue = '200.000000000002' | ||
const amount = Money.init(initialValue, { currencyFractionals: 12 }) | ||
expect(amount.multiplyBy(2).toString()).toBe('400.000000000004') | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.toString()).toBe(initialValue) | ||
}) | ||
|
||
test('[multiply] with biggest values', () => { | ||
const initialValue = 9999999999999.99 | ||
const initialValue = '9999999999999.99' | ||
const amount = Money.init(initialValue) | ||
expect(amount.multiplyBy(10).toString()).toBe('99,999,999,999,999.90') | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.toString()).toBe('9,999,999,999,999.99') | ||
}) | ||
|
||
test('[divide] with real values', () => { | ||
const initialValue = 2.02 | ||
const initialValue = '2.02' | ||
const amount = Money.init(initialValue, { currencyFractionals: 2 }) | ||
expect(amount.divideBy(2).toString()).toBe('1.01') | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.toString()).toBe(initialValue) | ||
}) | ||
|
||
test('[divide] with smallest values', () => { | ||
const initialValue = 200.000000000002 | ||
const initialValue = '200.000000000002' | ||
const amount = Money.init(initialValue, { currencyFractionals: 12 }) | ||
expect(amount.divideBy(2).toString()).toBe('100.000000000001') | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.toString()).toBe(initialValue) | ||
}) | ||
|
||
test('[divide] with biggest values', () => { | ||
const initialValue = 999999999999999.00 | ||
const initialValue = '99999999999999.00' | ||
const amount = Money.init(initialValue) | ||
expect(amount.divideBy(10).toString()).toBe('99,999,999,999,999.90') | ||
expect(amount.getValue()).toBe(initialValue) | ||
expect(amount.divideBy(10).toString()).toBe('9,999,999,999,999.90') | ||
expect(amount.toString()).toBe('99,999,999,999,999.00') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.