Skip to content

Commit

Permalink
refactor: formathelper module (cieslarmichal#774)
Browse files Browse the repository at this point in the history
- formathelper module migration from class to functions within common namespace

- modified reference to functions and variables from formathelper module in all dependent modules

Signed-off-by: Guru Mehar Rachaputi <[email protected]>
  • Loading branch information
00thirdeye00 authored Jul 2, 2024
1 parent 7b966d0 commit bd02a42
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 86 deletions.
26 changes: 13 additions & 13 deletions src/common/FormatHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace faker

// TODO: change to std::function<std::string_view()>
std::string
FormatHelper::fillTokenValues(const std::string& format,
common::fillTokenValues(const std::string& format,
std::unordered_map<std::string, std::function<std::string()>> tokenValueGenerators)
{
std::string filledFormat;
Expand All @@ -31,7 +31,7 @@ FormatHelper::fillTokenValues(const std::string& format,

if (foundTokenGenerator == tokenValueGenerators.end())
{
throw std::runtime_error{FormatHelper::format("Generator not found for token {}.", token)};
throw std::runtime_error{common::format("Generator not found for token {}.", token)};
}

filledFormat += foundTokenGenerator->second();
Expand All @@ -47,7 +47,7 @@ FormatHelper::fillTokenValues(const std::string& format,
return filledFormat;
}

std::string FormatHelper::fillTokenValues(
std::string common::fillTokenValues(
const std::string& format,
std::unordered_map<std::string_view, std::function<std::string_view()>> tokenValueGenerators)
{
Expand All @@ -69,7 +69,7 @@ std::string FormatHelper::fillTokenValues(

if (foundTokenGenerator == tokenValueGenerators.end())
{
throw std::runtime_error{FormatHelper::format("Generator not found for token {}.", token)};
throw std::runtime_error{common::format("Generator not found for token {}.", token)};
}

filledFormat += foundTokenGenerator->second();
Expand All @@ -85,26 +85,26 @@ std::string FormatHelper::fillTokenValues(
return filledFormat;
}

std::string FormatHelper::precisionFormat(Precision precision, double value)
std::string common::precisionFormat(Precision precision, double value)
{
switch (precision)
{
case Precision::ZeroDp:
return FormatHelper::format("{:.0f}", value);
return common::format("{:.0f}", value);
case Precision::OneDp:
return FormatHelper::format("{:.1f}", value);
return common::format("{:.1f}", value);
case Precision::TwoDp:
return FormatHelper::format("{:.2f}", value);
return common::format("{:.2f}", value);
case Precision::ThreeDp:
return FormatHelper::format("{:.3f}", value);
return common::format("{:.3f}", value);
case Precision::FourDp:
return FormatHelper::format("{:.4f}", value);
return common::format("{:.4f}", value);
case Precision::FiveDp:
return FormatHelper::format("{:.5f}", value);
return common::format("{:.5f}", value);
case Precision::SixDp:
return FormatHelper::format("{:.6f}", value);
return common::format("{:.6f}", value);
case Precision::SevenDp:
return FormatHelper::format("{:.7f}", value);
return common::format("{:.7f}", value);
default:
throw std::invalid_argument("Invalid precision");
}
Expand Down
16 changes: 6 additions & 10 deletions src/common/FormatHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,29 @@

#endif

namespace faker
namespace faker::common
{
class FormatHelper
{
public:
#if !defined(USE_STD_FORMAT)
template <typename... Args>
static std::string format(fmt::format_string<Args...> fmt, Args&&... args)
std::string format(fmt::format_string<Args...> fmt, Args&&... args)
{
return fmt::format(fmt, std::forward<Args>(args)...);
}
#else
template <typename... Args>
static std::string format(std::format_string<Args...> fmt, Args&&... args)
std::string format(std::format_string<Args...> fmt, Args&&... args)
{
return std::format(fmt, std::forward<Args>(args)...);
}
#endif

FAKER_CXX_EXPORT static std::string precisionFormat(Precision precision, double value);
FAKER_CXX_EXPORT std::string precisionFormat(Precision precision, double value);

FAKER_CXX_EXPORT static std::string
FAKER_CXX_EXPORT std::string
fillTokenValues(const std::string& format,
std::unordered_map<std::string, std::function<std::string()>> tokenValueGenerators);

FAKER_CXX_EXPORT static std::string
FAKER_CXX_EXPORT std::string
fillTokenValues(const std::string& format,
std::unordered_map<std::string_view, std::function<std::string_view()>> tokenValueGenerators);
};
}
22 changes: 11 additions & 11 deletions src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ std::string rgb(bool includeAlpha)

if (!includeAlpha)
{
return FormatHelper::format("rgb({}, {}, {})", red, green, blue);
return common::format("rgb({}, {}, {})", red, green, blue);
}

std::floating_point auto alpha = number::decimal(1.0);

return FormatHelper::format("rgba({}, {}, {}, {:.2f})", red, green, blue, alpha);
return common::format("rgba({}, {}, {}, {:.2f})", red, green, blue, alpha);
}

std::string hex(HexCasing casing, HexPrefix prefix, bool includeAlpha)
Expand All @@ -46,12 +46,12 @@ std::string hsl(bool include_alpha)

if (!include_alpha)
{
return FormatHelper::format("hsl({}, {}, {})", hue, saturation, lightness);
return common::format("hsl({}, {}, {})", hue, saturation, lightness);
}

std::floating_point auto alpha = number::decimal(1.0);

return FormatHelper::format("hsla({}, {}, {}, {:.2f})", hue, saturation, lightness, alpha);
return common::format("hsla({}, {}, {}, {:.2f})", hue, saturation, lightness, alpha);
}

std::string lch(bool include_alpha)
Expand All @@ -62,12 +62,12 @@ std::string lch(bool include_alpha)

if (!include_alpha)
{
return FormatHelper::format("lch({}, {}, {})", luminance, chroma, hue);
return common::format("lch({}, {}, {})", luminance, chroma, hue);
}

std::floating_point auto alpha = number::decimal(1.0);

return FormatHelper::format("lcha({}, {}, {}, {:.2f})", luminance, chroma, hue, alpha);
return common::format("lcha({}, {}, {}, {:.2f})", luminance, chroma, hue, alpha);
}

std::string cmyk()
Expand All @@ -77,7 +77,7 @@ std::string cmyk()
std::floating_point auto yellow = number::decimal(1.);
std::floating_point auto key = number::decimal(1.);

return FormatHelper::format("cmyk({:.2f}, {:.2f}, {:.2f}, {:.2f})", cyan, magenta, yellow, key);
return common::format("cmyk({:.2f}, {:.2f}, {:.2f}, {:.2f})", cyan, magenta, yellow, key);
}

std::string lab()
Expand All @@ -86,7 +86,7 @@ std::string lab()
std::floating_point auto red_green = number::decimal(-128.0, 128.0);
std::floating_point auto blue_yellow = number::decimal(-128.0, 128.0);

return FormatHelper::format("lab({:.2f}, {:.2f}, {:.2f})", lightness, red_green, blue_yellow);
return common::format("lab({:.2f}, {:.2f}, {:.2f})", lightness, red_green, blue_yellow);
}

std::string hsb()
Expand All @@ -95,7 +95,7 @@ std::string hsb()
std::integral auto saturation = number::integer(100);
std::integral auto brightness = number::integer(100);

return FormatHelper::format("hsb({}, {}, {})", hue, saturation, brightness);
return common::format("hsb({}, {}, {})", hue, saturation, brightness);
}

std::string hsv()
Expand All @@ -104,7 +104,7 @@ std::string hsv()
std::integral auto saturation = number::integer(100);
std::integral auto value = number::integer(100);

return FormatHelper::format("hsv({}, {}, {})", hue, saturation, value);
return common::format("hsv({}, {}, {})", hue, saturation, value);
}

std::string yuv()
Expand All @@ -113,7 +113,7 @@ std::string yuv()
std::integral auto chrominance_blue = number::integer(255);
std::integral auto chrominance_red = number::integer(255);

return FormatHelper::format("yuv({}, {}, {})", luminance, chrominance_blue, chrominance_red);
return common::format("yuv({}, {}, {})", luminance, chrominance_blue, chrominance_red);
}

}
2 changes: 1 addition & 1 deletion src/modules/commerce/Commerce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ std::string_view productName()

