finprim 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Oluwatosin Adelaja
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,207 @@
1
+ # finprim
2
+
3
+ **Financial primitives for modern TypeScript applications.**
4
+
5
+ A unified, production-grade TypeScript library for validating and formatting financial data. No more stitching together five different packages. No more custom glue code. Just clean, typed, fintech-first utilities that work anywhere TypeScript runs.
6
+
7
+ ---
8
+
9
+ ## Why finprim?
10
+
11
+ Every fintech team builds this internally. Sort code validation here, an IBAN check there, a custom currency formatter somewhere else. It's fragmented, inconsistent, and expensive to maintain.
12
+
13
+ finprim is the open source version of what your team has already written three times.
14
+
15
+ ---
16
+
17
+ ## Features
18
+
19
+ - ✅ IBAN validation and formatting (80+ countries, with country code)
20
+ - ✅ UK sort code and account number validation
21
+ - ✅ BIC/SWIFT validation
22
+ - ✅ Card number validation (Luhn, network detection, formatting)
23
+ - ✅ Currency validation and formatting with locale support
24
+ - ✅ Branded types for compile-time correctness
25
+ - ✅ Zod schemas out of the box
26
+ - ✅ Optional React hooks for form inputs
27
+ - ✅ Zero dependencies at the core
28
+ - ✅ Tree-shakeable ESM and CJS builds
29
+ - ✅ Fully typed
30
+
31
+ ---
32
+
33
+ ## Installation
34
+
35
+ ```bash
36
+ npm install finprim
37
+ ```
38
+
39
+ For Zod integration:
40
+
41
+ ```bash
42
+ npm install finprim zod
43
+ ```
44
+
45
+ ---
46
+
47
+ ## Usage
48
+
49
+ ### Validation
50
+
51
+ ```ts
52
+ import {
53
+ validateIBAN,
54
+ validateUKSortCode,
55
+ validateUKAccountNumber,
56
+ validateBIC,
57
+ validateCardNumber,
58
+ validateCurrencyCode,
59
+ } from 'finprim'
60
+
61
+ const iban = validateIBAN('GB29NWBK60161331926819')
62
+ // { valid: true, value: 'GB29NWBK60161331926819', formatted: 'GB29 NWBK 6016 1331 9268 19', countryCode: 'GB' }
63
+
64
+ const sortCode = validateUKSortCode('60-16-13')
65
+ // { valid: true, value: '601613', formatted: '60-16-13' }
66
+
67
+ const account = validateUKAccountNumber('31926819')
68
+ // { valid: true, value: '31926819', formatted: '3192 6819' }
69
+
70
+ const card = validateCardNumber('4532015112830366')
71
+ // { valid: true, value: '...', formatted: '4532 0151 1283 0366', network: 'Visa', last4: '0366' }
72
+ ```
73
+
74
+ ### Currency Formatting
75
+
76
+ ```ts
77
+ import { formatCurrency, parseMoney } from 'finprim'
78
+
79
+ formatCurrency(1000.5, 'GBP', 'en-GB') // '£1,000.50'
80
+ formatCurrency(1000.5, 'EUR', 'de-DE') // '1.000,50 €'
81
+ formatCurrency(1000.5, 'USD', 'en-US') // '$1,000.50'
82
+
83
+ parseMoney('£1,000.50') // { valid: true, amount: 1000.50, currency: 'GBP', formatted: '£1,000.50' }
84
+ ```
85
+
86
+ ### Branded Types
87
+
88
+ ```ts
89
+ import type { IBAN, SortCode, AccountNumber, CurrencyCode } from 'finprim'
90
+
91
+ // Invalid data cannot be passed where valid data is expected
92
+ function processPayment(iban: IBAN, amount: number) { ... }
93
+
94
+ // This forces validation before use
95
+ const iban = validateIBAN(input)
96
+ if (iban.valid) {
97
+ processPayment(iban.value, 100) // iban.value is typed as IBAN
98
+ }
99
+ ```
100
+
101
+ ### Zod Schemas
102
+
103
+ ```ts
104
+ import { ibanSchema, sortCodeSchema, accountNumberSchema, currencySchema } from 'finprim/zod'
105
+
106
+ const PaymentSchema = z.object({
107
+ iban: ibanSchema,
108
+ sortCode: sortCodeSchema,
109
+ accountNumber: accountNumberSchema,
110
+ amount: z.number().positive(),
111
+ currency: currencySchema,
112
+ })
113
+ ```
114
+
115
+ ### React Hooks
116
+
117
+ ```ts
118
+ import { useIBANInput, useCardNumberInput, useCurrencyInput } from 'finprim/react'
119
+
120
+ function PaymentForm() {
121
+ const iban = useIBANInput()
122
+ const card = useCardNumberInput()
123
+ const { rawValue, formatted, onChange } = useCurrencyInput('GBP', 'en-GB')
124
+
125
+ return (
126
+ <>
127
+ <input value={iban.value} onChange={iban.onChange} aria-invalid={iban.valid === false} />
128
+ <input value={card.formatted} onChange={card.onChange} aria-invalid={card.valid === false} />
129
+ <input value={formatted} onChange={onChange} />
130
+ </>
131
+ )
132
+ }
133
+ ```
134
+
135
+ ---
136
+
137
+ ## API Reference
138
+
139
+ ### Validation
140
+
141
+ | Function | Input | Returns |
142
+ |----------|-------|---------|
143
+ | `validateIBAN(input)` | `string` | `IBANValidationResult` (includes `countryCode` when valid) |
144
+ | `validateUKSortCode(input)` | `string` | `ValidationResult<SortCode>` |
145
+ | `validateUKAccountNumber(input)` | `string` | `ValidationResult<AccountNumber>` |
146
+ | `validateCurrencyCode(input)` | `string` | `ValidationResult<CurrencyCode>` |
147
+ | `validateBIC(input)` | `string` | `ValidationResult<BIC>` |
148
+ | `validateCardNumber(input)` | `string` | `CardValidationResult` (includes `network`, `last4` when valid) |
149
+
150
+ ### Formatting
151
+
152
+ | Function | Input | Returns |
153
+ |----------|-------|---------|
154
+ | `formatCurrency(amount, currency, locale?)` | `number`, `SupportedCurrency`, `string?` | `string` |
155
+ | `parseMoney(input)` | `string` | `MoneyResult` |
156
+
157
+ Validation results include a `formatted` string when valid (e.g. IBAN and card numbers are space-separated).
158
+
159
+ ---
160
+
161
+ ## Packages
162
+
163
+ | Import path | What it contains | Extra dependency |
164
+ |---|---|---|
165
+ | `finprim` | Core validators and formatters | none |
166
+ | `finprim/zod` | Zod schemas | `zod` |
167
+ | `finprim/react` | React hooks | `react` |
168
+
169
+ ---
170
+
171
+ ## Tech Stack
172
+
173
+ - TypeScript
174
+ - tsup (build)
175
+ - Vitest (testing)
176
+ - React (optional hooks subpath)
177
+ - Zod (optional schema subpath)
178
+
179
+ ---
180
+
181
+ ## Roadmap
182
+
183
+ - [x] SWIFT / BIC validation
184
+ - [x] Luhn algorithm for card number validation
185
+ - [ ] EU VAT number validation
186
+ - [ ] NestJS pipe integration
187
+ - [ ] More locale coverage
188
+
189
+ ---
190
+
191
+ ## Contributing
192
+
193
+ Contributions are welcome. Please open an issue before submitting a pull request so we can discuss the change.
194
+
195
+ ```bash
196
+ git clone https://github.com/YOUR_USERNAME/finprim
197
+ cd finprim
198
+ npm install
199
+ npm test
200
+ npm run dev
201
+ ```
202
+
203
+ ---
204
+
205
+ ## License
206
+
207
+ MIT
@@ -0,0 +1,50 @@
1
+ declare const __brand: unique symbol;
2
+ type Brand<T, B> = T & {
3
+ readonly [__brand]: B;
4
+ };
5
+ type IBAN = Brand<string, 'IBAN'>;
6
+ type SortCode = Brand<string, 'SortCode'>;
7
+ type AccountNumber = Brand<string, 'AccountNumber'>;
8
+ type CurrencyCode = Brand<string, 'CurrencyCode'>;
9
+ type BIC = Brand<string, 'BIC'>;
10
+ type CardNumber = Brand<string, 'CardNumber'>;
11
+ type SupportedCurrency = 'GBP' | 'EUR' | 'USD' | 'JPY' | 'CHF' | 'CAD' | 'AUD' | 'NZD';
12
+ type ValidationSuccess<T> = {
13
+ valid: true;
14
+ value: T;
15
+ formatted: string;
16
+ };
17
+ type ValidationFailure = {
18
+ valid: false;
19
+ error: string;
20
+ };
21
+ type ValidationResult<T> = ValidationSuccess<T> | ValidationFailure;
22
+ declare function isValidationSuccess<T>(result: ValidationResult<T>): result is ValidationSuccess<T>;
23
+ type IBANValidationSuccess = ValidationSuccess<IBAN> & {
24
+ countryCode: string;
25
+ };
26
+ type IBANValidationResult = IBANValidationSuccess | ValidationFailure;
27
+ type MoneyResult = {
28
+ valid: true;
29
+ amount: number;
30
+ currency: SupportedCurrency;
31
+ formatted: string;
32
+ } | {
33
+ valid: false;
34
+ error: string;
35
+ };
36
+
37
+ type CardNetwork = 'Visa' | 'Mastercard' | 'Amex' | 'Discover' | 'Unknown';
38
+ type CardValidationResult = {
39
+ valid: true;
40
+ value: CardNumber;
41
+ formatted: string;
42
+ network: CardNetwork;
43
+ last4: string;
44
+ } | {
45
+ valid: false;
46
+ error: string;
47
+ };
48
+ declare function validateCardNumber(input: string): CardValidationResult;
49
+
50
+ export { type AccountNumber as A, type BIC as B, type CurrencyCode as C, type IBANValidationResult as I, type MoneyResult as M, type SortCode as S, type ValidationResult as V, type SupportedCurrency as a, type CardNetwork as b, type CardNumber as c, type CardValidationResult as d, type IBAN as e, type IBANValidationSuccess as f, type ValidationFailure as g, type ValidationSuccess as h, isValidationSuccess as i, validateCardNumber as v };
@@ -0,0 +1,50 @@
1
+ declare const __brand: unique symbol;
2
+ type Brand<T, B> = T & {
3
+ readonly [__brand]: B;
4
+ };
5
+ type IBAN = Brand<string, 'IBAN'>;
6
+ type SortCode = Brand<string, 'SortCode'>;
7
+ type AccountNumber = Brand<string, 'AccountNumber'>;
8
+ type CurrencyCode = Brand<string, 'CurrencyCode'>;
9
+ type BIC = Brand<string, 'BIC'>;
10
+ type CardNumber = Brand<string, 'CardNumber'>;
11
+ type SupportedCurrency = 'GBP' | 'EUR' | 'USD' | 'JPY' | 'CHF' | 'CAD' | 'AUD' | 'NZD';
12
+ type ValidationSuccess<T> = {
13
+ valid: true;
14
+ value: T;
15
+ formatted: string;
16
+ };
17
+ type ValidationFailure = {
18
+ valid: false;
19
+ error: string;
20
+ };
21
+ type ValidationResult<T> = ValidationSuccess<T> | ValidationFailure;
22
+ declare function isValidationSuccess<T>(result: ValidationResult<T>): result is ValidationSuccess<T>;
23
+ type IBANValidationSuccess = ValidationSuccess<IBAN> & {
24
+ countryCode: string;
25
+ };
26
+ type IBANValidationResult = IBANValidationSuccess | ValidationFailure;
27
+ type MoneyResult = {
28
+ valid: true;
29
+ amount: number;
30
+ currency: SupportedCurrency;
31
+ formatted: string;
32
+ } | {
33
+ valid: false;
34
+ error: string;
35
+ };
36
+
37
+ type CardNetwork = 'Visa' | 'Mastercard' | 'Amex' | 'Discover' | 'Unknown';
38
+ type CardValidationResult = {
39
+ valid: true;
40
+ value: CardNumber;
41
+ formatted: string;
42
+ network: CardNetwork;
43
+ last4: string;
44
+ } | {
45
+ valid: false;
46
+ error: string;
47
+ };
48
+ declare function validateCardNumber(input: string): CardValidationResult;
49
+
50
+ export { type AccountNumber as A, type BIC as B, type CurrencyCode as C, type IBANValidationResult as I, type MoneyResult as M, type SortCode as S, type ValidationResult as V, type SupportedCurrency as a, type CardNetwork as b, type CardNumber as c, type CardValidationResult as d, type IBAN as e, type IBANValidationSuccess as f, type ValidationFailure as g, type ValidationSuccess as h, isValidationSuccess as i, validateCardNumber as v };
package/dist/index.d.mts CHANGED
@@ -1,127 +1,16 @@
1
- import { V as ValidationResult, I as IBAN, A as AccountNumber, S as SortCode, a as SupportedCurrency, M as MoneyResult, C as CurrencyCode, B as BIC, b as CardNumber } from './types-KG-eFvWt.mjs';
2
- export { c as ValidationFailure, d as ValidationSuccess } from './types-KG-eFvWt.mjs';
1
+ import { I as IBANValidationResult, V as ValidationResult, A as AccountNumber, S as SortCode, a as SupportedCurrency, M as MoneyResult, C as CurrencyCode, B as BIC } from './card-D3WC2rzV.mjs';
2
+ export { b as CardNetwork, c as CardNumber, d as CardValidationResult, e as IBAN, f as IBANValidationSuccess, g as ValidationFailure, h as ValidationSuccess, i as isValidationSuccess, v as validateCardNumber } from './card-D3WC2rzV.mjs';
3
3
 
