Skip to content

Commit

Permalink
feat: Feature/science locale (#1001)
Browse files Browse the repository at this point in the history
* Added module constructor to both header (defination and the declaration

* Reverting the changes as they are directly pushed into main

* Added locale for science module(en_US)

* fixed the formatting using clang-format

* Added comments to the new parameters

* Fixed the issue with formatting.
  • Loading branch information
SgtApone117 authored Dec 2, 2024
1 parent 96e0263 commit 4bb7098
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 58 deletions.
29 changes: 22 additions & 7 deletions include/faker-cxx/science.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string_view>

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

namespace faker::science
{
Expand All @@ -16,14 +17,16 @@ struct FAKER_CXX_EXPORT ChemicalElement
/**
* @brief Returns a random chemical element from the periodic table.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Chemical element.
*
* @code
* faker::science::chemicalElement() // Object of ChemicalElement containing info about a random element in the periodic
* table.
* @endcode
*/
FAKER_CXX_EXPORT ChemicalElement chemicalElement();
FAKER_CXX_EXPORT ChemicalElement chemicalElement(Locale locale = Locale::en_US);

struct FAKER_CXX_EXPORT Unit
{
Expand All @@ -35,68 +38,80 @@ struct FAKER_CXX_EXPORT Unit
/**
* @brief Returns a unit of measurement for either distance, mass, time, temp, current.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Unit.
*
* @code
* faker::science::unit() // Object of Unit containing info about a random unit of measurement.
* @endcode
*/
FAKER_CXX_EXPORT Unit unit();
FAKER_CXX_EXPORT Unit unit(Locale locale = Locale::en_US);

/**
* @brief Returns a unit of measurement for either distance.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Distance unit.
*
* @code
* faker::science::distanceUnit() // Object of Unit containing info about a random unit of measurement used to measure
* distance.
* @endcode
*/
FAKER_CXX_EXPORT Unit distanceUnit();
FAKER_CXX_EXPORT Unit distanceUnit(Locale locale = Locale::en_US);

/**
* @brief Returns a unit of measurement for either time.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Time unit.
*
* @code
* faker::science::timeUnit() // Object of Unit containing info about a random unit of measurement used to measure time.
* @endcode
*/
FAKER_CXX_EXPORT Unit timeUnit();
FAKER_CXX_EXPORT Unit timeUnit(Locale locale = Locale::en_US);

/**
* @brief Returns a unit of measurement for either mass.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Mass unit.
*
* @code
* faker::science::massUnit() // Object of Unit containing info about a random unit of measurement used to measure mass.
* @endcode
*/
FAKER_CXX_EXPORT Unit massUnit();
FAKER_CXX_EXPORT Unit massUnit(Locale locale = Locale::en_US);

/**
* @brief Returns a unit of measurement for either temp.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Temperture unit.
*
* @code
* faker::science::tempUnit() // Object of Unit containing info about a random unit of measurement used to measure temp.
* @endcode
*/
FAKER_CXX_EXPORT Unit tempUnit();
FAKER_CXX_EXPORT Unit tempUnit(Locale locale = Locale::en_US);

/**
* @brief Returns a unit of measurement for either current.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Current unit.
*
* @code
* faker::science::currentUnit() // Object of Unit containing info about a random unit of measurement used to measure
* current.
* @endcode
*/
FAKER_CXX_EXPORT Unit currentUnit();
FAKER_CXX_EXPORT Unit currentUnit(Locale locale = Locale::en_US);
}
63 changes: 44 additions & 19 deletions src/modules/science.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,72 @@

namespace faker::science
{
ChemicalElement chemicalElement()
namespace
{
return helper::randomElement(chemicalElements);
const struct ScienceDefinition& getScienceDefinition(Locale locale)
{
switch (locale)
{
default:
return enUSscienceDefinition;
}
}
}

Unit unit()
ChemicalElement chemicalElement(Locale locale)
{
const auto& scienceDefinition = getScienceDefinition(locale);

return helper::randomElement(scienceDefinition.chemicalElements);
}

Unit unit(Locale locale)
{
const auto& scienceDefinition = getScienceDefinition(locale);

std::vector<Unit> units;

units.insert(units.end(), distanceUnits.begin(), distanceUnits.end());
units.insert(units.end(), massUnits.begin(), massUnits.end());
units.insert(units.end(), timeUnits.begin(), timeUnits.end());
units.insert(units.end(), currentUnits.begin(), currentUnits.end());
units.insert(units.end(), temperatureUnits.begin(), temperatureUnits.end());
units.insert(units.end(), scienceDefinition.distanceUnits.begin(), scienceDefinition.distanceUnits.end());
units.insert(units.end(), scienceDefinition.massUnits.begin(), scienceDefinition.massUnits.end());
units.insert(units.end(), scienceDefinition.timeUnits.begin(), scienceDefinition.timeUnits.end());
units.insert(units.end(), scienceDefinition.currentUnits.begin(), scienceDefinition.currentUnits.end());
units.insert(units.end(), scienceDefinition.temperatureUnits.begin(), scienceDefinition.temperatureUnits.end());

return helper::randomElement(units);
}

Unit distanceUnit()
Unit distanceUnit(Locale locale)
{
return helper::randomElement(distanceUnits);
const auto& scienceDefinition = getScienceDefinition(locale);

return helper::randomElement(scienceDefinition.distanceUnits);
}

Unit timeUnit()
Unit timeUnit(Locale locale)
{
return helper::randomElement(timeUnits);
const auto& scienceDefinition = getScienceDefinition(locale);

return helper::randomElement(scienceDefinition.timeUnits);
}

Unit massUnit()
Unit massUnit(Locale locale)
{
return helper::randomElement(massUnits);
const auto& scienceDefinition = getScienceDefinition(locale);

return helper::randomElement(scienceDefinition.massUnits);
}

Unit tempUnit()
Unit tempUnit(Locale locale)
{
return helper::randomElement(temperatureUnits);
const auto& scienceDefinition = getScienceDefinition(locale);

return helper::randomElement(scienceDefinition.temperatureUnits);
}

Unit currentUnit()
Unit currentUnit(Locale locale)
{
return helper::randomElement(currentUnits);
}
const auto& scienceDefinition = getScienceDefinition(locale);

return helper::randomElement(scienceDefinition.currentUnits);
}
}
32 changes: 26 additions & 6 deletions src/modules/science_data.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#pragma once

#include <array>
#include <span>

#include "faker-cxx/science.h"

namespace faker::science
{
const auto chemicalElements = std::to_array<ChemicalElement>(
struct ScienceDefinition
{
std::span<const ChemicalElement> chemicalElements;
std::span<const Unit> distanceUnits;
std::span<const Unit> massUnits;
std::span<const Unit> timeUnits;
std::span<const Unit> currentUnits;
std::span<const Unit> temperatureUnits;
};

const auto enUSchemicalElements = std::to_array<ChemicalElement>(
{{"Hydrogen", "H", 1}, {"Helium", "He", 2}, {"Lithium", "Li", 3}, {"Beryllium", "Be", 4},
{"Boron", "B", 5}, {"Carbon", "C", 6}, {"Nitrogen", "N", 7}, {"Oxygen", "O", 8},
{"Fluorine", "F", 9}, {"Neon", "Ne", 10}, {"Sodium", "Na", 11}, {"Magnesium", "Mg", 12},
Expand Down Expand Up @@ -38,7 +49,7 @@ const auto chemicalElements = std::to_array<ChemicalElement>(
{"Nihonium", "Nh", 113}, {"Flerovium", "Fl", 114}, {"Moscovium", "Mc", 115}, {"Livermorium", "Lv", 116},
{"Tennessine", "Ts", 117}, {"Oganesson", "Og", 118}});

const auto distanceUnits = std::to_array<Unit>({
const auto enUSdistanceUnits = std::to_array<Unit>({
{"Millimeter", "mm", "Length"},
{"Centimeter", "cm", "Length"},
{"Meter", "m", "Length"},
Expand All @@ -49,7 +60,7 @@ const auto distanceUnits = std::to_array<Unit>({
{"Mile", "mi", "Length"},
});

const auto massUnits = std::to_array<Unit>({
const auto enUSmassUnits = std::to_array<Unit>({
{"Gram", "g", "Mass"},
{"Kilogram", "kg", "Mass"},
{"Milligram", "mg", "Mass"},
Expand All @@ -60,7 +71,7 @@ const auto massUnits = std::to_array<Unit>({
{"Slug", "sl", "Mass"},
});

const auto timeUnits = std::to_array<Unit>({
const auto enUStimeUnits = std::to_array<Unit>({
{"Second", "s", "Time"},
{"Minute", "min", "Time"},
{"Hour", "hr", "Time"},
Expand All @@ -70,16 +81,25 @@ const auto timeUnits = std::to_array<Unit>({
{"Year", "yr", "Time"},
});

const auto currentUnits = std::to_array<Unit>({
const auto enUScurrentUnits = std::to_array<Unit>({
{"Ampere", "A", "Electric Current"},
{"Milliampere", "mA", "Electric Current"},
{"Microampere", "μA", "Electric Current"},
});

const auto temperatureUnits = std::to_array<Unit>({
const auto enUStemperatureUnits = std::to_array<Unit>({
{"Celcius", "°C", "Temperature"},
{"Fahrenheit", "°F", "Temperature"},
{"Kelvin", "K", "Temperature"},
});

const ScienceDefinition enUSscienceDefinition = {
.chemicalElements = enUSchemicalElements,
.distanceUnits = enUSdistanceUnits,
.massUnits = enUSmassUnits,
.timeUnits = enUStimeUnits,
.currentUnits = enUScurrentUnits,
.temperatureUnits = enUStemperatureUnits,
};

}
Loading

0 comments on commit 4bb7098

Please sign in to comment.