Skip to content

Instantly share code, notes, and snippets.

@studentIvan
Created December 11, 2020 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save studentIvan/b11d251118bf55e8669dd4d45a888f44 to your computer and use it in GitHub Desktop.
Save studentIvan/b11d251118bf55e8669dd4d45a888f44 to your computer and use it in GitHub Desktop.
Price format super fast function by @studentIvan (Ivan Maslov)
/** [E]UR, [S]EK, [G]PB, [P]LN, [D]KK, NOK, CHF, [U]SD, [CA]D */
const defaultNumberToPriceFn = function numberToPrice(n, c) {
let smb = c || this.currency;
if (smb[0] === 'E') { smb = '€'; }
else if (smb[0] === 'U' || (smb[0] === 'C' && smb[1] === 'A')) { smb = '$'; }
else if (smb[0] === 'S' || smb[0] === 'D') { smb = 'kr'; }
else if (smb[0] === 'G') { smb = '£'; }
else if (smb[0] === 'P') { smb = 'zł'; }
const nR = Math.round(+(n || 0) * 100) / 100;
const nStr = nR.toString();
let r = ''; let x = 0; let d = (nR ^ 0) === nR; let adl = 0;
for (let i = nStr.length - 1; i >= 0; i--) {
if (!d) {
if (nStr[i] === '.') {
d = true; r = ',' + r;
continue;
}
r = nStr[i] + r;
adl++;
continue;
}
x++;
if (x % 4 === 0) {
x = 1;
r = nStr[i] + ' ' + r;
continue;
}
r = nStr[i] + r;
}
return smb === '$'
? `$${ r }${ adl === 1 ? '0' : '' } ${ c || this.currency }`
: r + (adl === 1 ? '0 ' : ' ') + smb;
};
const northAmericaNumberToPriceFn = function northAmericaNumberToPrice(n, c) {
const smb = '$';
const nR = Math.round(+(n || 0) * 100) / 100;
const nStr = nR.toString();
let r = ''; let x = 0; let d = (nR ^ 0) === nR; let adl = 0;
for (let i = nStr.length - 1; i >= 0; i--) {
if (!d) {
if (nStr[i] === '.') {
d = true; r = '.' + r;
continue;
}
r = nStr[i] + r;
adl++;
continue;
}
x++;
if (x % 4 === 0) {
x = 1;
r = nStr[i] + ',' + r;
continue;
}
r = nStr[i] + r;
}
return smb === '$'
? `$${ r }${ adl === 1 ? '0' : '' } ${ c || this.currency }`
: r + (adl === 1 ? '0 ' : ' ') + smb;
};
export { defaultNumberToPriceFn, northAmericaNumberToPriceFn }
/* eslint-disable */
import chai, { expect } from 'chai';
const { defaultNumberToPriceFn, northAmericaNumberToPriceFn } = require('./number-to-price');
const itShouldDisplayPriceCorrectly = (price, currency, result) =>
it(`price ${ price } ${ currency } must be equal to ${ result }`, () => {
expect(defaultNumberToPriceFn(price, currency)).to.equal(result);
});
const itShouldDisplayPriceCorrectlyNA = (price, currency, result) =>
it(`price ${ price } ${ currency } must be equal to ${ result }`, () => {
expect(northAmericaNumberToPriceFn(price, currency)).to.equal(result);
});
describe('defaultNumberToPriceFn price format test', () => {
/** simple domain cases */
for (let i = 0; i < 2; i+=0.01) {
itShouldDisplayPriceCorrectly(i, 'SEK', (i.toFixed(2) + ' kr').replace('.', ',').replace(',00', ''));
}
itShouldDisplayPriceCorrectly(0, 'EUR', '0 €');
itShouldDisplayPriceCorrectly(0.5, 'EUR', '0,50 €');
itShouldDisplayPriceCorrectly(10000, 'SEK', '10 000 kr');
itShouldDisplayPriceCorrectly(5000, 'SEK', '5 000 kr');
itShouldDisplayPriceCorrectly(5010.23, 'SEK', '5 010,23 kr');
itShouldDisplayPriceCorrectly(10000, 'DKK', '10 000 kr');
itShouldDisplayPriceCorrectly(5000, 'DKK', '5 000 kr');
itShouldDisplayPriceCorrectly(5010.23, 'DKK', '5 010,23 kr');
itShouldDisplayPriceCorrectly(5010.23, 'PLN', '5 010,23 zł');
itShouldDisplayPriceCorrectly(10000, 'EUR', '10 000 €');
itShouldDisplayPriceCorrectly(10000, 'PLN', '10 000 zł');
itShouldDisplayPriceCorrectly(10000.99, 'EUR', '10 000,99 €');
itShouldDisplayPriceCorrectly(10000.999, 'EUR', '10 001 €');
itShouldDisplayPriceCorrectly(10.50, 'EUR', '10,50 €');
itShouldDisplayPriceCorrectly(10.50, 'NOK', '10,50 NOK');
itShouldDisplayPriceCorrectly(100000.00, 'NOK', '100 000 NOK');
itShouldDisplayPriceCorrectly(100000.52, 'NOK', '100 000,52 NOK');
itShouldDisplayPriceCorrectly(129.9, 'EUR', '129,90 €');
itShouldDisplayPriceCorrectly(49.9, 'EUR', '49,90 €');
itShouldDisplayPriceCorrectly(39.95, 'EUR', '39,95 €');
itShouldDisplayPriceCorrectly(1299, 'NOK', '1 299 NOK');
itShouldDisplayPriceCorrectly(499, 'NOK', '499 NOK');
itShouldDisplayPriceCorrectly(108, 'GBP', '108 £');
itShouldDisplayPriceCorrectly(59.42, 'GBP', '59,42 £');
itShouldDisplayPriceCorrectly(59.9, 'GBP', '59,90 £');
itShouldDisplayPriceCorrectly(538922.9, 'GBP', '538 922,90 £');
itShouldDisplayPriceCorrectly(140, 'CHF', '140 CHF');
itShouldDisplayPriceCorrectly(29.9, 'CHF', '29,90 CHF');
itShouldDisplayPriceCorrectly(142.89, 'CHF', '142,89 CHF');
itShouldDisplayPriceCorrectly(74.95 + 99.90, 'EUR', '174,85 €');
itShouldDisplayPriceCorrectly(100, 'USD', '$100 USD');
itShouldDisplayPriceCorrectly(100, 'CAD', '$100 CAD');
itShouldDisplayPriceCorrectlyNA(100, 'USD', '$100 USD');
itShouldDisplayPriceCorrectlyNA(100.50, 'USD', '$100.50 USD');
itShouldDisplayPriceCorrectlyNA(100000.00, 'USD', '$100,000 USD');
itShouldDisplayPriceCorrectlyNA(100, 'CAD', '$100 CAD');
itShouldDisplayPriceCorrectlyNA(100.50, 'CAD', '$100.50 CAD');
itShouldDisplayPriceCorrectlyNA(100000.00, 'CAD', '$100,000 CAD');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment