Skip to content

Commit

Permalink
Merge pull request #4 from bchainhub/update/fix-03
Browse files Browse the repository at this point in the history
Update/fix 03
  • Loading branch information
rastislavcore authored Apr 24, 2024
2 parents cf775ab + 64e5fff commit 2ca3ee0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Configure the plugin with the following options to customize its behavior and ou

Example of customCurrencyData:

```json
```js
{
BTC: {
symbol: '',
Expand Down
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ interface CurrencyFormatterOptions extends Intl.NumberFormatOptions {
[key: string]: CurrencyData;
};
}
declare function remarkCurrencyFormatter(options: CurrencyFormatterOptions): (tree: Node) => void;
declare function remarkCurrencyFormatter(options?: CurrencyFormatterOptions): (tree: Node) => void;
export default remarkCurrencyFormatter;
11 changes: 4 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { visit } from 'unist-util-visit';
import ExchNumberFormat from 'exchange-rounding';
function remarkCurrencyFormatter(options) {
const { locale, customCurrencyData } = options;
const formatterOptions = {
customCurrency: customCurrencyData,
};
function remarkCurrencyFormatter(options = {}) {
const { locale, customCurrencyData = {} } = options;
return (tree) => {
visit(tree, 'text', (node) => {
if ('value' in node && typeof node.value === 'string') {
const text = node.value;
const regex = /\$\(([\d\.]+),?(\w+)?\)/g;
const newText = text.replace(regex, (match, amount, currencyCode) => {
const dynamicFormatterOptions = {
...formatterOptions,
customCurrency: customCurrencyData,
...(currencyCode ? { currency: currencyCode } : {})
};
const formatter = new ExchNumberFormat(locale || undefined, dynamicFormatterOptions);
const formatter = new ExchNumberFormat(locale, dynamicFormatterOptions);
return formatter.format(parseFloat(amount));
});
node.value = newText;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "remark-currency-formatter",
"version": "0.1.2",
"version": "0.1.3",
"description": "A Remark plugin to transform currency amounts into custom format.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
14 changes: 5 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,21 @@ interface CurrencyFormatterOptions extends Intl.NumberFormatOptions {
};
}

function remarkCurrencyFormatter(options: CurrencyFormatterOptions) {
const { locale, customCurrencyData } = options;

const formatterOptions = {
customCurrency: customCurrencyData,
};
function remarkCurrencyFormatter(options: CurrencyFormatterOptions = {}) {
const { locale, customCurrencyData = {} } = options;

return (tree: Node) => {
visit(tree, 'text', (node: Node) => {
if ('value' in node && typeof node.value === 'string') {
const text = node.value;
const regex = /\$\(([\d\.]+),?(\w+)?\)/g; // Regex to capture numbers and optional currency codes
const newText = text.replace(regex, (match, amount: string, currencyCode?: string) => {
// Construct formatter options dynamically, including currency only if defined
// Dynamically construct formatter options, including currency only if defined
const dynamicFormatterOptions = {
...formatterOptions,
customCurrency: customCurrencyData,
...(currencyCode ? { currency: currencyCode } : {})
};
const formatter = new ExchNumberFormat(locale || undefined, dynamicFormatterOptions);
const formatter = new ExchNumberFormat(locale, dynamicFormatterOptions);
return formatter.format(parseFloat(amount));
});

Expand Down

0 comments on commit 2ca3ee0

Please sign in to comment.