Skip to content

Commit

Permalink
export enums as strings + update @example + fix formatting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
madoke committed May 28, 2024
1 parent d87cb1c commit 4f6f03b
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 123 deletions.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
42 changes: 29 additions & 13 deletions src/modules/finance/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -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<BitcoinNetwork, string>;
prefix: Record<BitcoinNetworkType, string>;
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',
Expand All @@ -44,7 +60,7 @@ export const BitcoinAddressSpecs: Record<
casing: 'lower',
exclude: '1bBiIoO',
},
[BitcoinAddressType.Taproot]: {
[BitcoinAddressFamily.Taproot]: {
prefix: {
[BitcoinNetwork.Mainnet]: 'bc1p',
[BitcoinNetwork.Testnet]: 'tb1p',
Expand Down
185 changes: 92 additions & 93 deletions src/modules/finance/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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.
Expand All @@ -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 };
Expand Down Expand Up @@ -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.
Expand All @@ -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 };
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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.
Expand All @@ -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 };
Expand Down Expand Up @@ -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.
Expand All @@ -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 };
Expand Down
Loading

0 comments on commit 4f6f03b

Please sign in to comment.