Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add capitalize function #282

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,10 @@ Assignments only set the value within the rendering context; they do not modify

A few functions are implemented within the inja template syntax. They can be called with
```.cpp
// Upper and lower function, for string cases
// Upper, lower and capitalize function, for string cases
render("Hello {{ upper(neighbour) }}!", data); // "Hello PETER!"
render("Hello {{ lower(neighbour) }}!", data); // "Hello peter!"
render("Hello {{ capitalize(neighbour) }}!", data); // "Hello Peter!"

// Range function, useful for loops
render("{% for i in range(4) %}{{ loop.index1 }}{% endfor %}", data); // "1234"
Expand Down
2 changes: 2 additions & 0 deletions include/inja/function_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class FunctionStorage {
Modulo,
AtId,
At,
Capitalize,
Default,
DivisibleBy,
Even,
Expand Down Expand Up @@ -76,6 +77,7 @@ class FunctionStorage {

std::map<std::pair<std::string, int>, FunctionData> function_storage = {
{std::make_pair("at", 2), FunctionData {Operation::At}},
{std::make_pair("capitalize", 1), FunctionData {Operation::Capitalize}},
{std::make_pair("default", 2), FunctionData {Operation::Default}},
{std::make_pair("divisibleBy", 2), FunctionData {Operation::DivisibleBy}},
{std::make_pair("even", 1), FunctionData {Operation::Even}},
Expand Down
6 changes: 6 additions & 0 deletions include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ class Renderer : public NodeVisitor {
data_eval_stack.push(&args[0]->at(args[1]->get<int>()));
}
} break;
case Op::Capitalize: {
auto result = get_arguments<1>(node)[0]->get<json::string_t>();
result[0] = static_cast<char>(::toupper(result[0]));
std::transform(result.begin() + 1, result.end(), result.begin() + 1, [](char c) { return static_cast<char>(::tolower(c)); });
make_result(std::move(result));
} break;
case Op::Default: {
const auto test_arg = get_arguments<1, 0, false>(node)[0];
data_eval_stack.push(test_arg ? test_arg : get_arguments<1, 1>(node)[0]);
Expand Down
8 changes: 8 additions & 0 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class FunctionStorage {
Modulo,
AtId,
At,
Capitalize,
Default,
DivisibleBy,
Even,
Expand Down Expand Up @@ -165,6 +166,7 @@ class FunctionStorage {

std::map<std::pair<std::string, int>, FunctionData> function_storage = {
{std::make_pair("at", 2), FunctionData {Operation::At}},
{std::make_pair("capitalize", 1), FunctionData {Operation::Capitalize}},
{std::make_pair("default", 2), FunctionData {Operation::Default}},
{std::make_pair("divisibleBy", 2), FunctionData {Operation::DivisibleBy}},
{std::make_pair("even", 1), FunctionData {Operation::Even}},
Expand Down Expand Up @@ -2378,6 +2380,12 @@ class Renderer : public NodeVisitor {
data_eval_stack.push(&args[0]->at(args[1]->get<int>()));
}
} break;
case Op::Capitalize: {
auto result = get_arguments<1>(node)[0]->get<json::string_t>();
result[0] = std::toupper(result[0]);
std::transform(result.begin() + 1, result.end(), result.begin() + 1, [](char c) { return static_cast<char>(::tolower(c)); });
make_result(std::move(result));
} break;
case Op::Default: {
const auto test_arg = get_arguments<1, 0, false>(node)[0];
data_eval_stack.push(test_arg ? test_arg : get_arguments<1, 1>(node)[0]);
Expand Down
5 changes: 5 additions & 0 deletions test/test-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ TEST_CASE("functions") {
// [json.exception.type_error.302] type must be string, but is number" );
}

SUBCASE("capitalize") {
CHECK(env.render("{{ capitalize(name) }}", data) == "Peter");
CHECK(env.render("{{ capitalize(city) }}", data) == "New york");
}

SUBCASE("range") {
CHECK(env.render("{{ range(2) }}", data) == "[0,1]");
CHECK(env.render("{{ range(4) }}", data) == "[0,1,2,3]");
Expand Down