From dd6b63511ede3a66ff0bec5a3f118fc385b57115 Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Thu, 22 Jun 2023 17:14:22 +0200 Subject: [PATCH 1/4] feat: improve decode payment regex --- packages/ledger/src/utils/payment.utils.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/ledger/src/utils/payment.utils.ts b/packages/ledger/src/utils/payment.utils.ts index 004476268..83ded25aa 100644 --- a/packages/ledger/src/utils/payment.utils.ts +++ b/packages/ledger/src/utils/payment.utils.ts @@ -23,15 +23,32 @@ export const decodePayment = ( code: string ): { token: string; identifier: string; amount?: number } | undefined => { const regex = - /^([a-zA-Z]+):([A-Za-z0-9:\-.]+).*?(?:[?&](?:amount|value)=(\d+(?:\.\d+)?))?$/; + /^([a-zA-Z0-9%_.~+-]+):([a-zA-Z0-9%_.~+-]+(?:\/)?([a-zA-Z0-9%_.~+/-]+)?)\??((?:[a-zA-Z0-9%_.~+-]+=[a-zA-Z0-9%_.~+-]+(?:[&;])?)*)$/; const match = code.match(regex); + if (isNullish(match)) { return undefined; } // eslint-disable-next-line @typescript-eslint/no-unused-vars - const [_, token, identifier, amount] = match; + const [_code_, token, identifier, _undefined_, parameters] = match; + + const decodeAmount = (): string | undefined => { + const regex = /([a-zA-Z0-9%_.~+-]+)=([a-zA-Z0-9%_.~+-]+)([&;])?/; + const match = (parameters ?? "").match(regex); + + if (isNullish(match)) { + return undefined; + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const [_amount_, _param_, value] = match; + + return value; + }; + + const amount = decodeAmount(); return { token, From a54fbf11b1218e2e8f83eb1e251128f0a2a058bf Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Thu, 22 Jun 2023 17:15:28 +0200 Subject: [PATCH 2/4] feat: non capturing --- packages/ledger/src/utils/payment.utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ledger/src/utils/payment.utils.ts b/packages/ledger/src/utils/payment.utils.ts index 83ded25aa..2eba95663 100644 --- a/packages/ledger/src/utils/payment.utils.ts +++ b/packages/ledger/src/utils/payment.utils.ts @@ -23,7 +23,7 @@ export const decodePayment = ( code: string ): { token: string; identifier: string; amount?: number } | undefined => { const regex = - /^([a-zA-Z0-9%_.~+-]+):([a-zA-Z0-9%_.~+-]+(?:\/)?([a-zA-Z0-9%_.~+/-]+)?)\??((?:[a-zA-Z0-9%_.~+-]+=[a-zA-Z0-9%_.~+-]+(?:[&;])?)*)$/; + /^([a-zA-Z0-9%_.~+-]+):([a-zA-Z0-9%_.~+-]+(?:\/)?([a-zA-Z0-9%_.~+/-]+)?)(?:\?)?((?:[a-zA-Z0-9%_.~+-]+=[a-zA-Z0-9%_.~+-]+(?:[&;])?)*)$/; const match = code.match(regex); From 8d2a3ccebfcdc41bdfc4f8c3ac2dd6727b218cb2 Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Fri, 23 Jun 2023 10:18:28 +0200 Subject: [PATCH 3/4] feat: extract amount --- packages/ledger/src/utils/payment.utils.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/ledger/src/utils/payment.utils.ts b/packages/ledger/src/utils/payment.utils.ts index 2eba95663..1f1e7e564 100644 --- a/packages/ledger/src/utils/payment.utils.ts +++ b/packages/ledger/src/utils/payment.utils.ts @@ -23,7 +23,7 @@ export const decodePayment = ( code: string ): { token: string; identifier: string; amount?: number } | undefined => { const regex = - /^([a-zA-Z0-9%_.~+-]+):([a-zA-Z0-9%_.~+-]+(?:\/)?([a-zA-Z0-9%_.~+/-]+)?)(?:\?)?((?:[a-zA-Z0-9%_.~+-]+=[a-zA-Z0-9%_.~+-]+(?:[&;])?)*)$/; + /^([a-zA-Z0-9%_.~+-]+):([a-zA-Z0-9%_.~+-]+\/?([a-zA-Z0-9%_.~+/-]+)?)\??((?:[a-zA-Z0-9%_.~+-]+=[a-zA-Z0-9%_.~+-]+(?:[&;])?)*)$/; const match = code.match(regex); @@ -35,15 +35,18 @@ export const decodePayment = ( const [_code_, token, identifier, _undefined_, parameters] = match; const decodeAmount = (): string | undefined => { - const regex = /([a-zA-Z0-9%_.~+-]+)=([a-zA-Z0-9%_.~+-]+)([&;])?/; - const match = (parameters ?? "").match(regex); + const regex = /([a-zA-Z0-9%_.~+-]+)=([a-zA-Z0-9%_.~+-]+)([&;])?/g; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const match = [...(parameters ?? "").matchAll(regex)].find(([_param_, key]) => + /amount|value/.test(key) + ); if (isNullish(match)) { - return undefined; + return undefined } // eslint-disable-next-line @typescript-eslint/no-unused-vars - const [_amount_, _param_, value] = match; + const [_param_, _key_, value] = match; return value; }; From 16d28b32cb2a465f3ba47509eacb9bcd2ae6631e Mon Sep 17 00:00:00 2001 From: Formatting Committer Date: Fri, 23 Jun 2023 08:19:26 +0000 Subject: [PATCH 4/4] Updating formatting --- packages/ledger/src/utils/payment.utils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/ledger/src/utils/payment.utils.ts b/packages/ledger/src/utils/payment.utils.ts index 1f1e7e564..00f25883c 100644 --- a/packages/ledger/src/utils/payment.utils.ts +++ b/packages/ledger/src/utils/payment.utils.ts @@ -37,12 +37,12 @@ export const decodePayment = ( const decodeAmount = (): string | undefined => { const regex = /([a-zA-Z0-9%_.~+-]+)=([a-zA-Z0-9%_.~+-]+)([&;])?/g; // eslint-disable-next-line @typescript-eslint/no-unused-vars - const match = [...(parameters ?? "").matchAll(regex)].find(([_param_, key]) => - /amount|value/.test(key) + const match = [...(parameters ?? "").matchAll(regex)].find( + ([_param_, key]) => /amount|value/.test(key) ); if (isNullish(match)) { - return undefined + return undefined; } // eslint-disable-next-line @typescript-eslint/no-unused-vars