Skip to content

Instantly share code, notes, and snippets.

@sgromkov
Last active March 6, 2020 18:25
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 sgromkov/4ce5d7a1b3ccf5a7edba998d6c9b54e4 to your computer and use it in GitHub Desktop.
Save sgromkov/4ce5d7a1b3ccf5a7edba998d6c9b54e4 to your computer and use it in GitHub Desktop.
Cross-Platform price formatter in 'ru-RU' locale. Covered by unit tests on Jest
import numberFormat from './numberFormat';
/**
* Returns price in format '1 000 000 000'
* @param {Number|String} price
* @returns {String}
*/
export default function getPriceInRU(price) {
return numberFormat(price, 0, ',', ' ')
}
import numberFormat from './numberFormat';
/**
* Returns price in format '1 000 000 000'
* @param {Number|String} price
* @returns {String}
*/
export default function getPriceInRU(price) {
return numberFormat(price, 0, ',', ' ')
}
import getPriceInRU from './getPriceInRU';
describe('getPriceInRU(Number)', () => {
it('should return "0"', () => {
expect(getPriceInRU(0)).toBe("0");
});
it('should return "1"', () => {
expect(getPriceInRU(1)).toBe("1");
});
it('should return "10"', () => {
expect(getPriceInRU(10)).toBe("10");
});
it('should return "100"', () => {
expect(getPriceInRU(100)).toBe("100");
});
it('should return "1 000"', () => {
expect(getPriceInRU(1000)).toBe("1 000");
});
it('should return "10 000"', () => {
expect(getPriceInRU(10000)).toBe("10 000");
});
it('should return "100 000"', () => {
expect(getPriceInRU(100000)).toBe("100 000");
});
it('should return "1 000 000"', () => {
expect(getPriceInRU(1000000)).toBe("1 000 000");
});
it('should return "10 000 000"', () => {
expect(getPriceInRU(10000000)).toBe("10 000 000");
});
it('should return "100 000 000"', () => {
expect(getPriceInRU(100000000)).toBe("100 000 000");
});
it('should return "1 000 000 000"', () => {
expect(getPriceInRU(1000000000)).toBe("1 000 000 000");
});
it('should return "10 000 000 000"', () => {
expect(getPriceInRU(10000000000)).toBe("10 000 000 000");
});
it('should return "100 000 000 000"', () => {
expect(getPriceInRU(100000000000)).toBe("100 000 000 000");
});
});
describe('getPriceInRU(String)', () => {
it('should return "0"', () => {
expect(getPriceInRU("0")).toBe("0");
});
it('should return "1"', () => {
expect(getPriceInRU("1")).toBe("1");
});
it('should return "10"', () => {
expect(getPriceInRU("10")).toBe("10");
});
it('should return "100"', () => {
expect(getPriceInRU("100")).toBe("100");
});
it('should return "1 000"', () => {
expect(getPriceInRU("1000")).toBe("1 000");
});
it('should return "10 000"', () => {
expect(getPriceInRU("10000")).toBe("10 000");
});
it('should return "100 000"', () => {
expect(getPriceInRU("100000")).toBe("100 000");
});
it('should return "1 000 000"', () => {
expect(getPriceInRU("1000000")).toBe("1 000 000");
});
it('should return "10 000 000"', () => {
expect(getPriceInRU("10000000")).toBe("10 000 000");
});
it('should return "100 000 000"', () => {
expect(getPriceInRU("100000000")).toBe("100 000 000");
});
it('should return "1 000 000 000"', () => {
expect(getPriceInRU("1000000000")).toBe("1 000 000 000");
});
it('should return "10 000 000 000"', () => {
expect(getPriceInRU("10000000000")).toBe("10 000 000 000");
});
it('should return "100 000 000 000"', () => {
expect(getPriceInRU("100000000000")).toBe("100 000 000 000");
});
});
/**
* Format a number with grouped thousands.
*
* It is an analog of php's number_format:
* @see https://www.php.net/manual/en/function.number-format.php
*
* @param {Number|String} number The number being formatted
* @param {Number} decimals Sets the number of decimal points
* @param {String} decPoint Sets the separator for the decimal point
* @param {String} thousandsSep Sets the thousands separator
* @returns {String} A formatted version of number
*/
export default function numberFormat(number, decimals, decPoint, thousandsSep) {
var i, j, kw, kd, km;
if (isNaN(decimals = Math.abs(decimals))) {
decimals = 2;
}
if (decPoint == undefined) {
decPoint = ",";
}
if (thousandsSep == undefined) {
thousandsSep = ".";
}
i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
if ((j = i.length) > 3) {
j = j % 3;
} else {
j = 0;
}
km = (j ? i.substr(0, j) + thousandsSep : "");
kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousandsSep);
kd = (decimals ? decPoint + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
return km + kw + kd;
}
import numberFormat from './numberFormat';
describe('numberFormat()', () => {
it('should work correctly without additional params', () => {
expect(numberFormat(1000)).toBe('1.000,00');
expect(numberFormat(1234.56)).toBe('1.234,56');
expect(numberFormat('1.2000')).toBe('1,20');
});
it('should work correctly with additional params', () => {
expect(numberFormat(1234.56, 2, ',', ' ')).toBe('1 234,56');
expect(numberFormat(1234.5678, 2, '.', '')).toBe('1234.57');
expect(numberFormat(67, 2, ',', '.')).toBe('67,00');
expect(numberFormat(67.311, 2)).toBe('67,31');
expect(numberFormat(1000.55, 1)).toBe('1.000,5');
expect(numberFormat(67000, 5, ',', '.')).toBe('67.000,00000');
expect(numberFormat(0.9, 0)).toBe('1');
expect(numberFormat('1.20', 2)).toBe('1,20');
expect(numberFormat('1000.50', 2, '.', ' ')).toBe('1 000.50');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment