Skip to content

Instantly share code, notes, and snippets.

@fhpriamo
Last active April 6, 2020 20:54
Show Gist options
  • Save fhpriamo/0a904bbe5db9f6acb16b0d8517a9004d to your computer and use it in GitHub Desktop.
Save fhpriamo/0a904bbe5db9f6acb16b0d8517a9004d to your computer and use it in GitHub Desktop.
/**
* MIT License
*
* Copyright (c) 2020 Rodrigo Martins
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Authors:
* - Rodrigo Martins (https://github.com/drigos)
* - Fábio Priamo (https://github.com/fhpriamo)
*/
const assert = require('assert').strict;
function toCents(moneyStr) {
const [units = '0', cents = '0'] = moneyStr.replace(/[^\d,]/g, '').split(',');
const centsStr = cents.padEnd(2, '0');
const unitsStr = units.padEnd(1, '0');
return parseInt(unitsStr, 10) * 100 + parseInt(centsStr, 10);
}
const fromCents = (function () {
function splitRight(str, offset) {
const right = str.slice(-offset);
const left = str.substr(0, str.length - offset);
return [left, right];
}
// tail call optimized
function splitThousands(num) {
return (function _splitThousands(_num, acc = []) {
if (!_num.length) return [].concat(acc);
const [remaining, thousand] = splitRight(_num, 3);
return _splitThousands(remaining, acc.concat(thousand));
})(num).reverse();
}
return function (cents, padding = 1) {
const centsStr = cents.toString().padStart(2, '0');
const [mantissa, decimal] = splitRight(centsStr, 2);
const thousands = splitThousands(mantissa);
return `${thousands.join('.').padStart(padding, '0')},${decimal}`;
};
})();
// toCents
assert.equal(toCents(''), 0);
assert.equal(toCents('0'), 0);
assert.equal(toCents('0._'), 0);
assert.equal(toCents('0,10'), 10);
assert.equal(toCents('00,10'), 10);
assert.equal(toCents(',10'), 10);
assert.equal(toCents('1'), 100);
assert.equal(toCents('10'), 1000);
assert.equal(toCents('10,1'), 1010);
assert.equal(toCents('2.235'), 223500);
assert.equal(toCents('2235'), 223500);
assert.equal(toCents('2235,'), 223500);
assert.equal(toCents('2235,00'), 223500);
assert.equal(toCents('2235,0'), 223500);
assert.equal(toCents('2235,99'), 223599);
assert.equal(toCents('3.232.235,99'), 323223599);
assert.equal(toCents('90.071.992.547.409,91'), Number.MAX_SAFE_INTEGER);
// fromCents
assert.equal(fromCents(324005226), '3.240.052,26');
assert.equal(fromCents(40052260), '400.522,60');
assert.equal(fromCents(1000222), '10.002,22');
assert.equal(fromCents(2000), '20,00');
assert.equal(fromCents(0), '0,00');
assert.equal(fromCents(1), '0,01');
assert.equal(fromCents(2), '0,02');
assert.equal(fromCents(3), '0,03');
assert.equal(fromCents(10), '0,10');
assert.equal(fromCents(00000000), '0,00');
assert.equal(fromCents(999999999999999), '9.999.999.999.999,99');
assert.equal(fromCents(Number.MAX_SAFE_INTEGER), '90.071.992.547.409,91');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment