A glossary of intuitive, universally unambiguous Price definitions and useful procedures.
declare a ubiquitious language for
-
domain objects related to
Price
- price
- currency
-
procedures related to
Price
- cast price to words
- cast words to price
- sum prices
- etc
npm install domain-glossary-price
per the standard laid out by Stripe, price.amounts are declared in the smallest unit of the currency
for example, for USD, in cents
import { Price, Currency } from 'domain-glossary-price';
const price = new Price({ amount: 50_00, currency: 'USD' })
const price = new Price({ amount: 37_00, currency: Currency.USD })
its often convenient to declare prices in human words rather than dobjs
you can do so easily with ofPriceWord
const price: Price = ofPriceWord('$50.37')
expect(price).toEqual(new Price({ amount: 50_37, currency: 'USD' }))
a common usecase is to show a price from the database in human words
you can do so easily with asPriceWord
const price: string = asPriceWord(new Price({ 50_37, currency: 'USD' }))
expect(price).toEqual('$50.37')
you can use the options
input to further customize. for example, drop the cents
const price: string = asPriceWord(new Price({ 50_37, currency: 'USD' }), { cents: false })
expect(price).toEqual('$50')
add prices together
const priceSum = sumPrices([priceA, priceB, priceC])
subtract one price from another
const priceDiff = subPrices([priceA, priceB])
multiply a price by a scalar
const price5xed = multiplyPrice(price, 5)
divide a price by a scalar
const priceHalfed = dividePrice(price, 2)
calculate the standard deviation of a list of prices
const priceStdev = calcPriceStdev([priceA, priceB, priceC])