UNPKG

@activecollab/components

Version:

ActiveCollab Components

458 lines (454 loc) • 11.9 kB
import { formatNumber, getNumberFromString, numberWithSeparator, fixedDecimalSpaces, formatCurrency } from "./currencyUtils"; describe("format currency with thousandseparator set to ',' and decimalSeparator to '.'", () => { // please try to leave unformatted - ignore works for this block only // prettier-ignore it.each([[{ value: "" }, ""], [{ value: undefined }, ""], [{ value: "10" }, "10"], [{ value: "1000" }, "1,000"], [{ value: "1000.00" }, "1,000"], [{ value: "1200.50" }, "1,200.5"], [{ value: "1,200.00" }, "1,200"], [{ value: "2,240.00" }, "2,240"], [{ value: "22,240.00" }, "22,240"], [{ value: "50,000,000.00" }, "50,000,000"], [{ value: "-50,000.00" }, "-50,000"], [{ value: "-1,200" }, "-1,200"], [{ value: "-90,000,000.00" }, "-90,000,000"], [{ value: "900.25" }, "900.25"], [{ value: 900.25, thousandSeparator: ".", decimalSeparator: "," }, "900,25"], [{ value: 0, thousandSeparator: ".", decimalSeparator: ",", trimDecimals: false, format: "long", decimalSpaces: 2 }, "0,00"], [{ value: "900,25", thousandSeparator: ".", decimalSeparator: "," }, "900,25"], [{ value: 900.255556, thousandSeparator: ".", decimalSeparator: "," }, "900,25"], [{ value: "900,255556", thousandSeparator: ".", decimalSeparator: "," }, "900,25"], [{ value: 900.255556, thousandSeparator: ".", decimalSeparator: ",", trimDecimals: false, decimalSpaces: 6 }, "900,255556"], [{ value: "900,255556", thousandSeparator: ".", decimalSeparator: ",", trimDecimals: false }, "900,25"], [{ value: 900.25, thousandSeparator: ".", decimalSeparator: ",", trimDecimals: false, decimalSpaces: 3 }, "900,250"], [{ value: 900.25, thousandSeparator: ".", decimalSeparator: ",", trimDecimals: false, decimalSpaces: 3, format: "long" }, "900,250"], [{ value: 1150.6666667, thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "long" }, "1,150.66"], [{ value: "1150.6666667", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "long" }, "1,150.66"], [{ value: 1150.6666667, thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "short" }, "1.2K"], [{ value: "1150.6666667", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "short" }, "1.2K"], [{ value: 1150.0, thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "short" }, "1.1K"], [{ value: "1150.00000", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "short" }, "1.1K"], [{ value: 1150.6, thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "long" }, "1,150.6"], [{ value: "1150.60", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "long" }, "1,150.6"], [{ value: 1150.0, thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "long" }, "1,150"], [{ value: "1150.00", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "long" }, "1,150"], [{ value: 110.155557, thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "long" }, "110.15"], [{ value: "110.155557", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "long" }, "110.15"], [{ value: 110.155557, thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "short" }, "110.15"], [{ value: "110.155557", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "short" }, "110.15"], [{ value: 1501000.155557, thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "long" }, "1,501,000.15"], [{ value: "1501000.155557", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "long" }, "1,501,000.15"], [{ value: 1501000.155557, thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "short" }, "1.5M"], [{ value: "1501000.155557", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "short" }, "1.5M"], [{ value: "1501000.155557", thousandSeparator: ".", decimalSeparator: ",", trimDecimals: true, decimalSpaces: 2, format: "short" }, "1,5M"], [{ value: "1579.50", thousandSeparator: ".", decimalSeparator: ",", trimDecimals: true, decimalSpaces: 2, format: "short" }, "1,6K"], [{ value: "1579,50", thousandSeparator: ".", decimalSeparator: ",", trimDecimals: true, decimalSpaces: 2, format: "short" }, "1,6K"], [{ value: "1579.50", thousandSeparator: ".", decimalSeparator: ",", trimDecimals: false, decimalSpaces: 2, format: "long" }, "1.579,50"], [{ value: "1579.50", thousandSeparator: ".", decimalSeparator: ",", trimDecimals: false, decimalSpaces: 2, format: "short", shortenThreshold: 10000 }, "1.579,50"]])("should format currency advanced", (params, expected) => { const { value, thousandSeparator, decimalSeparator, trimDecimals, decimalSpaces, format, shortenThreshold } = params; expect(formatNumber(value, thousandSeparator, decimalSeparator, trimDecimals, decimalSpaces, format, shortenThreshold)).toEqual(expected); }); //fixedDecimalSpaces it.each([[{ number: 123.456789, decimalSpaces: 3 }, 123.456], [{ number: 123.3, decimalSpaces: 2 }, 123.3], [{ number: 123.3, decimalSpaces: 0 }, 123]])("should handle fixedDecimalSpaces", (params, expected) => { expect(fixedDecimalSpaces(params.number, params.decimalSpaces)).toBe(expected); }); //numberWithSeparator it("should format number with thousand separator", () => { const result = numberWithSeparator(1000.0.toFixed(3), ",", "."); expect(result).toBe("1,000.000"); }); it("should format string number with thousand separator", () => { const result = numberWithSeparator("1000", ","); expect(result).toBe("1,000"); }); it("should format decimal number with thousand separator and keep decimal part", () => { const result = numberWithSeparator(12345.67, ","); expect(result).toBe("12,345.67"); }); it("should not format when format is set to false", () => { const result = numberWithSeparator(1000, ",", ".", false); expect(result).toBe(1000); }); it("should handle zero with thousand separator", () => { const result = numberWithSeparator(0, ","); expect(result).toBe("0"); }); it("should handle negative number with thousand separator", () => { const result = numberWithSeparator(-123456789, ","); expect(result).toBe("-123,456,789"); }); it("should not format", () => { const result = numberWithSeparator(123456789, ",", ".", false); expect(result).toBe(123456789); }); //getNumberFromString it.each([[{ number: "1.000.000,256", thousandSeparator: ".", decimalSeparator: "," }, 1000000.256], [{ number: 1000000.256, thousandSeparator: ".", decimalSeparator: "," }, 1000000.256], [{ number: "1,000,000.256", thousandSeparator: ",", decimalSeparator: "." }, 1000000.256], [{ number: ",256", thousandSeparator: ".", decimalSeparator: "," }, 0.256], [{ number: "1562", thousandSeparator: ".", decimalSeparator: "," }, 1562], [{ number: "1,2,3.4", thousandSeparator: ".", decimalSeparator: "," }, 0], [{ number: "1562", thousandSeparator: ",", decimalSeparator: "." }, 1562], [{ number: "-1.562,25", thousandSeparator: ".", decimalSeparator: "," }, -1562.25], [{ number: "-1 562.25", thousandSeparator: " ", decimalSeparator: "." }, -1562.25], [{ number: "1562.50", thousandSeparator: ".", decimalSeparator: "," }, 1562.5], [{ number: "1562.", thousandSeparator: ".", decimalSeparator: "," }, 1562]])("should handle getNumberFromString", (params, expected) => { const { number, thousandSeparator, decimalSeparator } = params; expect(getNumberFromString(number, thousandSeparator, decimalSeparator)).toBe(expected); }); // please try to leave unformatted - ignore works for this block only // prettier-ignore it.each([[{ value: "", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: false, decimalSpaces: 2, format: "long", currencyCode: "", currencyCodePosition: "right" }, ""], [{ value: 0, thousandSeparator: ",", decimalSeparator: ".", trimDecimals: false, decimalSpaces: 2, format: "long", currencyCode: "USD", currencyCodePosition: "right" }, "0.00 USD"], [{ value: "10", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: false, decimalSpaces: 2, format: "long", currencyCode: "", currencyCodePosition: "right" }, "10.00"], [{ value: "1000", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: false, decimalSpaces: 2, format: "long", currencyCode: "USD", currencyCodePosition: "right" }, "1,000.00 USD"], [{ value: "1000.00", thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "long", currencyCode: "USD", currencyCodePosition: "left" }, "USD 1,000"], [{ value: "1200,50", thousandSeparator: ".", decimalSeparator: ",", trimDecimals: false, decimalSpaces: 2, format: "long", currencyCode: "EUR", currencyCodePosition: "right" }, "1.200,50 EUR"], [{ value: "1200,50", thousandSeparator: ".", decimalSeparator: ",", trimDecimals: false, decimalSpaces: 2, format: "long", currencyCode: "", currencyCodePosition: "right" }, "1.200,50"], [{ value: "900,25", thousandSeparator: ".", decimalSeparator: ",", trimDecimals: false, decimalSpaces: 2, format: "long", currencyCode: "GBP", currencyCodePosition: "left" }, "GBP 900,25"], [{ value: 900.25, thousandSeparator: ".", decimalSeparator: ",", trimDecimals: false, decimalSpaces: 2, format: "long", currencyCode: "GBP", currencyCodePosition: "left" }, "GBP 900,25"], [{ value: 1150.6666667, thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "short", currencyCode: "JPY", currencyCodePosition: "right" }, "1,150.66 JPY"], [{ value: 1501000.155557, thousandSeparator: ",", decimalSeparator: ".", trimDecimals: true, decimalSpaces: 2, format: "short", currencyCode: "USD", currencyCodePosition: "left" }, "USD 1.5M"]])("should format currency correctly", (args, expected) => { expect(formatCurrency(args.value, args.thousandSeparator, args.decimalSeparator, args.trimDecimals, args.decimalSpaces, args.format, args.currencyCode, args.currencyCodePosition)).toEqual(expected); }); }); //# sourceMappingURL=currencyUtils.test.js.map