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

Update/cleaning 01 #11

Merged
merged 2 commits into from
Jan 23, 2025
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
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Install the `payto-rl` package using npm or yarn:

```sh
npm install payto-rl
npm i payto-rl
```

```sh
Expand Down Expand Up @@ -69,6 +69,11 @@ const bankPayto = new Payto('payto://bic/DEUTDEFF500');
console.log(bankPayto.bic); // 'DEUTDEFF500'
bankPayto.routingNumber = 123456789; // Valid 9-digit routing number
console.log(bankPayto.routingNumber); // 123456789

// Convert to JSON object
const jsonObj = payto.toJSONObject();
console.log(jsonObj.colorForeground); // Access typed properties
console.log(jsonObj['custom-field']); // Access custom properties
```

## API Reference
Expand All @@ -94,7 +99,7 @@ Creates a new Payto instance from a payto URL string.
| `bic` | `string \| null` | Bank Identifier Code (8 or 11 characters) |
| `colorBackground` | `string \| null` | Background color in hex format |
| `colorForeground` | `string \| null` | Foreground color in hex format |
| `currency` | `[string, string?, string?]` | Currency codes array |
| `currency` | `[string \| null, string \| null]` | Currency codes array |
| `deadline` | `number \| null` | Payment deadline (Unix timestamp) |
| `donate` | `boolean \| null` | Donation flag |
| `fiat` | `string \| null` | Fiat currency code |
Expand Down Expand Up @@ -131,7 +136,7 @@ Creates a new Payto instance from a payto URL string.
|--------|-------------|-------------|
| `toString()` | `string` | Returns the complete payto URL string |
| `toJSON()` | `string` | Returns a JSON string representation |
| `toJSONObject()` | `object` | Returns a plain object with all properties |
| `toJSONObject()` | `PaytoJSON` | Returns a typed object with all properties and custom fields |

## Type Safety

Expand All @@ -145,6 +150,7 @@ The library includes TypeScript type definitions and runtime validation for:
- Plus codes
- Unix timestamps
- Barcode formats
- IBAN format

## Payment System Support

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": "payto-rl",
"version": "1.0.7",
"version": "1.0.8",
"description": "PayTo Resource Locator",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
35 changes: 28 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type PaytoJSON = {
[key: string]: any;
[key: number]: never;
port?: string;
host?: string;
hostname?: string;
Expand Down Expand Up @@ -40,13 +41,14 @@ export type PaytoJSON = {
};

class Payto {
private static readonly ACCOUNT_NUMBER_REGEX = /^(\d{7,14})$/;
private static readonly BIC_REGEX = /^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/i;
private static readonly EMAIL_REGEX = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
private static readonly GEO_LOCATION_REGEX = /^(\+|-)?(?:90(?:\.0{1,6})?|(?:[0-9]|[1-8][0-9])(?:\.[0-9]{1,6})?),(\+|-)?(?:180(?:\.0{1,6})?|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:\.[0-9]{1,6})?)$/;
private static readonly IBAN_REGEX = /^[A-Z]{2}[0-9]{2}[A-Z0-9]{12,30}$/i;
private static readonly PLUS_CODE_REGEX = /^[23456789CFGHJMPQRVWX]{2,8}\+[23456789CFGHJMPQRVWX]{2,7}$/;
private static readonly ROUTING_NUMBER_REGEX = /^(\d{9})$/;
private static readonly UNIX_TIMESTAMP_REGEX = /^\d+$/;
private static readonly ACCOUNT_NUMBER_REGEX = /^(\d{7,14})$/;
private static readonly PLUS_CODE_REGEX = /^[23456789CFGHJMPQRVWX]{2,8}\+[23456789CFGHJMPQRVWX]{2,7}$/;
private static readonly GEO_LOCATION_REGEX = /^(\+|-)?(?:90(?:\.0{1,6})?|(?:[0-9]|[1-8][0-9])(?:\.[0-9]{1,6})?),(\+|-)?(?:180(?:\.0{1,6})?|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:\.[0-9]{1,6})?)$/;
private static readonly EMAIL_REGEX = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

private url: URL;

Expand Down Expand Up @@ -349,11 +351,15 @@ class Payto {
}

get iban(): string | null {
return this.getHostnameParts(this.pathname.split('/'), 'iban', 2);
const ibanStr = this.getHostnameParts(this.pathname.split('/'), 'iban', 2);
return ibanStr && Payto.IBAN_REGEX.test(ibanStr) ? ibanStr.toUpperCase() : null;
}

set iban(value: string | null) {
this.setHostnameParts(value, 2);
if (value && !Payto.IBAN_REGEX.test(value)) {
throw new Error('Invalid IBAN format');
}
this.setHostnameParts(value?.toUpperCase() ?? null, 2);
}

get item(): string | null {
Expand All @@ -369,7 +375,22 @@ class Payto {
}

get location(): string | null {
return this.searchParams.get('loc') as string | null;
const voidType = this.void;
if (!voidType) {
return null;
}

const value = this.searchParams.get('loc');
if (!value) return null;

if (voidType === 'geo') {
return Payto.GEO_LOCATION_REGEX.test(value) ? value : null;
}
if (voidType === 'plus') {
return Payto.PLUS_CODE_REGEX.test(value) ? value : null;
}

return value;
}

set location(value: string | null) {
Expand Down
131 changes: 0 additions & 131 deletions types/index.d.ts

This file was deleted.

Loading