4
- /**
5
- * Validates an IBAN string.
6
- * Accepts IBANs with or without spaces.
7
- * Validates: country code, expected length, characters, and mod97 checksum.
8
- *
9
- * @example
10
- * validateIBAN('GB29NWBK60161331926819')
11
- * // { valid: true, value: 'GB29NWBK60161331926819', formatted: 'GB29 NWBK 6016 1331 9268 19' }
12
- *
13
- * validateIBAN('GB00NWBK60161331926819')
14
- * // { valid: false, error: 'IBAN checksum is invalid' }
15
- */
16
- declare function validateIBAN(input: string): ValidationResult<IBAN>;
4
+ declare function validateIBAN(input: string): IBANValidationResult;
17
5
 
18
- /**
19
- * Validates a UK sort code.
20
- * Accepts formats: 60-16-13, 601613, 60 16 13
21
- *
22
- * @example
23
- * validateUKSortCode('60-16-13')
24
- * // { valid: true, value: '601613', formatted: '60-16-13' }
25
- *
26
- * validateUKSortCode('999')
27
- * // { valid: false, error: 'Sort code must be 6 digits...' }
28
- */
29
6
  declare function validateUKSortCode(input: string): ValidationResult<SortCode>;
30
- /**
31
- * Validates a UK bank account number.
32
- * Must be exactly 8 digits.
33
- *
34
- * @example
35
- * validateUKAccountNumber('31926819')
36
- * // { valid: true, value: '31926819', formatted: '3192 6819' }
37
- *
38
- * validateUKAccountNumber('1234')
39
- * // { valid: false, error: 'UK account number must be exactly 8 digits' }
40
- */
41
7
  declare function validateUKAccountNumber(input: string): ValidationResult<AccountNumber>;
42
8
 
43
9
  declare const SUPPORTED_CURRENCIES: SupportedCurrency[];
44
- /**
45
- * Validates a currency code against supported ISO 4217 codes.
46
- *
47
- * @example
48
- * validateCurrencyCode('GBP')
49
- * // { valid: true, value: 'GBP', formatted: 'GBP' }
50
- *
51
- * validateCurrencyCode('XYZ')
52
- * // { valid: false, error: 'Unsupported currency code: XYZ' }
53
- */
54
10
  declare function validateCurrencyCode(input: string): ValidationResult<CurrencyCode>;
55
- /**
56
- * Formats a number as a locale-aware currency string.
57
- * Uses the built-in Intl.NumberFormat API — zero dependencies.
58
- *
59
- * @example
60
- * formatCurrency(1000.5, 'GBP') // '£1,000.50'
61
- * formatCurrency(1000.5, 'EUR', 'de-DE') // '1.000,50 €'
62
- * formatCurrency(1000.5, 'USD', 'en-US') // '$1,000.50'
63
- * formatCurrency(1000, 'JPY') // '¥1,000'
64
- */
65
11
  declare function formatCurrency(amount: number, currency: SupportedCurrency, locale?: string): string;
66
- /**
67
- * Parses a formatted currency string back into a structured money object.
68
- * Detects the currency from the symbol prefix.
69
- *
70
- * @example
71
- * parseMoney('£1,000.50')
72
- * // { valid: true, amount: 1000.5, currency: 'GBP', formatted: '£1,000.50' }
73
- *
74
- * parseMoney('not money')
75
- * // { valid: false, error: 'Could not detect currency from input' }
76
- */
77
12
  declare function parseMoney(input: string): MoneyResult;
78
13
 
79
- /**
80
- * Validates a BIC (Bank Identifier Code) / SWIFT code.
81
- * Accepts both 8-character and 11-character BIC codes.
82
- *
83
- * Format: AAAABBCCXXX
84
- * - AAAA = Bank code (4 letters)
85
- * - BB = Country code (2 letters, ISO 3166-1)
86
- * - CC = Location code (2 alphanumeric)
87
- * - XXX = Branch code (3 alphanumeric, optional — 'XXX' means head office)
88
- *
89
- * @example
90
- * validateBIC('NWBKGB2L')
91
- * // { valid: true, value: 'NWBKGB2L', formatted: 'NWBKGB2L' }
92
- *
93
- * validateBIC('DEUTDEDB')
94
- * // { valid: true, value: 'DEUTDEDB', formatted: 'DEUTDEDB' }
95
- */
96
14
  declare function validateBIC(input: string): ValidationResult<BIC>;
97
15
 
98
- type CardNetwork = 'Visa' | 'Mastercard' | 'Amex' | 'Discover' | 'Unknown';
99
- type CardValidationResult = {
100
- valid: true;
101
- value: CardNumber;
102
- formatted: string;
103
- network: CardNetwork;
104
- last4: string;
105
- } | {
106
- valid: false;
107
- error: string;
108
- };
109
- /**
110
- * Validates a card number using the Luhn algorithm.
111
- * Accepts digits with or without spaces and hyphens.
112
- *
113
- * The Luhn algorithm:
114
- * 1. Double every second digit from the right
115
- * 2. If doubling produces a number > 9, subtract 9
116
- * 3. Sum all digits — result must be divisible by 10
117
- *
118
- * @example
119
- * validateCardNumber('4532015112830366')
120
- * // { valid: true, value: '...', formatted: '4532 0151 1283 0366', network: 'Visa', last4: '0366' }
121
- *
122
- * validateCardNumber('1234567890123456')
123
- * // { valid: false, error: 'Card number failed Luhn check' }
124
- */
125
- declare function validateCardNumber(input: string): CardValidationResult;
126
-
127
- export { AccountNumber, BIC, type CardNetwork, CardNumber, type CardValidationResult, CurrencyCode, IBAN, MoneyResult, SUPPORTED_CURRENCIES, SortCode, SupportedCurrency, ValidationResult, formatCurrency, parseMoney, validateBIC, validateCardNumber, validateCurrencyCode, validateIBAN, validateUKAccountNumber, validateUKSortCode };
16
+ export { AccountNumber, BIC, CurrencyCode, IBANValidationResult, MoneyResult, SUPPORTED_CURRENCIES, SortCode, SupportedCurrency, ValidationResult, formatCurrency, parseMoney, validateBIC, validateCurrencyCode, validateIBAN, validateUKAccountNumber, validateUKSortCode };
package/dist/index.d.ts CHANGED
@@ -1,127 +1,16 @@
1
- import { V as ValidationResult, I as IBAN, A as AccountNumber, S as SortCode, a as SupportedCurrency, M as MoneyResult, C as CurrencyCode, B as BIC, b as CardNumber } from './types-KG-eFvWt.js';
2
- export { c as ValidationFailure, d as ValidationSuccess } from './types-KG-eFvWt.js';
1
+ import { I as IBANValidationResult, V as ValidationResult, A as AccountNumber, S as SortCode, a as SupportedCurrency, M as MoneyResult, C as CurrencyCode, B as BIC } from './card-D3WC2rzV.js';
2
+ export { b as CardNetwork, c as CardNumber, d as CardValidationResult, e as IBAN, f as IBANValidationSuccess, g as ValidationFailure, h as ValidationSuccess, i as isValidationSuccess, v as validateCardNumber } from './card-D3WC2rzV.js';
3
3
 
4
- /**
5
- * Validates an IBAN string.
6
- * Accepts IBANs with or without spaces.
7
- * Validates: country code, expected length, characters, and mod97 checksum.
8
- *
9
- * @example
10
- * validateIBAN('GB29NWBK60161331926819')
11
- * // { valid: true, value: 'GB29NWBK60161331926819', formatted: 'GB29 NWBK 6016 1331 9268 19' }
12
- *
13
- * validateIBAN('GB00NWBK60161331926819')
14
- * // { valid: false, error: 'IBAN checksum is invalid' }
15
- */
16
- declare function validateIBAN(input: string): ValidationResult<IBAN>;
4
+ declare function validateIBAN(input: string): IBANValidationResult;
17
5
 
18
- /**
19
- * Validates a UK sort code.
20
- * Accepts formats: 60-16-13, 601613, 60 16 13
21
- *
22
- * @example
23
- * validateUKSortCode('60-16-13')
24
- * // { valid: true, value: '601613', formatted: '60-16-13' }
25
- *
26
- * validateUKSortCode('999')
27
- * // { valid: false, error: 'Sort code must be 6 digits...' }
28
- */
29
6
  declare function validateUKSortCode(input: string): ValidationResult<SortCode>;
30
- /**
31
- * Validates a UK bank account number.
32
- * Must be exactly 8 digits.
33
- *
34
- * @example
35
- * validateUKAccountNumber('31926819')
36
- * // { valid: true, value: '31926819', formatted: '3192 6819' }
37
- *
38
- * validateUKAccountNumber('1234')
39
- * // { valid: false, error: 'UK account number must be exactly 8 digits' }
40
- */
41
7
  declare function validateUKAccountNumber(input: string): ValidationResult<AccountNumber>;
