From 59157a429a1bcde3f78bd9009f6de02358365ba2 Mon Sep 17 00:00:00 2001 From: Matt Mayer Date: Mon, 27 Feb 2023 06:17:03 +0700 Subject: feat(finance): currency object (#1809) --- src/modules/finance/index.ts | 51 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 9 deletions(-) (limited to 'src/modules') diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 8db5bfce..84b42077 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -2,6 +2,26 @@ import type { Faker } from '../..'; import { FakerError } from '../../errors/faker-error'; import iban from './iban'; +/** + * The possible definitions related to currency entries. + */ +export interface Currency { + /** + * The full name for the currency (e.g. `US Dollar`). + */ + name: string; + + /** + * The code/short text/abbreviation for the currency (e.g. `USD`). + */ + code: string; + + /** + * The symbol for the currency (e.g. `$`). + */ + symbol: string; +} + /** * Module to generate finance related entries. */ @@ -572,6 +592,25 @@ export class FinanceModule { ); } + /** + * Returns a random currency object, containing `code`, `name `and `symbol` properties. + * + * @see + * faker.finance.currencyCode() + * faker.finance.currencyName() + * faker.finance.currencySymbol() + * + * @example + * faker.finance.currency() // { code: 'USD', name: 'US Dollar', symbol: '$' } + * + * @since 8.0.0 + */ + currency(): Currency { + return this.faker.helpers.arrayElement( + this.faker.definitions.finance.currency + ); + } + /** * Returns a random currency code. * (The short text/abbreviation for the currency (e.g. `US Dollar` -> `USD`)) @@ -582,9 +621,7 @@ export class FinanceModule { * @since 2.0.1 */ currencyCode(): string { - return this.faker.helpers.objectValue( - this.faker.definitions.finance.currency - )['code']; + return this.currency().code; } /** @@ -596,9 +633,7 @@ export class FinanceModule { * @since 2.0.1 */ currencyName(): string { - return this.faker.helpers.objectKey( - this.faker.definitions.finance.currency - ) as string; + return this.currency().name; } /** @@ -612,9 +647,7 @@ export class FinanceModule { currencySymbol(): string { let symbol: string; while (!symbol) { - symbol = this.faker.helpers.objectValue( - this.faker.definitions.finance.currency - )['symbol']; + symbol = this.currency().symbol; } return symbol; -- cgit v1.2.3