Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarah Dayan committed Dec 29, 2018
2 parents 3acb582 + 0f97d98 commit 6f268cf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/dinero.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const calculator = Calculator()
* * **Manipulation:** {@link module:Dinero~add add}, {@link module:Dinero~subtract subtract}, {@link module:Dinero~multiply multiply}, {@link module:Dinero~divide divide}, {@link module:Dinero~percentage percentage}, {@link module:Dinero~allocate allocate} and {@link module:Dinero~convert convert}.
* * **Testing:** {@link module:Dinero~equalsTo equalsTo}, {@link module:Dinero~lessThan lessThan}, {@link module:Dinero~lessThanOrEqual lessThanOrEqual}, {@link module:Dinero~greaterThan greaterThan}, {@link module:Dinero~greaterThanOrEqual greaterThanOrEqual}, {@link module:Dinero~isZero isZero}, {@link module:Dinero~isPositive isPositive}, {@link module:Dinero~isNegative isNegative}, {@link module:Dinero~hasSubUnits hasSubUnits}, {@link module:Dinero~hasSameCurrency hasSameCurrency} and {@link module:Dinero~hasSameAmount hasSameAmount}.
* * **Configuration:** {@link module:Dinero~setLocale setLocale}.
* * **Conversion & formatting:** {@link module:Dinero~toFormat toFormat}, {@link module:Dinero~toUnit toUnit}, {@link module:Dinero~toRoundedUnit toRoundedUnit}, {@link module:Dinero~toObject toObject}, {@link module:Dinero~convertPrecision convertPrecision} and {@link module:Dinero.normalizePrecision normalizePrecision}.
* * **Conversion & formatting:** {@link module:Dinero~toFormat toFormat}, {@link module:Dinero~toUnit toUnit}, {@link module:Dinero~toRoundedUnit toRoundedUnit}, {@link module:Dinero~toObject toObject}, {@link module:Dinero~toJSON toJSON}, {@link module:Dinero~convertPrecision convertPrecision} and {@link module:Dinero.normalizePrecision normalizePrecision}.
*
* @module Dinero
* @param {Number} [options.amount=0] - The amount in minor currency units (as an integer).
Expand Down Expand Up @@ -862,7 +862,7 @@ const Dinero = options => {
)
},
/**
* Return the object's data as an object literal.
* Returns the object's data as an object literal.
*
* @example
* // returns { amount: 500, currency: 'EUR', precision: 2 }
Expand All @@ -876,6 +876,21 @@ const Dinero = options => {
currency,
precision
}
},
/**
* Returns the object's data as an object literal.
*
* Alias of {@link module:Dinero~toObject toObject}.
* It is defined so that calling `JSON.stringify` on a Dinero object will automatically extract the relevant data.
*
* @example
* // returns '{"amount":500,"currency":"EUR","precision":2}'
* JSON.stringify(Dinero({ amount: 500, currency: 'EUR', precision: 2 }))
*
* @return {Object}
*/
toJSON() {
return this.toObject()
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/unit/dinero.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,18 @@ describe('Dinero', () => {
})
})
})
describe('#toJSON', () => {
test('should return the same object as #toObject', () => {
const price = Dinero({ amount: 500, currency: 'EUR', precision: 2 })
expect(price.toJSON()).toEqual(price.toObject())
})
test('should make Dinero objects stable when using JSON.parse after JSON.stringify', () => {
const price = Dinero({ amount: 500, currency: 'EUR', precision: 2 })
expect(Dinero(JSON.parse(JSON.stringify(price))).equalsTo(price)).toBe(
true
)
})
})
describe('#normalizePrecision', () => {
test('should return an array of Dinero objects with normalized precision and converted amount', () => {
const normalized = Dinero.normalizePrecision([
Expand Down

0 comments on commit 6f268cf

Please sign in to comment.