std::string productFullName()
{
return FormatHelper::format("{} {} {}", productAdjective(), productMaterial(), productName());
return common::format("{} {} {}", productAdjective(), productMaterial(), productName());
}

std::string EAN13()
Expand Down
12 changes: 6 additions & 6 deletions src/modules/company/Company.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ std::string name()
switch (number::integer<int>(3))
{
case 0:
companyName = FormatHelper::format("{} {}", person::lastName(), helper::arrayElement(companySuffixes));
companyName = common::format("{} {}", person::lastName(), helper::arrayElement(companySuffixes));
break;
case 1:
companyName = FormatHelper::format("{} {} {}", person::firstName(), person::lastName(), person::jobArea());
companyName = common::format("{} {} {}", person::firstName(), person::lastName(), person::jobArea());
break;
case 2:
companyName =
FormatHelper::format("{} {} {} Services", person::firstName(), person::lastName(), person::jobArea());
common::format("{} {} {} Services", person::firstName(), person::lastName(), person::jobArea());
break;
case 3:
companyName = FormatHelper::format("{} {} {} {}", person::firstName(), person::lastName(), person::jobArea(),
companyName = common::format("{} {} {} {}", person::firstName(), person::lastName(), person::jobArea(),
helper::arrayElement(companySuffixes));
break;
}
Expand All @@ -48,7 +48,7 @@ std::string_view industry()

std::string buzzPhrase()
{
return FormatHelper::format("{} {} {}", buzzVerb(), buzzAdjective(), buzzNoun());
return common::format("{} {} {}", buzzVerb(), buzzAdjective(), buzzNoun());
}

std::string_view buzzAdjective()
Expand All @@ -68,7 +68,7 @@ std::string_view buzzVerb()

std::string catchPhrase()
{
return FormatHelper::format("{} {} {}", catchPhraseAdjective(), catchPhraseDescriptor(), catchPhraseNoun());
return common::format("{} {} {}", catchPhraseAdjective(), catchPhraseDescriptor(), catchPhraseNoun());
}

std::string_view catchPhraseAdjective()
Expand Down
4 changes: 2 additions & 2 deletions src/modules/date/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ std::string betweenDate(const auto& from, const auto& to, DateFormat dateFormat)
{
if (from > to)
{
throw std::runtime_error{FormatHelper::format("Start date is greater than end date. {{from: {}, to: {}}}",
throw std::runtime_error{common::format("Start date is greater than end date. {{from: {}, to: {}}}",
serializeTimePoint(from, dateFormat),
serializeTimePoint(to, dateFormat))};
}
Expand Down Expand Up @@ -180,7 +180,7 @@ unsigned int second()

std::string time()
{
return FormatHelper::format("{:02}:{:02}", hour(), minute());
return common::format("{:02}:{:02}", hour(), minute());
}

unsigned int dayOfMonth()
Expand Down
2 changes: 1 addition & 1 deletion src/modules/finance/Finance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ std::string amount(double min, double max, Precision precision, const std::strin

std::string result{symbol};

result += FormatHelper::precisionFormat(precision, generatedNumber);
result += common::precisionFormat(precision, generatedNumber);

return result;
}
Expand Down
16 changes: 8 additions & 8 deletions src/modules/git/Git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ std::string branch(unsigned maxIssueNum)
switch (number::integer(1, 3))
{
case 1:
return FormatHelper::format("{}-{}", word::verb(), word::noun());
return common::format("{}-{}", word::verb(), word::noun());
case 2:
return FormatHelper::format("{}-{}-{}", word::verb(), word::adjective(), word::noun());
return common::format("{}-{}-{}", word::verb(), word::adjective(), word::noun());
default:
return FormatHelper::format("{}-{}-{}-{}", number::integer(unsigned(1), maxIssueNum), word::verb(),
return common::format("{}-{}-{}-{}", number::integer(unsigned(1), maxIssueNum), word::verb(),
word::adjective(), word::noun());
}
}
Expand Down Expand Up @@ -73,7 +73,7 @@ std::string commitDate(unsigned years)
timeZoneString += "00";
}

return FormatHelper::format("{} {} {} {} {} {}", faker::date::weekdayAbbreviatedName(),
return common::format("{} {} {} {} {} {}", faker::date::weekdayAbbreviatedName(),
faker::date::monthAbbreviatedNames[size_t(std::stoi(month) - 1)], day, time, year,
timeZoneString);
}
Expand Down Expand Up @@ -115,13 +115,13 @@ std::string commitMessage()
switch (number::integer(1, 4))
{
case 1:
return FormatHelper::format("{} {}", word::verb(), word::noun());
return common::format("{} {}", word::verb(), word::noun());
case 2:
return FormatHelper::format("{} {} {}", word::verb(), word::adjective(), word::noun());
return common::format("{} {} {}", word::verb(), word::adjective(), word::noun());
case 3:
return FormatHelper::format("{} {} {}", word::verb(), word::noun(), word::adverb());
return common::format("{} {} {}", word::verb(), word::noun(), word::adverb());
default:
return FormatHelper::format("{} {} {} {}", word::verb(), word::adjective(), word::noun(), word::adverb());
return common::format("{} {} {} {}", word::verb(), word::adjective(), word::noun(), word::adverb());
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/modules/image/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ std::string imageUrl(unsigned int width, unsigned int height, std::optional<Imag
{
const std::string image_category =
category.has_value() ?
FormatHelper::format("/{}", imageCategoryToLoremFlickrStringMapping.at(category.value())) :
common::format("/{}", imageCategoryToLoremFlickrStringMapping.at(category.value())) :
"";

return FormatHelper::format("https://loremflickr.com/{}/{}{}", width, height, image_category);
return common::format("https://loremflickr.com/{}/{}{}", width, height, image_category);
}

std::string githubAvatarUrl()
{
return FormatHelper::format("https://avatars.githubusercontent.com/u/{}", number::integer<int>(100000000));
return common::format("https://avatars.githubusercontent.com/u/{}", number::integer<int>(100000000));
}

std::string dimensions()
{
return FormatHelper::format("{}x{}", number::integer<int>(1, 32720), number::integer<int>(1, 17280));
return common::format("{}x{}", number::integer<int>(1, 32720), number::integer<int>(1, 17280));
}

std::string_view type()
Expand Down
Loading

0 comments on commit bd02a42

Please sign in to comment.