Skip to content

Instantly share code, notes, and snippets.

@SeanJM
Last active December 30, 2015 17:44
Show Gist options
  • Save SeanJM/411ba30d8bb584551dfc to your computer and use it in GitHub Desktop.
Save SeanJM/411ba30d8bb584551dfc to your computer and use it in GitHub Desktop.
A framework to build a number manipulation library with built in functions for currency, padding and grouping
// A framework to build a number manipulation library
// Usage: lumber(5).padLeft(2).value;
// -> 05
// Usage: lumber(5).padRight(2).value;
// -> 50
// Usage: lumber(5.1).currency().value;
// Default arguments for '.currency()' is a template of value: '($0.00)'
// -> $5.10
// Usage: lumber(-5.1).currency().value;
// -> ($5.10)
// Usage: lumber(4349847.18534).group().value;
// -> 4,349,847.18534
// Usage: lumber(4349847.18534).round(2).group().value;
// -> 4,349,847.19
// Usage: lumber(53234.1).currency('€0.00').value;
// -> €53234.10
// Usage: lumber(53234.1).currency('€0.00').group().value;
// -> €53,234.10
function lumber(number) {
var wrapper = {
number : number,
value : number.toString()
};
if (!/^[0-9\.\-]+$/.test(number.toString())) {
throw 'padNumber: Invalid type';
}
for (var fn in lumber) {
wrapper[fn] = function (fn) {
return function () {
return lumber[fn].apply(null, [wrapper].concat([].slice.call(arguments)));
};
}(fn);
}
return wrapper;
}
lumber.clone = function (wrapper) {
var clone = {};
for (var k in wrapper) {
clone[k] = wrapper[k];
}
return clone;
};
lumber.padLeft = function (wrapper, n) {
var padding = new Array(n + 1).join('0');
var start = padding.substr(0, n - wrapper.value.length);
wrapper.value = start + wrapper.value;
return wrapper;
};
lumber.padRight = function (wrapper, n) {
var padding = new Array(n + 1).join('0');
var start = padding.substr(0, n - wrapper.value.length);
wrapper.value = wrapper.value + start;
return wrapper;
};
lumber.group = function (wrapper, char) {
if (typeof char === 'undefined') {
char = ',';
}
wrapper.value = wrapper.value.replace(/[0-9]+/, function (b) {
b = b.split('').reverse();
for (var i = b.length; i >= 0; i--) {
if (i > 0 && i % 3 === 0) {
b.splice(i, 0, char);
}
}
return b.reverse().join('');
});
return wrapper;
};
lumber.currency = function (wrapper, temp) {
if (typeof temp === 'undefined') {
temp = '($0.00)';
}
function currency() {
var a = wrapper.number.toString().match(/[0-9]+/g).slice(0, 2);
var n = temp.match(/[0]+/g).slice(-1)[0].length;
var isNegative = wrapper.number < 0;
var i = false;
if (a.length === 1) {
a[1] = new Array(n + 1).join('0');
} else {
a[1] = lumber(Number(a[1])).padRight(n).value;
}
wrapper.value = temp.replace(/\)|\(|-|[0]+/g, function (b) {
var isZero = /0/.test(b);
if (isZero && !i) {
i = true;
return a[0];
} else if (isZero) {
return a[1];
} else if (isNegative && !isZero) {
return b;
}
return '';
});
return wrapper;
}
return currency();
};
lumber.round = function (wrapper, n) {
var number = Number(wrapper.value);
wrapper.value = (Math.round(number * Math.pow(10, n)) / Math.pow(10, n)).toString();
return wrapper;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment