Skip to content

Commit

Permalink
new feature getCurrencySymbol()
Browse files Browse the repository at this point in the history
  • Loading branch information
karczk-dnv committed Oct 4, 2022
1 parent 6d986e6 commit 86e85d7
Show file tree
Hide file tree
Showing 10 changed files with 779 additions and 684 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
# Changelog
Strictly follows [Semantic Versioning 2.0.0.](https://semver.org/)

## v1.8.0
`2022-10-04`\
\
:rocket: Features:
- [`getCurrencySymbol()`](DOCUMENTATION.md#getCurrencySymbol)
```typescript
getCurrencySymbol(): string
```

:bulb: Enhancements:
- [`parseNumber()`](DOCUMENTATION.md#parseNumber) - internal optimization

:wrench: Internal:
- TypeScript upgrade `4.7.2` -> `4.8.4`

## v1.7.0
`2022-05-27`\
\
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Check tools and rules discribed below.

### Visual Studio Code + TypeScript
- Press `ctrl+shift+p` in a TypeScript file -> choose "Select TypeScript Version" -> pick "Use Workspace Version"
- verification: open TypeScript file (*.ts or *.tsx), expected example on the bottom right: `Typescript 4.7.2`
- verification: open TypeScript file (*.ts or *.tsx), expected example on the bottom right: `Typescript 4.8.4`

## 3. Project configuration/installation

Expand Down
8 changes: 8 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ getCountryCodeFromIban("BE", { validateStructure: false }); // returns "BE"
getCountryCodeFromIban("BE...", { validateStructure: false }); // returns "BE"
```

### getCurrencySymbol()
```typescript
import { getCurrencySymbol } from '@dnvgl/i18n';

getCurrencySymbol("USD""en-US"); // returns "$"
getCurrencySymbol(840"en-US"); // returns "$" (where 840 is the USD numeric code)
```

### formatTime()
```typescript
import { formatTime } from '@dnvgl/i18n';
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ See full details in the [`DOCUMENTATION.md`](DOCUMENTATION.md) file.
[`findIso3166Country()`](DOCUMENTATION.md#findIso3166Country), [`formatCountry()`](DOCUMENTATION.md#formatCountry), [`getIso3166Countries()`](DOCUMENTATION.md#getIso3166Countries), [`getStatesOfUsa()`](DOCUMENTATION.md#getStatesOfUsa), [`isEuropeanUnionMember()`](DOCUMENTATION.md#isEuropeanUnionMember), [`isValidIso3166Code()`](DOCUMENTATION.md#isValidIso3166Code)

#### Currency utils
[`findIso4217Currency()`](DOCUMENTATION.md#findIso4217Currency), [`formatCurrency()`](DOCUMENTATION.md#formatCurrency), [`getIso4217Currencies()`](DOCUMENTATION.md#getIso4217Currencies), [`isValidIso4217Code()`](DOCUMENTATION.md#isValidIso4217Code)
[`findIso4217Currency()`](DOCUMENTATION.md#findIso4217Currency), [`formatCurrency()`](DOCUMENTATION.md#formatCurrency), [`getIso4217Currencies()`](DOCUMENTATION.md#getIso4217Currencies), [`getCurrencySymbol()`](DOCUMENTATION.md#getCurrencySymbol), [`isValidIso4217Code()`](DOCUMENTATION.md#isValidIso4217Code)

#### Financial utils
[`formatMoney()`](DOCUMENTATION.md#formatMoney), [`formatIban()`](DOCUMENTATION.md#formatIban), [`getCountryCodeFromBic()`](DOCUMENTATION.md#getCountryCodeFromBic), [`getCountryCodeFromIban()`](DOCUMENTATION.md#getCountryCodeFromIban), [`roundUsingBankersMethod()`](DOCUMENTATION.md#roundUsingBankersMethod)
Expand Down
1 change: 1 addition & 0 deletions __tests__/formatNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('formatNumber', () => {
[1234.78, "de", false, "1234,78"],
[1234.78, "de", true, "1.234,78"],
[1234567.78, "de", true, "1.234.567,78"],
[0.000000003, "en", false, "0.000000003"], // HINT: should not be 3e-9
[1234567.78, "pl", true, `1${IntlWhitespace}234${IntlWhitespace}567,78`],
])('formats %p with proper locale (%p) when thousands separator option is %p', (value, locale, thousandsSeparator, expected) => {
const result = formatNumber(value, { thousandsSeparator: thousandsSeparator }, locale);
Expand Down
18 changes: 18 additions & 0 deletions __tests__/getCurrencySymbol.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getCurrencySymbol } from "../src";

describe('getCurrencySymbol', () => {
test.each([
["USD", "en", "$"],
[840, "en", "$"],
["PLN", "pl", "zł"],
["PLN", "en", "PLN"],
[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);
});
});
16 changes: 16 additions & 0 deletions src/getCurrencySymbol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { findIso4217Currency } from "./findIso4217Currency";
import { getSeparator } from "./internal/getSeparator";
import { Iso4217Alpha3Code, Iso4217NumericCode } from "./types/iso4217";
import { Locale } from "./types/locale";

export function getCurrencySymbol(currencyCode: Iso4217Alpha3Code | Iso4217NumericCode, locale?: Locale): string {
const code = typeof currencyCode === "string"
? currencyCode
: findIso4217Currency(currencyCode)?.alpha3Code;

if (!code) {
return "";
}

return getSeparator(1, "currency", { style: "currency", currency: code, currencyDisplay: "symbol" }, locale);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export { formatString } from './formatString';
export { formatTime } from './formatTime';
export { getCountryCodeFromBic } from './getCountryCodeFromBic';
export { getCountryCodeFromIban } from './getCountryCodeFromIban';
export { getCurrencySymbol } from './getCurrencySymbol';
export { getDateFnsFormat } from './getDateFnsFormat';
export { getDecimalSeparator } from './getDecimalSeparator';
export { getIso3166Countries } from './getIso3166Countries';
Expand Down
2 changes: 1 addition & 1 deletion src/parseNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function parseNumber(value: string, strictMode: boolean = false, locale?:
if (strictMode && isEmptyOrWhiteSpace(preParsedValue)) return undefined;
const parsedNumber = (strictMode ? Number : parseFloat)(preParsedValue);

return (Number.isNaN(parsedNumber) || !Number.isFinite(parsedNumber))
return !Number.isFinite(parsedNumber)
? undefined
: (parsedNumber || 0 /* HINT: -0 case */);
}
Expand Down
Loading

0 comments on commit 86e85d7

Please sign in to comment.