A Fantasy RPG Currency Exchange.
npm install --save scribe-rpg-coin-purse
This project makes it easy to:
- Add and subtract multiple denominations
- Parse strings for value denomination pairs
- Keep your coins organized
- config
- copperValue(string, config)
- parser(exp, config)
- subUnits(coppers, config)
- total(array, config)
Most functions accept a config object.
An array of denomination objects
Schema:
{
name: String,
abrv: String, (must be two uppercase letters)
copperValue: Number,
}
Default:
[
{
name: 'copper',
abrv: 'CP',
copperValue: 1,
},
{
name: 'silver',
abrv: 'SP',
copperValue: 10,
},
{
name: 'electrum',
abrv: 'EP',
copperValue: 50,
},
{
name: 'gold',
abrv: 'GP',
copperValue: 100,
},
{
name: 'platinum',
abrv: 'PP',
copperValue: 1000,
},
]
Returns the total copper value of string.
copperValue(string, config)
-
string {string} required
- A string with value denomination pairs in it to be parsed
-
config {object} optional
- an object containing configuration options
- default value: { denominations }
-
@return {number} sum of all value denomination pairs in string
const val = copperValue('This sword is worth 20gp');
val === 200;
Returns an array of objects describing each denomination value pair found within exp.
parser(exp, config)
-
exp {string} required
- A string containing value denomination pairs
-
config {object} optional
- an object containing configuration options
- default value: { denominations }
-
@return {array}
[
{
match: String, // the match found
value: String, // the value in the found match
denomination: String, // the denomination in the found match
},
...
]
const val = parser('This sword is worth 20gp');
val === { match: '20gp', value: '20', denomination: 'GP' };
Takes a copper value and returns and object containing each denomination and their value.
subUnits(coppers, config)
-
exp {number} required
- A number representing the total copper value to be parsed
-
config {object} optional
- an object containing configuration options
- default value: { denominations }
-
@return {object} [contains a key for each denomination in denominations]
{
CP: 0,
SP: 0,
EP: 0,
GP: 0,
PP: 0,
}
const val = subUnits(1161);
val === {
CP: 1,
SP: 1,
EP: 1,
GP: 1,
PP: 1,
};
Returns the total copper value of all expressions in array
total(array, config)
-
array {array} required
- An array of strings containing value denomination pairs
-
config {object} optional
- an object containing configuration options
- default value: { denominations }
-
@return {number}
const val = total(['1CP', '1CP']);
val === 2;
Returns each total denomination value within given copperValue
total(array, config)
-
array {array} required
- An array of strings containing value denomination pairs
-
config {object} optional
- an object containing configuration options
- default value: { denominations }
-
@return {number}
const vals = values(1161);
vals === {
CP: 1161,
SP: 116,
EP: 23,
GP: 11,
PP: 1,
}
Feature requests, issues, and contributions are all welcome.