Skip to content

Commit

Permalink
Merge branch 'next' into Use-singular-locale-definition-keys-faker-js…
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmayer authored Jun 5, 2024
2 parents 5b9b787 + 3ae9393 commit 69c0f25
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 44 deletions.
3 changes: 1 addition & 2 deletions scripts/generate-locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ async function normalizeLocaleFile(filePath: string, definitionKey: string) {

const isDynamicFile = compareString.startsWith('mergeArrays');
const isNonApplicable = compareString.startsWith('null');
const isFrozenData = compareString.startsWith('Object.freeze');
if (isDynamicFile || isNonApplicable || isFrozenData) {
if (isDynamicFile || isNonApplicable) {
return;
}

Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ 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 {
BitcoinAddressFamily,
BitcoinNetwork,
} from './modules/finance/bitcoin';
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
4 changes: 2 additions & 2 deletions src/locales/en/science/chemical_element.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default Object.freeze([
export default [
{
symbol: 'H',
name: 'Hydrogen',
Expand Down Expand Up @@ -589,4 +589,4 @@ export default Object.freeze([
name: 'Oganesson',
atomicNumber: 118,
},
]);
];
4 changes: 2 additions & 2 deletions src/locales/en/science/unit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default Object.freeze([
export default [
{
name: 'meter',
symbol: 'm',
Expand Down Expand Up @@ -115,4 +115,4 @@ export default Object.freeze([
name: 'katal',
symbol: 'kat',
},
]);
];
4 changes: 2 additions & 2 deletions src/locales/eo/science/chemical_element.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default Object.freeze([
export default [
{
symbol: 'H',
name: 'hidrogeno',
Expand Down Expand Up @@ -589,4 +589,4 @@ export default Object.freeze([
name: 'oganesono',
atomicNumber: 118,
},
]);
];
4 changes: 2 additions & 2 deletions src/locales/eo/science/unit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default Object.freeze([
export default [
{
name: 'metro',
symbol: 'm',
Expand Down Expand Up @@ -115,4 +115,4 @@ export default Object.freeze([
name: 'katalo',
symbol: 'kat',
},
]);
];
4 changes: 2 additions & 2 deletions src/locales/nb_NO/science/chemical_element.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default Object.freeze([
export default [
{
symbol: 'H',
name: 'Hydrogen',
Expand Down Expand Up @@ -589,4 +589,4 @@ export default Object.freeze([
name: 'Oganesson',
atomicNumber: 118,
},
]);
];
4 changes: 2 additions & 2 deletions src/locales/nb_NO/science/unit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default Object.freeze([
export default [
{
name: 'meter',
symbol: 'm',
Expand Down Expand Up @@ -87,4 +87,4 @@ export default Object.freeze([
name: 'sievert',
symbol: 'Sv',
},
]);
];
4 changes: 2 additions & 2 deletions src/locales/pl/science/chemical_element.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default Object.freeze([
export default [
{
symbol: 'H',
name: 'Wodór',
Expand Down Expand Up @@ -589,4 +589,4 @@ export default Object.freeze([
name: 'Oganeson',
atomicNumber: 118,
},
]);
];
4 changes: 2 additions & 2 deletions src/locales/pl/science/unit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default Object.freeze([
export default [
{
name: 'metr',
symbol: 'm',
Expand Down Expand Up @@ -87,4 +87,4 @@ export default Object.freeze([
name: 'siwert',
symbol: 'Sv',
},
]);
];
4 changes: 2 additions & 2 deletions src/locales/zh_CN/science/chemical_element.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default Object.freeze([
export default [
{
symbol: 'H',
name: '氢',
Expand Down Expand Up @@ -589,4 +589,4 @@ export default Object.freeze([
name: '鿫',
atomicNumber: 118,
},
]);
];
4 changes: 2 additions & 2 deletions src/locales/zh_CN/science/unit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default Object.freeze([
export default [
{
name: '米',
symbol: 'm',
Expand Down Expand Up @@ -115,4 +115,4 @@ export default Object.freeze([
name: '开特',
symbol: 'kat',
},
]);
];
72 changes: 72 additions & 0 deletions src/modules/finance/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { Casing } from '../string';

/**
* 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 = 'mainnet',
Testnet = 'testnet',
}

/**
* The different bitcoin networks.
*/
export type BitcoinNetworkType = `${BitcoinNetwork}`;

type BitcoinAddressOptions = {
prefix: Record<BitcoinNetworkType, string>;
length: { min: number; max: number };
casing: Casing;
exclude: string;
};

export const BitcoinAddressSpecs: Record<
BitcoinAddressFamilyType,
BitcoinAddressOptions
> = {
[BitcoinAddressFamily.Legacy]: {
prefix: { [BitcoinNetwork.Mainnet]: '1', [BitcoinNetwork.Testnet]: 'm' },
length: { min: 26, max: 34 },
casing: 'mixed',
exclude: '0OIl',
},
[BitcoinAddressFamily.Segwit]: {
prefix: { [BitcoinNetwork.Mainnet]: '3', [BitcoinNetwork.Testnet]: '2' },
length: { min: 26, max: 34 },
casing: 'mixed',
exclude: '0OIl',
},
[BitcoinAddressFamily.Bech32]: {
prefix: {
[BitcoinNetwork.Mainnet]: 'bc1',
[BitcoinNetwork.Testnet]: 'tb1',
},
length: { min: 42, max: 42 },
casing: 'lower',
exclude: '1bBiIoO',
},
[BitcoinAddressFamily.Taproot]: {
prefix: {
[BitcoinNetwork.Mainnet]: 'bc1p',
[BitcoinNetwork.Testnet]: 'tb1p',
},
length: { min: 62, max: 62 },
casing: 'lower',
exclude: '1bBiIoO',
},
};
53 changes: 42 additions & 11 deletions src/modules/finance/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { FakerError } from '../../errors/faker-error';
import { ModuleBase } from '../../internal/module-base';
import type { BitcoinAddressFamilyType, BitcoinNetworkType } from './bitcoin';
import {
BitcoinAddressFamily,
BitcoinAddressSpecs,
BitcoinNetwork,
} from './bitcoin';
import iban from './iban';

/**
Expand Down Expand Up @@ -486,23 +492,48 @@ export class FinanceModule extends ModuleBase {
/**
* Generates a random Bitcoin address.
*
* @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'`.
*
* @example
* faker.finance.bitcoinAddress() // '3ySdvCkTLVy7gKD4j6JfSaf5d'
* faker.finance.bitcoinAddress() // '1TeZEFLmGPLEQrSRdAcnZLoWwYeiHwmRog'
* faker.finance.bitcoinAddress({ type: 'bech32' }) // 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'
* faker.finance.bitcoinAddress({ type: 'bech32', network: 'testnet' }) // 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx'
*
* @since 3.1.0
*/
bitcoinAddress(): string {
const addressLength = this.faker.number.int({ min: 25, max: 39 });

let address = this.faker.helpers.arrayElement(['1', '3']);

address += this.faker.string.alphanumeric({
length: addressLength,
casing: 'mixed',
exclude: '0OIl',
bitcoinAddress(
options: {
/**
* The bitcoin address type (`'legacy'`, `'sewgit'`, `'bech32'` or `'taproot'`).
*
* @default faker.helpers.arrayElement(['legacy','sewgit','bech32','taproot'])
*/
type?: BitcoinAddressFamilyType;
/**
* The bitcoin network (`'mainnet'` or `'testnet'`).
*
* @default 'mainnet'
*/
network?: BitcoinNetworkType;
} = {}
): string {
const {
type = this.faker.helpers.enumValue(BitcoinAddressFamily),
network = BitcoinNetwork.Mainnet,
} = options;
const addressSpec = BitcoinAddressSpecs[type];
const addressPrefix = addressSpec.prefix[network];
const addressLength = this.faker.number.int(addressSpec.length);

const address = this.faker.string.alphanumeric({
length: addressLength - addressPrefix.length,
casing: addressSpec.casing,
exclude: addressSpec.exclude,
});

return address;
return addressPrefix + address;
}

/**
Expand Down
18 changes: 15 additions & 3 deletions test/modules/__snapshots__/finance.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ exports[`finance > 42 > bic > noArgs 1`] = `"YTPECC2VXXX"`;

exports[`finance > 42 > bic > with branch code 1`] = `"JYTPCD52XXX"`;

exports[`finance > 42 > bitcoinAddress 1`] = `"3JAaa4SAH2YQdbbiwrhB9hnsMcvA3Ba"`;
exports[`finance > 42 > bitcoinAddress > noArgs 1`] = `"3JAaa4SAH2YQdbbiwrhB9hnsMcvA3Ba4XY"`;

exports[`finance > 42 > bitcoinAddress > with type and network option 1`] = `"1XJAaa4SAH2YQdbbiwrhB9hnsMcvA"`;

exports[`finance > 42 > bitcoinAddress > with type option 1`] = `"1XJAaa4SAH2YQdbbiwrhB9hnsMcvA"`;

exports[`finance > 42 > creditCardCVV 1`] = `"397"`;

Expand Down Expand Up @@ -108,7 +112,11 @@ exports[`finance > 1211 > bic > noArgs 1`] = `"XFZROMRC"`;

exports[`finance > 1211 > bic > with branch code 1`] = `"YXFZNPOROTR"`;

exports[`finance > 1211 > bitcoinAddress 1`] = `"3eZEFLmGPLEQrSRdAcnZLoWwYeiHwmRogjbyG9G"`;
exports[`finance > 1211 > bitcoinAddress > noArgs 1`] = `"bc1pw8zppsdqusnufvv7l7dzsexkz8aqjdve9a6kq5qh8f7vlh2q6q9sjg7mv4"`;

exports[`finance > 1211 > bitcoinAddress > with type and network option 1`] = `"1TeZEFLmGPLEQrSRdAcnZLoWwYeiHwmRog"`;

exports[`finance > 1211 > bitcoinAddress > with type option 1`] = `"1TeZEFLmGPLEQrSRdAcnZLoWwYeiHwmRog"`;

exports[`finance > 1211 > creditCardCVV 1`] = `"982"`;

Expand Down Expand Up @@ -192,7 +200,11 @@ exports[`finance > 1337 > bic > noArgs 1`] = `"EHLILK9ZXXX"`;

exports[`finance > 1337 > bic > with branch code 1`] = `"GEHLGGI9XXX"`;

exports[`finance > 1337 > bitcoinAddress 1`] = `"1hsjwgYJ7oC8ZrMNmqzLbhEubpcwQ"`;
exports[`finance > 1337 > bitcoinAddress > noArgs 1`] = `"3hsjwgYJ7oC8ZrMNmqzLbhEubpc"`;

exports[`finance > 1337 > bitcoinAddress > with type and network option 1`] = `"1ahsjwgYJ7oC8ZrMNmqzLbhEubpc"`;

exports[`finance > 1337 > bitcoinAddress > with type option 1`] = `"1ahsjwgYJ7oC8ZrMNmqzLbhEubpc"`;

exports[`finance > 1337 > creditCardCVV 1`] = `"212"`;

Expand Down
Loading

0 comments on commit 69c0f25

Please sign in to comment.