File tree Expand file tree Collapse file tree 2 files changed +23
-19
lines changed
Expand file tree Collapse file tree 2 files changed +23
-19
lines changed Original file line number Diff line number Diff line change @@ -1015,6 +1015,14 @@ registerHelper(
10151015 if (hasOptions)
10161016 positionalArgs.pop_back ();
10171017
1018+ auto allTruthy = [](std::vector<dom::Value> const & values)
1019+ {
1020+ return std::all_of (values.begin (), values.end (), [](dom::Value const & v)
1021+ {
1022+ return v.isTruthy ();
1023+ });
1024+ };
1025+
10181026 // Simple helpers: compute directly from positional args to avoid
10191027 // Handlebars options coercing into unintended string results.
10201028 if (simpleHelper)
@@ -1048,12 +1056,7 @@ registerHelper(
10481056 }
10491057 if (helperName == " and" )
10501058 {
1051- if (positionalArgs.size () < 2 )
1052- return dom::Value (false );
1053- auto const & a = positionalArgs[0 ];
1054- auto const & b = positionalArgs[1 ];
1055- bool res = a.isBoolean () && b.isBoolean () && a.getBool () && b.getBool ();
1056- return dom::Value (res);
1059+ return dom::Value (allTruthy (positionalArgs));
10571060 }
10581061 }
10591062
@@ -1186,11 +1189,7 @@ registerHelper(
11861189 }
11871190 if (helperName == " and" )
11881191 {
1189- if (positionalArgs.size () < 2 ) return dom::Value (false );
1190- auto const & a = positionalArgs[0 ];
1191- auto const & b = positionalArgs[1 ];
1192- bool res = a.isBoolean () && b.isBoolean () && a.getBool () && b.getBool ();
1193- return dom::Value (res);
1192+ return dom::Value (allTruthy (positionalArgs));
11941193 }
11951194 }
11961195 }
Original file line number Diff line number Diff line change @@ -1301,18 +1301,23 @@ struct JavaScript_test
13011301 });
13021302 BOOST_TEST (hbs.render (" {{concat 'a' 'b'}}" ) == " ab" );
13031303
1304- // Boolean
1305- ensureHelper (" and" , " function(a, b ) { return a && b ; }" ,
1304+ // Boolean (truthiness)
1305+ ensureHelper (" and" , " function(...args ) { args.pop(); return args.every(it => it) ; }" ,
13061306 [](dom::Array const & args, std::size_t count)
13071307 {
1308- if (count < 2 )
1309- return dom::Value (false );
1310- auto const & a = args.get (0 );
1311- auto const & b = args.get (1 );
1312- bool res = a.isBoolean () && b.isBoolean () && a.getBool () && b.getBool ();
1308+ bool res = true ;
1309+ for (std::size_t i = 0 ; i < count; ++i)
1310+ {
1311+ res = res && args.get (i).isTruthy ();
1312+ if (!res)
1313+ break ;
1314+ }
13131315 return dom::Value (res);
13141316 });
1315- BOOST_TEST (hbs.render (" {{and true true}}" ) == " true" );
1317+ BOOST_TEST (hbs.render (" {{and true 1 'x'}}" ) == " true" );
1318+ BOOST_TEST (hbs.render (" {{and true 0}}" ) == " false" );
1319+ BOOST_TEST (hbs.render (" {{and '' true}}" ) == " false" );
1320+ BOOST_TEST (hbs.render (" {{and 'a' 5}}" ) == " true" );
13161321
13171322 // Undefined
13181323 ensureHelper (" undef" , " function() { return undefined; }" ,
You can’t perform that action at this time.
0 commit comments