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 join function #204

Merged
merged 5 commits into from
Jun 17, 2021
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ render("{{ last(guests) }} was last.", data); // "Patir was last."
render("{{ sort([3,2,1]) }}", data); // "[1,2,3]"
render("{{ sort(guests) }}", data); // "[\"Jeff\", \"Patrick\", \"Tom\"]"

// Join a list with a separator
render("{{ join([1,2,3], \" + \") }}", data); // "1 + 2 + 3"
render("{{ join(guests, \", \") }}", data); // "Jeff, Patrick, Tom"

// Round numbers to a given precision
render("{{ round(3.1415, 0) }}", data); // 3
render("{{ round(3.1415, 3) }}", data); // 3.142
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 @@ -65,6 +65,7 @@ class FunctionStorage {
Sort,
Upper,
Super,
Join,
Callback,
ParenLeft,
ParenRight,
Expand Down Expand Up @@ -109,6 +110,7 @@ class FunctionStorage {
{std::make_pair("upper", 1), FunctionData { Operation::Upper }},
{std::make_pair("super", 0), FunctionData { Operation::Super }},
{std::make_pair("super", 1), FunctionData { Operation::Super }},
{std::make_pair("join", 2), FunctionData { Operation::Join }},
};

public:
Expand Down
2 changes: 1 addition & 1 deletion include/inja/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Parser {
auto function = operator_stack.top();
operator_stack.pop();

for (size_t i = 0; i < function->number_args; ++i) {
for (int i = 0; i < function->number_args; ++i) {
function->arguments.insert(function->arguments.begin(), arguments.back());
arguments.pop_back();
}
Expand Down
18 changes: 18 additions & 0 deletions include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,24 @@ class Renderer : public NodeVisitor {
json_tmp_stack.push_back(result_ptr);
json_eval_stack.push(result_ptr.get());
} break;
case Op::Join: {
const auto args = get_arguments<2>(node);
const auto separator = args[1]->get<std::string>();
std::ostringstream os;
std::string sep;
for (const auto& value : *args[0]) {
os << sep;
if (value.is_string()) {
os << value.get<std::string>(); // otherwise the value is surrounded with ""
} else {
os << value;
}
sep = separator;
}
result_ptr = std::make_shared<json>(os.str());
json_tmp_stack.push_back(result_ptr);
json_eval_stack.push(result_ptr.get());
} break;
case Op::ParenLeft:
case Op::ParenRight:
case Op::None:
Expand Down
22 changes: 21 additions & 1 deletion single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,7 @@ class FunctionStorage {
Sort,
Upper,
Super,
Join,
Callback,
ParenLeft,
ParenRight,
Expand Down Expand Up @@ -1642,6 +1643,7 @@ class FunctionStorage {
{std::make_pair("upper", 1), FunctionData { Operation::Upper }},
{std::make_pair("super", 0), FunctionData { Operation::Super }},
{std::make_pair("super", 1), FunctionData { Operation::Super }},
{std::make_pair("join", 2), FunctionData { Operation::Join }},
};

public:
Expand Down Expand Up @@ -2916,7 +2918,7 @@ class Parser {
auto function = operator_stack.top();
operator_stack.pop();

for (size_t i = 0; i < function->number_args; ++i) {
for (int i = 0; i < function->number_args; ++i) {
function->arguments.insert(function->arguments.begin(), arguments.back());
arguments.pop_back();
}
Expand Down Expand Up @@ -4018,6 +4020,24 @@ class Renderer : public NodeVisitor {
json_tmp_stack.push_back(result_ptr);
json_eval_stack.push(result_ptr.get());
} break;
case Op::Join: {
const auto args = get_arguments<2>(node);
const auto separator = args[1]->get<std::string>();
std::ostringstream os;
std::string sep;
for (const auto& value : *args[0]) {
os << sep;
if (value.is_string()) {
os << value.get<std::string>(); // otherwise the value is surrounded with ""
} else {
os << value;
}
sep = separator;
}
result_ptr = std::make_shared<json>(os.str());
json_tmp_stack.push_back(result_ptr);
json_eval_stack.push(result_ptr.get());
} break;
case Op::ParenLeft:
case Op::ParenRight:
case Op::None:
Expand Down
5 changes: 5 additions & 0 deletions test/test-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ TEST_CASE("functions") {
"[inja.exception.render_error] (at 1:22) variable 'sister' not found");
}

SUBCASE("join") {
CHECK(env.render("{{ join(names, \" | \") }}", data) == "Jeff | Seb | Peter | Tom");
CHECK(env.render("{{ join(vars, \", \") }}", data) == "2, 3, 4, 0, -1, -2, -3");
}

SUBCASE("isType") {
CHECK(env.render("{{ isBoolean(is_happy) }}", data) == "true");
CHECK(env.render("{{ isBoolean(vars) }}", data) == "false");
Expand Down