Skip to content

Commit

Permalink
feat: add locale to finance module (cieslarmichal#869)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Aug 22, 2024
1 parent dac4dcd commit 81df1e2
Show file tree
Hide file tree
Showing 9 changed files with 972 additions and 1,041 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file
* changed function name from `name` to `companyName` in company module
* changed function `companyName` to take optional enum parameter in company module
* changed function `branch` to take optional enum parameter in git module
* changed country, language, bic country, iban country params to locale

## v2.0.0 (27.06.2024)

Expand Down
72 changes: 12 additions & 60 deletions include/faker-cxx/finance.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#pragma once

#include <optional>
#include <string>
#include <string_view>

#include "faker-cxx/export.h"
#include "types/country.h"
#include "faker-cxx/types/locale.h"
#include "types/precision.h"

namespace faker::finance
Expand Down Expand Up @@ -92,78 +91,32 @@ FAKER_CXX_EXPORT std::string_view accountType();
FAKER_CXX_EXPORT std::string amount(double min = 0, double max = 1000, Precision precision = Precision::TwoDp,
const std::string& symbol = "");

enum class IbanCountry
{
Austria,
Belgium,
Bulgaria,
Croatia,
Cyprus,
Czechia,
Denmark,
Estonia,
Finland,
France,
Germany,
Greece,
Hungary,
Ireland,
Italy,
Latvia,
Lithuania,
Luxembourg,
Malta,
Netherlands,
Poland,
Portugal,
Romania,
Slovakia,
Slovenia,
Spain,
Sweden
};

/**
* Generates a random iban.
* Generates a random IBAN.
*
* @param locale The locale. Defaults to `Locale::de_DE`.
*
* @param country The country from which you want to generate an IBAN, if none is provided a random country
will be used.
*
* @returns IBAN.
*
* @code
* faker::finance::iban(IbanCountry::Poland) // "PL61109010140000071219812874"
* faker::finance::iban(Locale::pl_PL) // "PL61109010140000071219812874"
* @endcode
*/
FAKER_CXX_EXPORT std::string iban(std::optional<IbanCountry> country = std::nullopt);

enum class BicCountry
{
France,
Germany,
India,
Italy,
Netherlands,
Poland,
Romania,
Spain,
UnitedKingdom,
UnitedStates,
};
FAKER_CXX_EXPORT std::string iban(Locale locale = Locale::de_DE);

/**
* Generates a random bic.
* Generates a random BIC.
*
* @param country The country from which you want to generate a BIC, if none is provided a random country
will be used.
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns BIC.
*
* @code
* faker::finance::bic(BicCountry::Poland) // "BREXPLPWMUL"
* faker::finance::bic(Locale::pl_PL) // "BREXPLPWMUL"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view bic(std::optional<BicCountry> country = std::nullopt);
FAKER_CXX_EXPORT std::string_view bic(Locale locale = Locale::en_US);

/**
* Generates a random account number.
Expand Down Expand Up @@ -206,7 +159,6 @@ FAKER_CXX_EXPORT std::string routingNumber();

enum class CreditCardType
{
DefaultCard,
AmericanExpress,
Discover,
MasterCard,
Expand All @@ -216,15 +168,15 @@ enum class CreditCardType
/**
* Generates a random credit card number.
*
* @param creditCardType The type of the credit card.
* @param creditCardType The type of the credit card. Defaults to `CreditCardType::Visa`.
*
* @returns Credit card number.
*
* @code
* faker::finance::creditCardNumber() // "4882664999007"
* @endcode
*/
FAKER_CXX_EXPORT std::string creditCardNumber(std::optional<CreditCardType> creditCardType = std::nullopt);
FAKER_CXX_EXPORT std::string creditCardNumber(CreditCardType creditCardType = CreditCardType::Visa);

/**
* Generates a random credit card CVV.
Expand Down
2 changes: 1 addition & 1 deletion include/faker-cxx/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ decltype(auto) randomElement(Range&& range)
throw std::invalid_argument{"Range [start, end) is empty."};
}

auto size = std::ranges::distance(range);
const auto size = std::ranges::distance(range);

const auto index = number::integer(size - 1);

Expand Down
26 changes: 9 additions & 17 deletions src/modules/finance.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "faker-cxx/finance.h"

#include <optional>
#include <string>
#include <string_view>
#include <vector>
Expand All @@ -12,6 +11,7 @@
#include "faker-cxx/number.h"
#include "faker-cxx/string.h"
#include "faker-cxx/types/hex.h"
#include "faker-cxx/types/locale.h"
#include "faker-cxx/types/precision.h"
#include "finance_data.h"

Expand Down Expand Up @@ -54,11 +54,9 @@ std::string amount(double min, double max, Precision precision, const std::strin
return result;
}

std::string iban(std::optional<IbanCountry> country)
std::string iban(Locale locale)
{
const auto ibanCountry = country ? *country : helper::randomElement(ibanCountries);

const auto& ibanFormat = ibanFormats.at(ibanCountry);
const auto& ibanFormat = ibanFormats.contains(locale) ? ibanFormats.at(locale) : ibanFormats.at(Locale::de_DE);

const auto& countryCode = ibanFormat[0];

Expand Down Expand Up @@ -89,11 +87,10 @@ std::string iban(std::optional<IbanCountry> country)
return iban;
}

std::string_view bic(std::optional<BicCountry> country)
std::string_view bic(Locale locale)
{
const auto bicCountry = country ? *country : helper::randomElement(bicCountries);

return helper::randomElement(bicCountriesCodes.at(bicCountry));
return helper::randomElement(bicCountriesCodes.contains(locale) ? bicCountriesCodes.at(locale) :
bicCountriesCodes.at(Locale::en_US));
}

std::string accountNumber(unsigned int length)
Expand All @@ -111,11 +108,9 @@ std::string routingNumber()
return string::numeric(9, true);
}

std::string creditCardNumber(std::optional<CreditCardType> creditCardType)
std::string creditCardNumber(CreditCardType creditCardType)
{
const auto creditCardTargetType = creditCardType ? *creditCardType : helper::randomElement(creditCardTypes);

switch (creditCardTargetType)
switch (creditCardType)
{
case CreditCardType::AmericanExpress:
return helper::replaceCreditCardSymbols(
Expand All @@ -126,12 +121,9 @@ std::string creditCardNumber(std::optional<CreditCardType> creditCardType)
case CreditCardType::MasterCard:
return helper::replaceCreditCardSymbols(
static_cast<std::string>(helper::randomElement(masterCardCreditCardFormats)));
case CreditCardType::Visa:
default:
return helper::replaceCreditCardSymbols(static_cast<std::string>(helper::randomElement(visaCreditCardFormats)));
default:;
}

return "";
}

std::string creditCardCvv()
Expand Down
Loading

0 comments on commit 81df1e2

Please sign in to comment.