Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add currency related utility functions and update docs #96

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
id: 'formatToAUD'
title: 'formatToAUD'
sidebar_label: 'formatToAUD'
sidebar_position: 0
custom_edit_url: null
---

`import { formatToAUD } from '@orfium/toolbox';`

### Description

A function for formatting a number or number-formatted string specifically to AUD.

### Parameters

- `value: string | number` - If it's a string it must be expressing a number, otherwise you will get a runtime error
- `options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>` - An [`options`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options) compliant object, excluding the properties `currency` and `style`

### Return value

Ƭ `string`
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
id: 'formatToCAD'
title: 'formatToCAD'
sidebar_label: 'formatToCAD'
sidebar_position: 0
custom_edit_url: null
---

`import { formatToCAD } from '@orfium/toolbox';`

### Description

A function for formatting a number or number-formatted string specifically to CAD.

### Parameters

- `value: string | number` - If it's a string it must be expressing a number, otherwise you will get a runtime error
- `options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>` - An [`options`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options) compliant object, excluding the properties `currency` and `style`

### Return value

Ƭ `string`
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
id: 'formatToCNY'
title: 'formatToCNY'
sidebar_label: 'formatToCNY'
sidebar_position: 0
custom_edit_url: null
---

`import { formatToCNY } from '@orfium/toolbox';`

### Description

A function for formatting a number or number-formatted string specifically to CNY.

### Parameters

- `value: string | number` - If it's a string it must be expressing a number, otherwise you will get a runtime error
- `options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>` - An [`options`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options) compliant object, excluding the properties `currency` and `style`

### Return value

Ƭ `string`
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
id: 'formatToCurrency'
title: 'formatToCurrency'
sidebar_label: 'formatToCurrency'
sidebar_position: -1
custom_edit_url: null
---

`import { formatToCurrency } from '@orfium/toolbox';`

### Description

A generic function for formatting a number or number-formatted string to any valid currency.

### Parameters

