Skip to content

Instantly share code, notes, and snippets.

@zekesonxx
Created August 28, 2014 17:11
Show Gist options
  • Save zekesonxx/78086f53a16cff33ed9e to your computer and use it in GitHub Desktop.
Save zekesonxx/78086f53a16cff33ed9e to your computer and use it in GitHub Desktop.
WoW Gold parsing tool
/* jshint undef: true, node: true, quotmark: single, indent: 2 */
'use strict';
/**
* Zeke's World of Warcraft Gold Parsing Tool
* https://gist.github.com/zekesonxx/78086f53a16cff33ed9e
* Copyright (C) Zeke Sonxx 2014.
* Licensed under the MIT License <choosealicense.com/licenses/mit>
* @author Zeke Sonxx <github.com/zekesonxx>
* @version 1.0.0
*/
var gold = {};
/**
* Converts a copper amount to copper/silver/gold
* Inverse: gold.asCopper
* @param {Number} amount Copper
* @return {Object} {copper, silver, gold}
* @public
*/
gold.convert = function(amount) {
if (typeof amount !== 'number') {
throw new TypeError('Expected number as input, got '+typeof amount);
}
var copper = amount;
var silver = Math.floor(amount / 100);
// and
var gold = Math.floor(silver / 100);
if (silver !== 0) {
// adjust the copper amount
copper = copper - (silver*100);
}
if (gold !== 0) {
silver = silver - (gold*100);
}
return {
gold: gold,
silver: silver,
copper: copper
};
};
/**
* Take a gold/silver/copper object and return the value in copper
* Inverse: gold.convert
* @param {Object} input {gold, silver, copper}
* @return {Number} Copper
* @public
*/
gold.asCopper = function(input) {
if (typeof input !== 'object') {
throw new TypeError('Expected object as input, got '+typeof input);
}
var out = 0;
out += input.gold * 100 * 100;
out += input.silver * 100;
out += input.copper;
return out;
};
/**
* Turns a copper value or object from gold.convert into a string
* @param {Object} input Or number
* @param {Boolean} zeros Whether to display 0s reguardless (ex 0g 0s 5c)
* @return {String} Converted String
* @public
*/
gold.stringify = function(input, zeros) {
if (typeof input === 'number') {
//allow people to pass a copper value directly
input = gold.convert(input);
}
if (typeof input !== 'object') {
//it should be a object now
throw new TypeError('Expected object or number as first input, got '+typeof input);
}
if (zeros !== undefined && typeof zeros !== 'boolean') {
throw new TypeError('Expected boolean or undefined as second input, got '+typeof zeros);
}
var out = [];
// Gold
if (input.gold !== 0 || zeros === true) {
out.push((input.gold || 0) + 'g');
}
// Silver
if (input.silver !== 0 || zeros === true || out.length === 1) {
out.push((input.silver || 0) + 's');
}
// Copper
if (input.copper !== 0 || zeros === true || out.length >= 1) {
out.push((input.copper || 0) + 'c');
}
return out.join(' ');
};
/**
* Take an input string like `5g 2s 10c` and return an object
* @param {String} input Input string
* @return {Object} {gold, silver, copper}
* @throws {Error} Parse error, input is invalid
* @public
*/
gold.parse = function(input) {
if (typeof input !== 'string') {
throw new TypeError('Expected string as input, got '+typeof input);
}
var expression = /(?:([0-9]{0,6})g\s)?(?:([0-9]{1,2})s\s)?([0-9]{1,2})c/;
var result = expression.exec(input);
if (result === null) {
throw new Error('Parse error, input is invalid');
}
return {
gold: parseInt(result[1]) || 0,
silver: parseInt(result[2]) || 0,
copper: parseInt(result[3])
};
};
/**
* Test suite
* Errors if fails, silent if succeeded
* @requires assert For assertion testing
* @return {Null}
* @private
*/
gold.__test = function() {
var assert = require('assert');
//gold.convert
assert.equal(gold.convert(4).copper, 4, 'convert #1 failed');
assert.equal(gold.convert(400).silver, 4, 'convert #2 failed');
assert.equal(gold.convert(80000).gold, 8, 'convert #3 failed');
assert.equal(gold.convert(80000).copper, 0, 'convert #4 failed');
//gold.stringify
assert.equal(gold.stringify(5524052), '552g 40s 52c', 'stringify #1 failed');
assert.equal(gold.stringify(4, true), '0g 0s 4c', 'stringify #2 failed');
//gold.parse
assert.equal(gold.parse('50g 2s 10c').gold, 50, 'parse #1 failed');
assert.equal(gold.parse('54c').copper, 54, 'parse #2 failed');
//gold.asCopper
assert.equal(gold.asCopper({ copper: 54, silver: 0, gold: 0 }), 54, 'asCopper #1 failed');
assert.equal(gold.asCopper({ copper: 4, silver: 0, gold: 10 }), 100004, 'asCopper #2 failed');
};
gold.__test();
module.exports = gold;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment