diff --git a/src/index.ts b/src/index.ts index b5676f4475a..ae2d99f21e7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -55,6 +55,10 @@ export type { DatabaseModule } from './modules/database'; export type { DatatypeModule } from './modules/datatype'; export type { DateModule, SimpleDateModule } from './modules/date'; export type { Currency, FinanceModule } from './modules/finance'; +export type { + BitcoinAddressFamilyType, + BitcoinNetworkType, +} from './modules/finance/bitcoin'; export type { FoodModule } from './modules/food'; export type { GitModule } from './modules/git'; export type { HackerModule } from './modules/hacker'; diff --git a/src/modules/finance/bitcoin.ts b/src/modules/finance/bitcoin.ts index 359091315cc..7e7057323c5 100644 --- a/src/modules/finance/bitcoin.ts +++ b/src/modules/finance/bitcoin.ts @@ -1,41 +1,57 @@ import type { Casing } from '../string'; -export enum BitcoinAddressType { - Legacy, - Segwit, - Bech32, - Taproot, +/** + * The bitcoin address families + */ +export enum BitcoinAddressFamily { + Legacy = 'legacy', + Segwit = 'segwit', + Bech32 = 'bech32', + Taproot = 'taproot', } +/** + * The bitcoin address families + */ +export type BitcoinAddressFamilyType = `${BitcoinAddressFamily}`; + +/** + * The different bitcoin networks + */ export enum BitcoinNetwork { - Mainnet, - Testnet, + Mainnet = 'mainnet', + Testnet = 'testnet', } +/** + * The different bitcoin networks + */ +export type BitcoinNetworkType = `${BitcoinNetwork}`; + type BitcoinAddressOptions = { - prefix: Record; + prefix: Record; length: { min: number; max: number }; casing: Casing; exclude: string; }; export const BitcoinAddressSpecs: Record< - BitcoinAddressType, + BitcoinAddressFamilyType, BitcoinAddressOptions > = { - [BitcoinAddressType.Legacy]: { + [BitcoinAddressFamily.Legacy]: { prefix: { [BitcoinNetwork.Mainnet]: '1', [BitcoinNetwork.Testnet]: 'm' }, length: { min: 26, max: 34 }, casing: 'mixed', exclude: '0OIl', }, - [BitcoinAddressType.Segwit]: { + [BitcoinAddressFamily.Segwit]: { prefix: { [BitcoinNetwork.Mainnet]: '3', [BitcoinNetwork.Testnet]: '2' }, length: { min: 26, max: 34 }, casing: 'mixed', exclude: '0OIl', }, - [BitcoinAddressType.Bech32]: { + [BitcoinAddressFamily.Bech32]: { prefix: { [BitcoinNetwork.Mainnet]: 'bc1', [BitcoinNetwork.Testnet]: 'tb1', @@ -44,7 +60,7 @@ export const BitcoinAddressSpecs: Record< casing: 'lower', exclude: '1bBiIoO', }, - [BitcoinAddressType.Taproot]: { + [BitcoinAddressFamily.Taproot]: { prefix: { [BitcoinNetwork.Mainnet]: 'bc1p', [BitcoinNetwork.Testnet]: 'tb1p', diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 0db97ed1d08..3ec19bf5b92 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -1,9 +1,11 @@ import { FakerError } from '../../errors/faker-error'; import { ModuleBase } from '../../internal/module-base'; import { + BitcoinAddressFamily, BitcoinAddressSpecs, - BitcoinAddressType, BitcoinNetwork, + type BitcoinAddressFamilyType, + type BitcoinNetworkType, } from './bitcoin'; import iban from './iban'; @@ -106,13 +108,13 @@ export class FinanceModule extends ModuleBase { optionsOrLength?: | number | { - /** - * The length of the account number. - * - * @default 8 - */ - length?: number; - } + /** + * The length of the account number. + * + * @default 8 + */ + length?: number; + } ): string; /** * Generates a random account number. @@ -131,13 +133,13 @@ export class FinanceModule extends ModuleBase { options: | number | { - /** - * The length of the account number. - * - * @default 8 - */ - length?: number; - } = {} + /** + * The length of the account number. + * + * @default 8 + */ + length?: number; + } = {} ): string { if (typeof options === 'number') { options = { length: options }; @@ -260,25 +262,25 @@ export class FinanceModule extends ModuleBase { optionsOrLength?: | number | { - /** - * The length of the unmasked number. - * - * @default 4 - */ - length?: number; - /** - * Whether to use surrounding parenthesis. - * - * @default true - */ - parens?: boolean; - /** - * Whether to prefix the numbers with an ellipsis. - * - * @default true - */ - ellipsis?: boolean; - } + /** + * The length of the unmasked number. + * + * @default 4 + */ + length?: number; + /** + * Whether to use surrounding parenthesis. + * + * @default true + */ + parens?: boolean; + /** + * Whether to prefix the numbers with an ellipsis. + * + * @default true + */ + ellipsis?: boolean; + } ): string; /** * Generates a random masked number. @@ -301,25 +303,25 @@ export class FinanceModule extends ModuleBase { options: | number | { - /** - * The length of the unmasked number. - * - * @default 4 - */ - length?: number; - /** - * Whether to use surrounding parenthesis. - * - * @default true - */ - parens?: boolean; - /** - * Whether to prefix the numbers with an ellipsis. - * - * @default true - */ - ellipsis?: boolean; - } = {} + /** + * The length of the unmasked number. + * + * @default 4 + */ + length?: number; + /** + * Whether to use surrounding parenthesis. + * + * @default true + */ + parens?: boolean; + /** + * Whether to prefix the numbers with an ellipsis. + * + * @default true + */ + ellipsis?: boolean; + } = {} ): string { if (typeof options === 'number') { options = { length: options }; @@ -493,38 +495,35 @@ export class FinanceModule extends ModuleBase { * * @param options An optional options object. * @param options.type The bitcoin address type (`'legacy'`, `'sewgit'`, `'bech32'` or `'taproot'`). Defaults to a random address type. - * @param options.network The bitcoin network (`'mainnet'` or `'testnet'`). Defaults to `'mainnet'`. + * @param options.network The bitcoin network (`'mainnet'` or `'testnet'`). Defaults to `'Mainnet'`. * * @example * faker.finance.bitcoinAddress() // '1TeZEFLmGPLEQrSRdAcnZLoWwYeiHwmRog' * - * enum BitcoinAddressType { Legacy, Segwit, Bech32, Taproot } - * faker.finance.bitcoinAddress({ type: BitcoinAddressType.Bech32 }) // 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4' + * faker.finance.bitcoinAddress({ type: 'bech32' }) // 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4' * - * enum BitcoinAddressType { Legacy, Segwit, Bech32, Taproot } - * enum BitcoinNetwork { Mainnet, Testnet } - * faker.finance.bitcoinAddress({ type: BitcoinAddressType.Bech32, network: BitcoinNetwork.Testnet }) // 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx' + * faker.finance.bitcoinAddress({ type: 'bech32', network: 'testnet' }) // 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx' * * @since 3.1.0 */ bitcoinAddress( options: { /** - * The bitcoin address type (`'Legacy'`, `'Sewgit'`, `'Bech32'` or `'Taproot'`). + * The bitcoin address type (`'legacy'`, `'sewgit'`, `'bech32'` or `'taproot'`). * - * @default faker.helpers.enumValue(BitcoinAddressType) + * @default faker.helpers.arrayElement(['legacy','sewgit','bech32','taproot']) */ - type?: BitcoinAddressType; + type?: BitcoinAddressFamilyType; /** - * The bitcoin network (`'Mainnet'` or `'Testnet'`). + * The bitcoin network (`'mainnet'` or `'testnet'`). * * @default 'mainnet' */ - network?: BitcoinNetwork; + network?: BitcoinNetworkType; } = {} ): string { const { - type = this.faker.helpers.enumValue(BitcoinAddressType), + type = this.faker.helpers.enumValue(BitcoinAddressFamily), network = BitcoinNetwork.Mainnet, } = options || {}; const addressSpec = BitcoinAddressSpecs[type]; @@ -613,13 +612,13 @@ export class FinanceModule extends ModuleBase { options?: | string | { - /** - * The name of the issuer (case-insensitive) or the format used to generate one. - * - * @default '' - */ - issuer?: string; - } + /** + * The name of the issuer (case-insensitive) or the format used to generate one. + * + * @default '' + */ + issuer?: string; + } ): string; /** * Generates a random credit card number. @@ -639,13 +638,13 @@ export class FinanceModule extends ModuleBase { options: | string | { - /** - * The name of the issuer (case-insensitive) or the format used to generate one. - * - * @default '' - */ - issuer?: string; - } = {} + /** + * The name of the issuer (case-insensitive) or the format used to generate one. + * + * @default '' + */ + issuer?: string; + } = {} ): string { if (typeof options === 'string') { options = { issuer: options }; @@ -753,13 +752,13 @@ export class FinanceModule extends ModuleBase { options?: | number | { - /** - * The length of the PIN to generate. - * - * @default 4 - */ - length?: number; - } + /** + * The length of the PIN to generate. + * + * @default 4 + */ + length?: number; + } ): string; /** * Generates a random PIN number. @@ -780,13 +779,13 @@ export class FinanceModule extends ModuleBase { options: | number | { - /** - * The length of the PIN to generate. - * - * @default 4 - */ - length?: number; - } = {} + /** + * The length of the PIN to generate. + * + * @default 4 + */ + length?: number; + } = {} ): string { if (typeof options === 'number') { options = { length: options }; diff --git a/test/modules/finance.spec.ts b/test/modules/finance.spec.ts index b8c3c3fb11d..a9d61791118 100644 --- a/test/modules/finance.spec.ts +++ b/test/modules/finance.spec.ts @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest'; import { faker, fakerZH_CN } from '../../src'; import { FakerError } from '../../src/errors/faker-error'; import { - BitcoinAddressType, + BitcoinAddressFamily, BitcoinNetwork, } from '../../src/modules/finance/bitcoin'; import ibanLib from '../../src/modules/finance/iban'; @@ -96,9 +96,9 @@ describe('finance', () => { t.describe('bitcoinAddress', (t) => { t.it('noArgs') - .it('with type option', { type: BitcoinAddressType.Legacy }) + .it('with type option', { type: BitcoinAddressFamily.Legacy }) .it('with type and network option', { - type: BitcoinAddressType.Legacy, + type: BitcoinAddressFamily.Legacy, network: BitcoinNetwork.Mainnet, }); }); @@ -344,13 +344,13 @@ describe('finance', () => { }); it.each([ - [BitcoinAddressType.Legacy, legacy], - [BitcoinAddressType.Segwit, segwit], - [BitcoinAddressType.Bech32, bech32], - [BitcoinAddressType.Taproot, taproot], + [BitcoinAddressFamily.Legacy, legacy], + [BitcoinAddressFamily.Segwit, segwit], + [BitcoinAddressFamily.Bech32, bech32], + [BitcoinAddressFamily.Taproot, taproot], ])( 'should handle the type = $type argument', - (type: BitcoinAddressType, regex: RegExp) => { + (type: BitcoinAddressFamily, regex: RegExp) => { const bitcoinAddress = faker.finance.bitcoinAddress({ type, }); @@ -382,18 +382,18 @@ describe('finance', () => { ); it.each([ - [BitcoinAddressType.Legacy, BitcoinNetwork.Mainnet, '1'], - [BitcoinAddressType.Legacy, BitcoinNetwork.Testnet, 'm'], - [BitcoinAddressType.Segwit, BitcoinNetwork.Mainnet, '3'], - [BitcoinAddressType.Segwit, BitcoinNetwork.Testnet, '2'], - [BitcoinAddressType.Bech32, BitcoinNetwork.Mainnet, 'bc1'], - [BitcoinAddressType.Bech32, BitcoinNetwork.Testnet, 'tb1'], - [BitcoinAddressType.Taproot, BitcoinNetwork.Mainnet, 'bc1p'], - [BitcoinAddressType.Taproot, BitcoinNetwork.Testnet, 'tb1p'], + [BitcoinAddressFamily.Legacy, BitcoinNetwork.Mainnet, '1'], + [BitcoinAddressFamily.Legacy, BitcoinNetwork.Testnet, 'm'], + [BitcoinAddressFamily.Segwit, BitcoinNetwork.Mainnet, '3'], + [BitcoinAddressFamily.Segwit, BitcoinNetwork.Testnet, '2'], + [BitcoinAddressFamily.Bech32, BitcoinNetwork.Mainnet, 'bc1'], + [BitcoinAddressFamily.Bech32, BitcoinNetwork.Testnet, 'tb1'], + [BitcoinAddressFamily.Taproot, BitcoinNetwork.Mainnet, 'bc1p'], + [BitcoinAddressFamily.Taproot, BitcoinNetwork.Testnet, 'tb1p'], ])( 'should handle the type = $type and network = $network arguments', ( - type: BitcoinAddressType, + type: BitcoinAddressFamily, network: BitcoinNetwork, expectedPrefix: string ) => {