- `value: string | number` - If it's a string it must be expressing a number, otherwise you will get a runtime error
- `currency: string` - Must be a valid ISO 4217 currency code as specified [here](https://en.wikipedia.org/wiki/ISO_4217#List_of_ISO_4217_currency_codes)
- `options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>` - An [`options`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options) compliant object, excluding the properties `currency` and `style`

### Return value

Ƭ `string`
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
id: 'formatToEUR'
title: 'formatToEUR'
sidebar_label: 'formatToEUR'
sidebar_position: 0
custom_edit_url: null
---

`import { formatToEUR } from '@orfium/toolbox';`

### Description

A function for formatting a number or number-formatted string specifically to EUR.

### Parameters

- `value: string | number` - If it's a string it must be expressing a number, otherwise you will get a runtime error
- `options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>` - An [`options`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options) compliant object, excluding the properties `currency` and `style`

### Return value

Ƭ `string`
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
id: 'formatToGBP'
title: 'formatToGBP'
sidebar_label: 'formatToGBP'
sidebar_position: 0
custom_edit_url: null
---

`import { formatToGBP } from '@orfium/toolbox';`

### Description

A function for formatting a number or number-formatted string specifically to GBP.

### Parameters

- `value: string | number` - If it's a string it must be expressing a number, otherwise you will get a runtime error
- `options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>` - An [`options`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options) compliant object, excluding the properties `currency` and `style`

### Return value

Ƭ `string`
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
id: 'formatToJPY'
title: 'formatToJPY'
sidebar_label: 'formatToJPY'
sidebar_position: 0
custom_edit_url: null
---

`import { formatToJPY } from '@orfium/toolbox';`

### Description

A function for formatting a number or number-formatted string specifically to JPY.

### Parameters

- `value: string | number` - If it's a string it must be expressing a number, otherwise you will get a runtime error
- `options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>` - An [`options`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options) compliant object, excluding the properties `currency` and `style`

### Return value

Ƭ `string`
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
id: 'formatToUSD'
title: 'formatToUSD'
sidebar_label: 'formatToUSD'
sidebar_position: 0
custom_edit_url: null
---

`import { formatToUSD } from '@orfium/toolbox';`

### Description

A function for formatting a number or number-formatted string specifically to USD.

### Parameters

- `value: string | number` - If it's a string it must be expressing a number, otherwise you will get a runtime error
- `options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>` - An [`options`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options) compliant object, excluding the properties `currency` and `style`

### Return value

Ƭ `string`
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sidebar_label: 'Optional'
sidebar_position: 1
custom_edit_url: null
---
import Optional from '../../Types/Optional.mdx';
import Optional from '../../04-Types/Optional.mdx';

<Optional />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
| :----------------------- |:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `isAuthenticated` | `boolean` |
| `isLoading` | `boolean` |
| `user` | <code>[User](../Types/User) &#124; undefined</code> |
| `getAccessTokenSilently` | <pre>(opts?: GetTokenSilentlyOptions) => Promise<<br/> void &#124; <br/> { <br/> decodedToken: [DecodedTokenResponse](../Types/DecodedTokenResponse) &#124; Record<string, never\> ; <br/> token: string; <br/> }<br/>\></pre> |
| `user` | <code>[User](../04-Types/User) &#124; undefined</code> |
| `getAccessTokenSilently` | <pre>(opts?: GetTokenSilentlyOptions) => Promise<<br/> void &#124; <br/> { <br/> decodedToken: [DecodedTokenResponse](../04-Types/DecodedTokenResponse) &#124; Record<string, never\> ; <br/> token: string; <br/> }<br/>\></pre> |
| `loginWithRedirect` | <code>(o?: RedirectLoginOptions<any\>) => Promise<void\></code> |
| `logout` | `() => void` |
2 changes: 1 addition & 1 deletion documentation/docs/api/_type-definitions/MenuItem.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
| `text` | `string` |
| `url` | `string` |
| `iconName` | `AcceptedIconNames` |
| `children?` | <code>[Optional](../utilities/Types/Optional)<[MenuItem](../Types/MenuItem), 'iconName'\>[]</code> |
| `children?` | <code>[Optional](../03-Utilities/Types/Optional)<[MenuItem](../04-Types/MenuItem), 'iconName'\>[]</code> |
6 changes: 3 additions & 3 deletions documentation/docs/api/_type-definitions/NavigationProps.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
| Name | Type |
| :--------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `regularNavigation` | <code>[MenuItem](../Types/MenuItem)[]</code> |
| `regularNavigation` | <code>[MenuItem](../04-Types/MenuItem)[]</code> |
| `navigationHeader` | `string` |
| `userIsAdmin?` | `boolean` _(default: `false`)_ |
| `hideOrgSwitcher?` | `boolean` _(default: `false`)_ |
| `adminNavigation?` | <code>[MenuItem](../Types/MenuItem)[]</code> |
| `adminNavigation?` | <code>[MenuItem](../04-Types/MenuItem)[]</code> |
| `adminNavigationHeader?` | `string` |
| `adminNavigationURLSegment?` | `string` |
| `adminButtonTooltipText?` | `string` |
| `extras?` | <code>{ title: string; menuItems: ([Omit](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys)<[MenuItem](../Types/MenuItem), 'children'\>)[] }[]</code> |
| `extras?` | <code>{ title: string; menuItems: ([Omit](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys)<[MenuItem](../04-Types/MenuItem), 'children'\>)[] }[]</code> |
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[`Product`](../Types/Product)`[] | null`
[`Product`](../04-Types/Product)`[] | null`
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
| Name | Type |
| :--------------------- | :--------------------------------------------------------------- |
| `organizations` | <code>[Organization](../Types/Organization)[]</code> |
| `selectedOrganization` | <code>[Organization](../Types/Organization)[] &#124; null</code> |
| `organizations` | <code>[Organization](../04-Types/Organization)[]</code> |
| `selectedOrganization` | <code>[Organization](../04-Types/Organization)[] &#124; null</code> |
| `switchOrganization` | `(organisation: string) => void` |
4 changes: 2 additions & 2 deletions documentation/docs/api/_type-definitions/RouteItem.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
| Name | Type |
| :--------------- | :----------------------------------------------------------------------------------------- |
| `path` | <code>string &#124; string[]</code> |
| `authorization?` | [`Authorization`](../Types/Authorization) |
| `authorization?` | [`Authorization`](../04-Types/Authorization) |
| `extraProps?` | `unknown` |
| `component?` | <code>React.FunctionComponent<[RouteComponentProps](../Types/RouteComponentProps)\></code> |
| `component?` | <code>React.FunctionComponent<[RouteComponentProps](../04-Types/RouteComponentProps)\></code> |
4 changes: 2 additions & 2 deletions documentation/docs/api/_type-definitions/RoutingStructure.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
| Name | Type |
| :-------------- | :---------------------------------------------------------------------------- |
| `fallbackPaths` | <code>Partial<Record<[FallbackPath](../Types/FallbackPath), string\>\></code> |
| `routes` | <code>[RouteItem](../Types/RouteItem)[]</code> |
| `fallbackPaths` | <code>Partial<Record<[FallbackPath](../04-Types/FallbackPath), string\>\></code> |
| `routes` | <code>[RouteItem](../04-Types/RouteItem)[]</code> |
11 changes: 0 additions & 11 deletions documentation/docs/api/components/index.md

This file was deleted.

14 changes: 0 additions & 14 deletions documentation/docs/api/hooks/index.md

This file was deleted.

11 changes: 0 additions & 11 deletions documentation/docs/api/utilities/index.md

This file was deleted.

7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ export * from './routing';
export type { Organization } from './store/organizations';
export * from './ui';
export {
formatToAUD,
formatToCAD,
formatToCurrency,
formatToEUR,
formatToGBP,
formatToJPY,
formatToUSD,
LinkWithStatePassthrough,
NavLinkWithStatePassthrough,
RedirectWithStatePassthrough,
Expand Down
60 changes: 60 additions & 0 deletions src/utils/currencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export function formatToCurrency(
value: number | string,
currency = 'USD',
options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>
) {
if (typeof value === 'string' && isNaN(Number(value))) {
throw 'A string value must be expressing a proper numeric value';
}

return new Intl.NumberFormat('en-US', {
...options,
style: 'currency',
currency: currency,
// Per spec, Intl.NumberFormat.prototype.format can accept a string value, however TS is not allowing it.
// Read this issue thread for more information: https://github.com/microsoft/TypeScript/issues/52124
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
}).format(value);
}
export function formatToUSD(
value: number | string,
options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>
) {
return formatToCurrency(value, 'USD', options);
}

export function formatToEUR(
value: number | string,
options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>
) {
return formatToCurrency(value, 'EUR', options);
}

export function formatToGBP(
value: number | string,
options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>
) {
return formatToCurrency(value, 'GBP', options);
}

export function formatToAUD(
value: number | string,
options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>
) {
return formatToCurrency(value, 'AUD', options);
}

export function formatToCAD(
value: number | string,
options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>
) {
return formatToCurrency(value, 'CAD', options);
}

export function formatToJPY(
value: number | string,
options?: Omit<Intl.NumberFormatOptions, 'currency' | 'style'>
) {
return formatToCurrency(value, 'JPY', options);
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './currencies';
export * from './LinkWithStatePassthrough';
export * from './NavLinkWithStatePassthrough';
export * from './reactQuery';
Expand Down
Loading