Skip to content

Instantly share code, notes, and snippets.

@mdamaceno
Forked from fhpriamo/pt-br-currency-formatter.js
Last active April 6, 2020 20:56
Show Gist options
  • Save mdamaceno/d7f53b70c4dc1e3e0aa2541ed45479de to your computer and use it in GitHub Desktop.
Save mdamaceno/d7f53b70c4dc1e3e0aa2541ed45479de 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, separator = ',') {
let regex = /[^\d,]/g;
if (separator === '.') {
regex = /[^\d.]/g;
}
if (!moneyStr) {
return 0;
}
let [units = '0', cents = '0'] = moneyStr.replace(regex, '').split(separator);
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, centSeparator = ',', thousandSeparator = '.', padding = 1) {
const centsStr = cents.toString().padStart(2, '0');
const [mantissa, decimal] = splitRight(centsStr, 2);
const thousands = splitThousands(mantissa);
return `${thousands.join(thousandSeparator).padStart(padding, '0')}${centSeparator}${decimal}`;
};
})();
// toCents with comma
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 with comma
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');
let centSep = '.';
let thousandSep = ',';
// toCents with dot
assert.equal(toCents('', centSep), 0);
assert.equal(toCents('0', centSep), 0);
assert.equal(toCents('0._', centSep), 0);
assert.equal(toCents('0.10', centSep), 10);
assert.equal(toCents('00.10', centSep), 10);
assert.equal(toCents('.10', centSep), 10);
assert.equal(toCents('1', centSep), 100);
assert.equal(toCents('10', centSep), 1000);
assert.equal(toCents('10.1', centSep), 1010);
assert.equal(toCents('2,235', centSep), 223500);
assert.equal(toCents('2235', centSep), 223500);
assert.equal(toCents('2235.', centSep), 223500);
assert.equal(toCents('2235.00', centSep), 223500);
assert.equal(toCents('2235.0', centSep), 223500);
assert.equal(toCents('2235.99', centSep), 223599);
assert.equal(toCents('3,232,235.99', centSep), 323223599);
assert.equal(toCents('90,071,992,547,409.91', centSep), Number.MAX_SAFE_INTEGER);
// fromCents with dot
assert.equal(fromCents(324005226, centSep, thousandSep), '3,240,052.26');
assert.equal(fromCents(40052260, centSep, thousandSep), '400,522.60');
assert.equal(fromCents(1000222, centSep, thousandSep), '10,002.22');
assert.equal(fromCents(2000, centSep, thousandSep), '20.00');
assert.equal(fromCents(0, centSep, thousandSep), '0.00');
assert.equal(fromCents(1, centSep, thousandSep), '0.01');
assert.equal(fromCents(2, centSep, thousandSep), '0.02');
assert.equal(fromCents(3, centSep, thousandSep), '0.03');
assert.equal(fromCents(10, centSep, thousandSep), '0.10');
assert.equal(fromCents(00000000, centSep, thousandSep), '0.00');
assert.equal(fromCents(999999999999999, centSep, thousandSep), '9,999,999,999,999.99');
assert.equal(fromCents(Number.MAX_SAFE_INTEGER, centSep, thousandSep), '90,071,992,547,409.91');
thousandSep = '_'
// fromCents with dot (thousandSep = "_")
assert.equal(fromCents(324005226, centSep, thousandSep), '3_240_052.26');
assert.equal(fromCents(40052260, centSep, thousandSep), '400_522.60');
assert.equal(fromCents(1000222, centSep, thousandSep), '10_002.22');
assert.equal(fromCents(2000, centSep, thousandSep), '20.00');
assert.equal(fromCents(0, centSep, thousandSep), '0.00');
assert.equal(fromCents(1, centSep, thousandSep), '0.01');
assert.equal(fromCents(2, centSep, thousandSep), '0.02');
assert.equal(fromCents(3, centSep, thousandSep), '0.03');
assert.equal(fromCents(10, centSep, thousandSep), '0.10');
assert.equal(fromCents(00000000, centSep, thousandSep), '0.00');
assert.equal(fromCents(999999999999999, centSep, thousandSep), '9_999_999_999_999.99');
assert.equal(fromCents(Number.MAX_SAFE_INTEGER, centSep, thousandSep), '90_071_992_547_409.91');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment