Skip to content

API Reference

Daniel Leite de Oliveira edited this page Feb 21, 2017 · 8 revisions

Wallet

constructor

Create a new Wallet object.

Parameters

amount

{Map}

A key/value Map with currency/value

import Wallet from 'walletjs'
const wallet = new Wallet()

init

Returns a new Wallet with initial money.

Parameters

arrayOfMoney

{Money []}

An array of money.

import Wallet, { Money } from 'walletjs'
const money1 = Money.init(100)
const money2 = Money.init(100)
const wallet1 = Wallet.init(money1, money2)
const wallet2 = Wallet.init.apply(this, [money1, money2])

getAmount

Returns an amount by currency.

Parameters

currency

{string}

A money currency.

import Wallet, { Money } from 'walletjs'
const money1 = Money.init(100)
const money2 = Money.init(100)
const wallet = Wallet.init(money1, money2)
wallet.getAmount('USD')
wallet.getAmount(money1.currency)

add

Return a new Wallet with added Money.

Parameters

money

{Money}

The money that will be added.

import Wallet, { Money } from 'walletjs'
const money = Money.init(100)
const wallet = Wallet.init()
wallet.add(money)

subtract

Return a new Wallet with subtracted Money.

Parameters

money

{Money}

The money that will be subtracted.

import Wallet, { Money } from 'walletjs'
const money = Money.init(100)
const wallet = Wallet.init()
wallet.subtract(money)

convertCurrency

Return a new Wallet with new currencies.

Parameters

from

{currency}

to

{currency}

exchangeRate

{number}

import Wallet, { Money } from 'walletjs'
const money = Money.init(100)
const wallet = Wallet.init()
wallet.convertCurrency('USD', 'BRL', 0.39)

Money

constructor

Create a new Money object with the initial value.

Parameters

value (required)

{number}

The value to put on wallet

locale

{number} [default=en]

The locale for this money

currency

{number} [default=USD]

The currency to use in currency formatting.

currencyFractionals

{number} [default=2]

The currency fractionals to use in currency formatting

import { Money } from 'walletjs'
const amount = new Money(100, { 
  locale: 'en', 
  currency: 'USD', 
  currencyFractionals: 2,
})

init

A static method to generate a new Money object with the initial value.

Parameters

value (required)

{number}

The value to put on wallet

locale

{number} [default=en]

The locale for this money

currency

{number} [default=USD]

The currency to use in currency formatting.

currencyFractionals

{number} [default=2]

The currency fractionals to use in currency formatting

import { Money } from 'walletjs'
const amount = Money.init(100, { 
  locale: 'en', 
  currency: 'USD', 
  currencyFractionals: 2,
})

fromString

A static method to generate a new Money object from String.

Parameters

value (required)

{string}

The value to put on wallet

locale

{number} [default=en]

The locale for this money

currency

{number} [default=USD]

The currency to use in currency formatting.

currencyFractionals

{number} [default=2]

The currency fractionals to use in currency formatting

import { Money } from 'walletjs'
const amount = Money.fromString('100', { 
  locale: 'en', 
  currency: 'USD', 
  currencyFractionals: 2,
})

add

Adds a value to money entity. Returns a Money.

Parameters

value (required)

{number}

The value to put on wallet

import { Money } from 'walletjs'
const amount = Money.init(100)
const newAmount = amount.add(100)

subtract

Subtract a value from money entity. Returns a Money.

Parameters

value (required)

{number}

The value to remove from wallet

import { Money } from 'walletjs'
const amount = Money.init(100)
const newAmount = amount.subtract(100)

multiplyBy

Multiply your money by value. Returns a Money.

Parameters

value (required)

{number}

The value to multiply your money

import { Money } from 'walletjs'
const amount = Money.init(100)
const newAmount = amount.multiplyBy(2)

divideBy

Divide your money by value. Returns a Money.

Parameters

value (required)

{number}

The value to divide your money

import { Money } from 'walletjs'
const amount = Money.init(100)
const newAmount = amount.divideBy(2)

getValue

Return the current value on Money

import { Money } from 'walletjs'
const amount = Money.init(100)
console.log(amount.getValue())

getLocale

Return Money locale

import { Money } from 'walletjs'
const amount = Money.init(100, { locale: 'pt-BR' } )
console.log(amount.getLocale()) => 'pt-BR'

toCurrency

Return a formatted currency of Money

Parameters

currencyDisplay

{string} [default=symbol]

How to display the value in currency formatting. Available formats: [symbol, code, name]

import { Money } from 'walletjs'
const amount = Money.init(100)
console.log(amount.toCurrency())

toString

Return a formatted value of Money

import { Money } from 'walletjs'
const amount = Money.init(100)
console.log(amount.toString())