From c86b2ff60cf7fd359badf826a118a0c3666b8553 Mon Sep 17 00:00:00 2001 From: Krystian Karczewski Date: Thu, 6 Oct 2022 11:39:18 +0200 Subject: [PATCH] new feature, options: currencySign, currencyDisplay --- CHANGELOG.md | 17 ++++++++++++++++ DOCUMENTATION.md | 8 +++++++- __tests__/formatMoney.test.ts | 6 ++++++ __tests__/getCurrencySymbol.test.ts | 28 +++++++++++++++++++++++---- package.json | 2 +- src/formatMoney.ts | 3 ++- src/formatNumber.ts | 11 +++++++++-- src/formatNumberToFixed.ts | 1 + src/getCurrencySymbol.ts | 30 +++++++++++++++++++++++------ src/index.ts | 4 +++- src/types/currencySymbolOptions.ts | 7 +++++++ src/types/intlCurrencySign.ts | 1 + src/types/numberFormatBase.ts | 6 ++++-- src/types/numberFormatMoney.ts | 6 ++++-- 14 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 src/types/currencySymbolOptions.ts create mode 100644 src/types/intlCurrencySign.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 5428d7c..22c6cef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,23 @@ # Changelog Strictly follows [Semantic Versioning 2.0.0.](https://semver.org/) +## v1.10.0 +`2022-10-06`\ +\ +:rocket: Features: +- [`formatNumber()`](DOCUMENTATION.md#formatNumber), [`formatNumberToFixed()`](DOCUMENTATION.md#formatNumberToFixed), [`formatMoney()`](DOCUMENTATION.md#formatMoney) - accepts `currency` as numeric representation (e.g. `USD = 840`). +```typescript +formatMoney(1.532, { precision: 2, currency: 840 }, "pl-PL") +``` +- [`formatNumber()`](DOCUMENTATION.md#formatNumber), [`formatNumberToFixed()`](DOCUMENTATION.md#formatNumberToFixed), [`formatMoney()`](DOCUMENTATION.md#formatMoney) - accepts new option `currencySign` (Safari >= `14.1`). Defaults: `currencySign: "standard"` when `currency` value is provided. +```typescript +formatMoney(-1.532, { precision: 2, currency: 840, currencySign: "accounting" }) +``` +- [`getCurrencySymbol()`](DOCUMENTATION.md#getCurrencySymbol) - accepts new option `currencyDisplay` (Safari >= `14.1`) +```typescript +getCurrencySymbol({ currency: "CAD", currencyDisplay: "narrowSymbol" }, "en") +``` + ## v1.9.0 `2022-10-05`\ \ diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 1c3a70c..bf0f1f9 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -225,6 +225,9 @@ formatMoney(1.53588, { precision: 3, currency: "USD" }, "en"); // returns "$1.53 formatMoney(1.53588, { precision: 3, currency: "USD", currencyDisplay: "code" }, "en"); // returns "USD 1.536" formatMoney(1.532, { precision: 2, currency: "PLN" }, "pl-PL"); // returns "1,53 zł" formatMoney(1.532, { precision: 2, currency: "USD" }, "pl-PL"); // returns "1,53 USD" +formatMoney(1.532, { precision: 2, currency: "CAD" }, "en-US"); // returns "CA$1.53" +formatMoney(1.532, { precision: 2, currency: "CAD", currencyDisplay: "narrowSymbol" }, "en-US"); // returns "$1.53" +formatMoney(-1.53588, { precision: 3, currency: "USD", currencySign: "accounting" }, "en"); // returns "($1.536)" ``` ### formatMonth() @@ -252,6 +255,7 @@ arguments: - negativeZero (boolean): default `true` (current Intl default behavior) - currency (string): currency ISO Alpha 3 code (e.g. `"USD"`), default `undefined` - currencyDisplay (string): `"symbol" | "narrowSymbol" | "code" | "name"`; default `"symbol"` when `currency` is provided; see more info in the `Intl` documentation + - currencySign (string): `"standard" | "accounting"`; default `"standard"` when `currency` is provided; see more info in the `Intl` documentation - locale (optional, default: browser locale): BCP47 language tag/tags (`string` or `string[]`) or `Intl.Collator` ```typescript @@ -336,6 +340,8 @@ import { getCurrencySymbol } from '@dnvgl/i18n'; getCurrencySymbol("USD", "en-US"); // returns "$" getCurrencySymbol(840, "en-US"); // returns "$" (where 840 is the USD numeric code) +getCurrencySymbol({ currency: "CAD" }, "en-US"); // returns "CA$" +getCurrencySymbol({ currency: "CAD", currencyDisplay: "narrowSymbol" }, "en-US"); // returns "$" ``` ### formatTime() @@ -529,7 +535,7 @@ parseNumber("-1,62-sd3.454", true, "en-GB"); // returns undefined ``` ### plural() -String pattern as array or separated by characeter (default: `|`, can be customized by `separator` argument) +String pattern as array or separated by character (default: `|`, can be customized by `separator` argument) ```typescript import { plural } from '@dnvgl/i18n'; diff --git a/__tests__/formatMoney.test.ts b/__tests__/formatMoney.test.ts index 3aba974..5da1b37 100644 --- a/__tests__/formatMoney.test.ts +++ b/__tests__/formatMoney.test.ts @@ -19,7 +19,9 @@ describe('formatMoney', () => { test("using options", () => { expect(formatMoney(1.532, { precision: 2, currency: "PLN" }, "pl-PL")).toEqual(`1,53${IntlWhitespace}zł`); expect(formatMoney(1.532, { precision: 2, currency: "USD" }, "pl-PL")).toEqual(`1,53${IntlWhitespace}USD`); + expect(formatMoney(1.532, { precision: 2, currency: 840 }, "pl-PL")).toEqual(`1,53${IntlWhitespace}USD`); expect(formatMoney(1.53588, { precision: 3, currency: "USD" }, "en")).toEqual("$1.536"); + expect(formatMoney(-1.53588, { precision: 3, currency: "USD", currencySign: "accounting" }, "en")).toEqual("($1.536)"); expect(formatMoney(1.53588, { precision: 3, currency: "USD", currencyDisplay: "code" }, "en")).toEqual(`USD${IntlWhitespace}1.536`); }); @@ -27,6 +29,10 @@ describe('formatMoney', () => { expect(formatMoney(1.53588, { }, "en")).toEqual("1.54"); expect(formatMoney(1.53588, { precision: 3 }, "en")).toEqual("1.536"); expect(formatMoney(1.53588, { currency: "USD" }, "en")).toEqual("$1.54"); + expect(formatMoney(1.53588, { currency: "CAD" }, "en")).toEqual("CA$1.54"); + expect(formatMoney(1.53588, { currency: "CAD", currencyDisplay: "narrowSymbol" }, "en")).toEqual("$1.54"); + expect(formatMoney(1.53588, { currencyDisplay: "narrowSymbol" }, "en")).toEqual("1.54"); + expect(formatMoney(-1.53588, { currencySign: "accounting" }, "en")).toEqual("-1.54"); expect(formatMoney(1.53588, { currency: "USD", currencyDisplay: "code" }, "en")).toEqual(`USD${IntlWhitespace}1.54`); }); }); diff --git a/__tests__/getCurrencySymbol.test.ts b/__tests__/getCurrencySymbol.test.ts index bbd0e26..f4d12ea 100644 --- a/__tests__/getCurrencySymbol.test.ts +++ b/__tests__/getCurrencySymbol.test.ts @@ -1,4 +1,4 @@ -import { getCurrencySymbol } from "../src"; +import { CurrencySymbolOptions, getCurrencySymbol } from "../src"; describe('getCurrencySymbol', () => { test.each([ @@ -9,10 +9,30 @@ describe('getCurrencySymbol', () => { [985, "pl", "zł"], [985, "en", "PLN"] ])('returns proper value for %p currency and %p locale', (currency, locale, expected) => { - for (var i =0; i < 1000; i++) { - getCurrencySymbol(currency, locale); - } const result = getCurrencySymbol(currency, locale); expect(result).toBe(expected); }); + + test.each([ + [{ currency: "USD" }, "en", "$"], + [{ currency: "CAD" }, "en", "CA$"], + [{ currency: "CAD", currencyDisplay: "narrowSymbol" }, "en", "$"], + [{ currency: 840 }, "en", "$"], + [{ currency: "PLN" }, "pl", "zł"], + [{ currency: "PLN" }, "en", "PLN"], + [{ currency: 985 }, "pl", "zł"], + [{ currency: 985 }, "en", "PLN"] + ] as [CurrencySymbolOptions, string, string][])('returns proper value for %p options and %p locale', (currency, locale, expected) => { + const result = getCurrencySymbol(currency, locale); + expect(result).toBe(expected); + }); + + describe("given invalid parameters", () => { + it("fails to compile", () => { + // @ts-expect-error + () => getCurrencySymbol({ currencyDisplay: "narrowSymbol" }); // HINT: missing currency argument + // @ts-expect-error + () => getCurrencySymbol({ currencyDisplay: "narrowSymbol" }, "en"); // HINT: missing currency argument + }); + }); }); \ No newline at end of file diff --git a/package.json b/package.json index f45697d..2f1d0a4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dnvgl/i18n", - "version": "1.9.0", + "version": "1.10.0", "description": "A set of functions to support multiple languages/cultures in a browser or Node.js", "main": "./lib/index.js", "types": "./lib/index.d.ts", diff --git a/src/formatMoney.ts b/src/formatMoney.ts index 7547b11..41f1466 100644 --- a/src/formatMoney.ts +++ b/src/formatMoney.ts @@ -18,7 +18,8 @@ export function formatMoney(value: number, options?: number | Partial function createFormatterOptions(opts?: Partial): Intl.NumberFormatOptions { const minimumFractionDigits = opts?.minPrecision, maximumFractionDigits = opts?.maxPrecision ?? 10, - style = opts?.currency ? "currency" : "decimal"; + style = opts?.currency ? "currency" : "decimal", + currency = opts?.currency !== undefined + ? typeof opts.currency === "string" + ? opts.currency + : findIso4217Currency(opts.currency)?.alpha3Code + : undefined;   return { style: style, - currency: opts?.currency, + currency: currency, currencyDisplay: style === "currency" ? opts?.currencyDisplay ?? "symbol" : undefined, + currencySign: opts?.currencySign, minimumFractionDigits: minimumFractionDigits, maximumFractionDigits: maximumFractionDigits, useGrouping:opts?.thousandsSeparator === false ? false : true diff --git a/src/formatNumberToFixed.ts b/src/formatNumberToFixed.ts index 97e25aa..df4edc9 100644 --- a/src/formatNumberToFixed.ts +++ b/src/formatNumberToFixed.ts @@ -17,6 +17,7 @@ export function formatNumberToFixed(value: number, options?: Partial