0.5.1
Adds an arbitrary amount of ledgers together.
(Array<immutable.Map>)
immutable.Map
:
a ledger combining all values
const wallet = Map({GOLD: 0});
const expensesLedger = Map({GOLD: -5});
const profitsLedger = Map({GOLD: 10, Silver: 3 });
const total = sum(wallet, expensesLedger, profitsLedger);
total.get("GOLD"); // 5
total.get("SILVER"); // 3
const ledgers = [wallet, expenses, profits];
const total = sum(...ledgers);
Multiplies all currency values in a ledger by the scale. This is quite useful for when you want to multiply the effects of an item by the amount of that item.
(immutable.Map)
(Number)
immutable.Map
:
the updated ledger
const incomeFromSalesfolk = Map({GOLD: 2, INFLUENCE: 5});
const newIncome = scale(incomeFromSalesFolk, state.numOfSalesfolk);
newIncome.get("GOLD"); // 2 * state.numOfSalesfolk
newIncome.get("INFLUENCE") // 5 * state.numOfSalesfolk
Returns true if all items in the ledger are positive.
(immutable.Map)
Boolean
:
Returns true if all items in the ledger are negative.
(immutable.Map)
Boolean
:
Returns all unique currencies defined in several ledgers
(...any)
(Array<immutable.Map>)
immutable.List
:
Gets the currency value of a particular currency given several ledgers. This is useful for finding the cost of something spread over several different ledgers.
const materialCost = Map({GOLD: -5, SILVER: -2});
const transportCost = Map({GOLD: -2});
const goldCost = totalOf("GOLD", materialCost, transportCost); // -7
Given an item, run it's cost function and return the result.
immutable.Map
:
a ledger
Given a bunch of items, figure out all their costs and return it as an immutable Map
immutable.Map
:
a Map of ledgers where the key is the
item.type
Buy returns a new wallet after an item is purchased. If the item is free, it just returns the same wallet.
(Object)
has at least a "cost" that returns a ledger.
(immutable.Map)
the current sum of ledgers
(Object
= {}
)
a state object that will be passed to the cost to determine the type
const MagicSword = {
type: "MagicSword",
cost: state => Map({ GOLD: -(5 + state.marketPrice) })
}
const wallet = new Map({ GOLD: 10 });
const state = { marketPrice : 1 };
const newWallet = buy(MagicSword, wallet, state); // Gold => 4
const canBuy = inTheBlack(newWallet); // true
Adds an item to the wallet
const MagicSword = {
type: "MagicSword"
}
const wallet = new Map({GOLD: 10})
add(MagicSword, wallet); // Map({GOLD: 10, MagicSword: 1})
add(MagicSword, wallet, 3); // Map({GOLD: 10, MagicSword: 3})
For a collection of items, combines their "effect" functions into one big ledger. This is useful for caching the onUpdate effects to your wallet rather than computing them every time.
const pouch = {
MagicSword: {
type: "MagicSword",
effect: state => doSomething(state)
},
// ...
}
const ledger = effects(pouch, wallet, state);