Skip to content

Commit

Permalink
Merge 32e83ff into 00ac4fd
Browse files Browse the repository at this point in the history
  • Loading branch information
scotttrinh committed Apr 20, 2018
2 parents 00ac4fd + 32e83ff commit cc09a56
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
29 changes: 25 additions & 4 deletions src/dinero.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,26 @@ const Dinero = options => {
if (!Number.isInteger(number)) {
throw new TypeError('You must provide an integer.')
}
},
isValidPrecision(number) {
if (number < 1 || number > 15) {
throw new TypeError('You must provide an integer between 1 and 15.')
}
}
}

const { amount, currency } = Object.assign(
{},
const { amount, currency, precision } = Object.assign(
{
amount: Dinero.defaultAmount,
currency: Dinero.defaultCurrency
currency: Dinero.defaultCurrency,
precision: Dinero.defaultPrecision
},
options
)

assert.isInteger(amount)
assert.isInteger(precision)
assert.isValidPrecision(precision)

const { globalLocale, globalFormat } = Dinero

Expand Down Expand Up @@ -126,6 +133,18 @@ const Dinero = options => {
getCurrency() {
return currency
},
/**
* Returns the precision.
*
* @example
* // returns 6
* Dinero({ precision: 6 }).getPrecision()
*
* @return {Number}
*/
getPrecision() {
return precision
},
/**
* Returns the locale.
*
Expand Down Expand Up @@ -574,7 +593,9 @@ const Dinero = options => {
* @return {Number}
*/
toUnit() {
return calculator.divide(this.getAmount(), 100)
const factor = Math.pow(10, this.getPrecision())

return calculator.divide(this.getAmount(), factor)
},
/**
* Returns the amount represented by this object in rounded units.
Expand Down
3 changes: 2 additions & 1 deletion src/services/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
export const Defaults = {
defaultAmount: 0,
defaultCurrency: 'USD'
defaultCurrency: 'USD',
defaultPrecision: 2
}

/**
Expand Down
12 changes: 12 additions & 0 deletions test/property/dinero.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import Dinero from '../../src/dinero'
import jsc from 'jsverify'

describe('Dinero', () => {
describe('precision', () => {
test('should return the correct amount for precision values in range', () => {
jsc.assert(
jsc.forall(
jsc.integer(1, 15),
a =>
Dinero({ amount: Math.pow(10, a + 1), precision: a }).toUnit() ===
10
)
)
})
})
describe('#add()', () => {
test('should be commutative', () => {
jsc.assert(
Expand Down
23 changes: 21 additions & 2 deletions test/unit/dinero.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ describe('Dinero', () => {
expect(() => Dinero({ amount: '100' })).toThrow()
})
})
describe('precision', () => {
test('should return a new Dinero object with proper precision', () => {
expect(Dinero({ amount: 10000000, precision: 6 }).toUnit()).toBe(10)
})
test('should throw when precision less than 1', () => {
expect(() => Dinero({ precision: 0 })).toThrow()
})
test('should throw when precision more than 15', () => {
expect(() => Dinero({ precision: 16 })).toThrow()
})
})
describe('#getAmount()', () => {
test('should return the right amount as a number', () => {
expect(Dinero({ amount: 500 }).getAmount()).toBe(500)
Expand All @@ -28,6 +39,14 @@ describe('Dinero', () => {
expect(Dinero().getCurrency()).toBe('USD')
})
})
describe('#getPrecision()', () => {
test('should return the right precision as a number', () => {
expect(Dinero({ precision: 6 }).getPrecision()).toBe(6)
})
test('should return the default precision as a number when no precision is specified', () => {
expect(Dinero().getPrecision()).toBe(2)
})
})
describe('#getLocale()', () => {
test('should return the right locale as a string', () => {
expect(
Expand Down Expand Up @@ -400,8 +419,8 @@ describe('Dinero', () => {
})
})
describe('#toUnit()', () => {
test('should return the amount divided by 100', () => {
expect(Dinero({ amount: 1050 }).toUnit()).toBe(10.5)
test('should return the amount divided by the correct precision', () => {
expect(Dinero({ amount: 105000, precision: 4 }).toUnit()).toBe(10.5)
})
})
describe('#toRoundedUnit()', () => {
Expand Down

0 comments on commit cc09a56

Please sign in to comment.