42
8
 
43
9
  declare const SUPPORTED_CURRENCIES: SupportedCurrency[];
44
- /**
45
- * Validates a currency code against supported ISO 4217 codes.
46
- *
47
- * @example
48
- * validateCurrencyCode('GBP')
49
- * // { valid: true, value: 'GBP', formatted: 'GBP' }
50
- *
51
- * validateCurrencyCode('XYZ')
52
- * // { valid: false, error: 'Unsupported currency code: XYZ' }
53
- */
54
10
  declare function validateCurrencyCode(input: string): ValidationResult<CurrencyCode>;
55
- /**
56
- * Formats a number as a locale-aware currency string.
57
- * Uses the built-in Intl.NumberFormat API — zero dependencies.
58
- *
59
- * @example
60
- * formatCurrency(1000.5, 'GBP') // '£1,000.50'
61
- * formatCurrency(1000.5, 'EUR', 'de-DE') // '1.000,50 €'
62
- * formatCurrency(1000.5, 'USD', 'en-US') // '$1,000.50'
63
- * formatCurrency(1000, 'JPY') // '¥1,000'
64
- */
65
11
  declare function formatCurrency(amount: number, currency: SupportedCurrency, locale?: string): string;
66
- /**
67
- * Parses a formatted currency string back into a structured money object.
68
- * Detects the currency from the symbol prefix.
69
- *
70
- * @example
71
- * parseMoney('£1,000.50')
72
- * // { valid: true, amount: 1000.5, currency: 'GBP', formatted: '£1,000.50' }
73
- *
74
- * parseMoney('not money')
75
- * // { valid: false, error: 'Could not detect currency from input' }
76
- */
77
12
  declare function parseMoney(input: string): MoneyResult;
78
13
 
79
- /**
80
- * Validates a BIC (Bank Identifier Code) / SWIFT code.
81
- * Accepts both 8-character and 11-character BIC codes.
82
- *
83
- * Format: AAAABBCCXXX
84
- * - AAAA = Bank code (4 letters)
85
- * - BB = Country code (2 letters, ISO 3166-1)
86
- * - CC = Location code (2 alphanumeric)
87
- * - XXX = Branch code (3 alphanumeric, optional — 'XXX' means head office)
88
- *
89
- * @example
90
- * validateBIC('NWBKGB2L')
91
- * // { valid: true, value: 'NWBKGB2L', formatted: 'NWBKGB2L' }
92
- *
93
- * validateBIC('DEUTDEDB')
94
- * // { valid: true, value: 'DEUTDEDB', formatted: 'DEUTDEDB' }
95
- */
96
14
  declare function validateBIC(input: string): ValidationResult<BIC>;
97
15
 
98
- type CardNetwork = 'Visa' | 'Mastercard' | 'Amex' | 'Discover' | 'Unknown';
99
- type CardValidationResult = {
100
- valid: true;
101
- value: CardNumber;
102
- formatted: string;
103
- network: CardNetwork;
104
- last4: string;
105
- } | {
106
- valid: false;
107
- error: string;
108
- };
109
- /**
110
- * Validates a card number using the Luhn algorithm.
111
- * Accepts digits with or without spaces and hyphens.
112
- *
113
- * The Luhn algorithm:
114
- * 1. Double every second digit from the right
115
- * 2. If doubling produces a number > 9, subtract 9
116
- * 3. Sum all digits — result must be divisible by 10
117
- *
118
- * @example
119
- * validateCardNumber('4532015112830366')
120
- * // { valid: true, value: '...', formatted: '4532 0151 1283 0366', network: 'Visa', last4: '0366' }
121
- *
122
- * validateCardNumber('1234567890123456')
123
- * // { valid: false, error: 'Card number failed Luhn check' }
124
- */
125
- declare function validateCardNumber(input: string): CardValidationResult;
126
-
127
- export { AccountNumber, BIC, type CardNetwork, CardNumber, type CardValidationResult, CurrencyCode, IBAN, MoneyResult, SUPPORTED_CURRENCIES, SortCode, SupportedCurrency, ValidationResult, formatCurrency, parseMoney, validateBIC, validateCardNumber, validateCurrencyCode, validateIBAN, validateUKAccountNumber, validateUKSortCode };
16
+ export { AccountNumber, BIC, CurrencyCode, IBANValidationResult, MoneyResult, SUPPORTED_CURRENCIES, SortCode, SupportedCurrency, ValidationResult, formatCurrency, parseMoney, validateBIC, validateCurrencyCode, validateIBAN, validateUKAccountNumber, validateUKSortCode };
package/dist/index.js CHANGED
@@ -67,18 +67,20 @@ var IBAN_LENGTHS = {
67
67
  GB: 22,
68
68
  VG: 24
69
69
  };
70
+ var LETTER_A = "A".codePointAt(0);
71
+ var LETTER_Z = "Z".codePointAt(0);
72
+ var LETTER_TO_DIGIT_OFFSET = 55;
70
73
  function mod97(value) {
71
- let remainder = 0;
72
- for (const char of value) {
73
- remainder = (remainder * 10 + parseInt(char, 10)) % 97;
74
- }
75
- return remainder;
74
+ return [...value].reduce(
75
+ (remainder, char) => (remainder * 10 + Number.parseInt(char, 10)) % 97,
76
+ 0
77
+ );
76
78
  }
77
79
  function ibanToDigits(iban) {
78
80
  const rearranged = iban.slice(4) + iban.slice(0, 4);
79
- return rearranged.split("").map((char) => {
80
- const code = char.charCodeAt(0);
81
- return code >= 65 && code <= 90 ? (code - 55).toString() : char;
81
+ return [...rearranged].map((char) => {
82
+ const code = char.codePointAt(0) ?? 0;
83
+ return code >= LETTER_A && code <= LETTER_Z ? (code - LETTER_TO_DIGIT_OFFSET).toString() : char;
82
84
  }).join("");
83
85
  }
84
86
  function formatIBANString(iban) {
@@ -116,7 +118,8 @@ function validateIBAN(input) {
116
118
  return {
117
119
  valid: true,
118
120
  value: cleaned,
119
- formatted: formatIBANString(cleaned)
121
+ formatted: formatIBANString(cleaned),
122
+ countryCode
120
123
  };
121
124
  }
122
125
 
@@ -229,8 +232,8 @@ function parseMoney(input) {
229
232
  return { valid: false, error: "Could not detect currency from input. Expected a symbol like \xA3, \u20AC, $, \xA5" };
230
233
  }
231
234
  const normalised = cleaned.replace(/,/g, "");
232
- const amount = parseFloat(normalised);
233
- if (isNaN(amount)) {
235
+ const amount = Number.parseFloat(normalised);
236
+ if (Number.isNaN(amount)) {
234
237
  return { valid: false, error: `Could not parse amount from: "${cleaned}"` };
235
238
  }
236
239
  return {
@@ -302,7 +305,7 @@ function validateCardNumber(input) {
302
305
  let sum = 0;
303
306
  let shouldDouble = false;
304
307
  for (let i = digits.length - 1; i >= 0; i--) {
305
- let digit = parseInt(digits[i], 10);
308
+ let digit = Number.parseInt(digits[i], 10);
306
309
  if (shouldDouble) {
307
310
  digit *= 2;
308
311
  if (digit > 9) digit -= 9;
@@ -323,8 +326,14 @@ function validateCardNumber(input) {
323
326
  };
324
327
  }
325
328
 
329
+ // src/types.ts
330
+ function isValidationSuccess(result) {
331
+ return result.valid === true;
332
+ }
333
+
326
334
  exports.SUPPORTED_CURRENCIES = SUPPORTED_CURRENCIES;
327
335
  exports.formatCurrency = formatCurrency;
336
+ exports.isValidationSuccess = isValidationSuccess;
328
337
  exports.parseMoney = parseMoney;
329
338
  exports.validateBIC = validateBIC;
330
339
  exports.validateCardNumber = validateCardNumber;