How can I use a bool and bool condition in a mustache template? - boolean

I am looking to use two conditions together to handle a display. If both a and b are true, then show something.
In JavaScript, we would write: if (a && b) { ... }
Moustache
{{#(one && two)}} // or #one && #two
<p>foobar</p>
{{/(one && two)}}
JSON
{
"one": "OK",
"two": "Yes"
}

Remember that
if (a && b) {
}
is the same as
if (a) {
if (b) {
}
}
So you can write you Mustach template like this:
Moustache
{{#one}}
{{#two}}
<p>foobar</p>
{{/two}}
{{/one}}

Related

TWebBrowser QueryInterface IID_IHTMLElement2 always returns E_NOINTERFACE

I am doing a simple QueryInterface() to get the IHTMLElement2 interface, but it always fails with E_NOINTERFACE.
UPDATE: I need to get the IHTMLElement2 interface of the body element because it has a focus() method so I can set focus to the body. It can't be done with the IHTMLElement interface.
Any ideas why this errors (or how to reach body->focus())?
WebBrowser1->Navigate(L"c:\\test.htm");
while (WebBrowser1->Busy) Application->ProcessMessages();
DelphiInterface<IHTMLDocument2> diDoc = WebBrowser1->Document;
if (diDoc) {
DelphiInterface<IHTMLElement2> diBodyElement;
// all good until this point
if (SUCCEEDED(diDoc->QueryInterface(IID_IHTMLElement2, reinterpret_cast<void**>(&diBodyElement))) && diBodyElement) {
// Never reaches this part - always E_NOINTERFACE when querying for IID_IHTMLElement2
diBodyElement->focus();
}
}
I've reached my own solution how to reach body->focus() which follows:
DelphiInterface <IHTMLDocument2> diDoc2 = WebBrowser1->Document;
if (diDoc2) {
DelphiInterface<IHTMLElement> pBody1;
DelphiInterface<IHTMLElement2> pBody2;
DelphiInterface<IHTMLBodyElement> pBodyElem;
if (SUCCEEDED(diDoc2->get_body(&pBody1)) && pBody1) {
if (SUCCEEDED(pBody1->QueryInterface(IID_IHTMLBodyElement, reinterpret_cast<void**>(&pBodyElem))) && pBodyElem) {
if (SUCCEEDED(pBodyElem->QueryInterface(IID_IHTMLElement2, reinterpret_cast<void**>(&pBody2))) && pBody2) {
pBody2->focus();
}
}
}
}
EDIT (suggested by Reby Lebeau) - simplified version:
DelphiInterface <IHTMLDocument2> diDoc2 = WebBrowser1->Document;
if (diDoc2) {
DelphiInterface<IHTMLElement> pBody1;
DelphiInterface<IHTMLElement2> pBody2;
if (SUCCEEDED(diDoc2->get_body(&pBody1)) && pBody1) {
if (SUCCEEDED(pBody1->QueryInterface(IID_IHTMLElement2, reinterpret_cast<void**>(&pBody2))) && pBody2) {
// focus to <body> element
pBody2->focus();
}
}
}

Type guarding React.KeyboardEvent to reuse event handlers

I created a search-bar-React-Component that resembles the one by Google.
It should fire off a search based on the input if I either click on the 'search' icon or if I hit the enter key.
I want to reuse the same function for both the click and the keydown handler:
...
var [searchParam, setSearchParam] = useState('');
function initSearch(
e:
| React.MouseEvent<HTMLButtonElement>
| React.KeyboardEvent<HTMLInputElement>
): void {
if (e.type == 'click' || (e.type == 'keydown' && e.key == 'Enter')) {
console.log(searchParam); /* ⬆️ this throws the error */
}
}
...
TypeScript keeps giving me the following error:
'Property 'key' does not exist on type 'MouseEvent<HTMLButtonElement, MouseEvent>'
I tried both of the following:
(e instance of KeyboardEvent && e.key == 'Enter') // This is always false, since e is a React.KeyboardEvent
(e instance of React.KeyboardEvent) // KeyboardEvent is not a property of React.
What is a good way to typeguard? Is there a better way to write the function?
Thank you.
Turns out using an intersection type solved the problem:
function initSearch(
e:
| (React.MouseEvent<HTMLButtonElement> & { type: 'click' }) /*⬅️*/
| (React.KeyboardEvent<HTMLInputElement> & { type: 'keydown' }) /*⬅️*/
): void {
if (e.type == 'click' || (e.type == 'keydown' && e.key == 'Enter')) {
console.log(searchParam);
}
}
I checked the type definitions, turns out the 'type' property is only defined as 'string', not as a definite primitive value.
In case I'm missing something here (i.e. that a keydown event can somehow not include the e.type == 'keydown' property), please let me know.
It feels unnecessarily hacky!

How to pass values instead of Vector in foreach loop?

My Reponse is:
[
{"id":106455,"assetId":482282,"masterKeyframeId":157060,"closed":false},
{"id":106661,"assetId":502174,"masterKeyframeId":169193,"closed":false}
{.....and so many...}
]
I have fetched "assetId" and "masterKeyframeId" correctly using below request, but the problem is how do I set both values in foreach loop? (I have used "aid" and it fetches single value but don't know about passing single value for "mkeyframeId" as it takes Vector)
.exec(http("request_7")
.get(uri3 + "/sortBy=SEGGREGATED_SESSION_SORT;reviewState=IN_PROGRESS")
.check(jsonPath("$..assetId").findAll.saveAs("astId"))
.check(jsonPath("$..masterKeyframeId").findAll.saveAs("mkeyframeId"))
.headers(headers_7)
)
.foreach("${astId}", "aid") {
doIf(session => session("aid").as[String] != "-1")
{
exec(http("Set_IDs")
.get("/a/" + accountname + "/assets/${aid}/keyframe/${mkeyframeId}")
)
}
}
Here the problem is ${mkeyframeId} it takes vector and pass in url like this,
Sending request=Set_IDs uri=https://qa1.net/a/hbmin1ac/assets/482282/keyframe/Vector(157060,%20169193):
Instead of
https://qa1.net/a/hbmin1ac/assets/482282/keyframe/157060
https://qa1.net/a/hbmin1ac/assets/502174/keyframe/169193
Thanks.
You should try something like this:
.foreach("${astId}", "aid", "counter") {
doIf { session =>
for {
aid <- session("aid").validate[String]
} yield aid != "-1"
} {
exec(http("Set_IDs")
.get { session =>
for {
aid <- session("aid").validate[String]
mkeyframeId <- session("mkeyframeId").validate[Seq[String]]
c <- session("counter").validate[Int]
} yield s"/a/$accountname/assets/$aid/keyframe/${mkeyframeId(c)}"
}
)
}
}
You can write the doIf block like this too:
doIf( _("aid").validate[String].map(_ != "-1"))

perl nested unless statements not equal to use of &&'s?

7 foreach (#crons) {
8 unless (index($_, "cks") != -1) {
9 unless (index($_, "aab") != -1) {
10 unless (index($_, "lam") != -1) {
11 push (#found, $_);
12 }
13 }
14 }
15 }
how come the above does not give the same output as the following:
7 foreach (#crons) {
8 unless (index($_, "cks") != -1 && index($_, "aab") != -1 && index($_, "lam") != -1) {
9 push (#found, $_);
10 }
11 }
#crons has the list of strings and I am trying to get all string that does not have "cks", "aab" and "lam"
The first section of code does what i want but the second doesnt and in my mind, it should...
Can anyone care to explain why they are not the same nor why it does not give the same output?
Let's call your conditions A, B, C. We now have the code
unless (A) {
unless (B) {
unless (C) {
The unless can be very confusing, so we write it only using if:
if (!A) {
if (!B) {
if (!C) {
Now we && those conditions together:
if (!A && !B && !C) {
This could also be written as
if (not(A || B || C)) {
This equivalence is called de Morgan's law
The non-equivalence of the two logics become clear when you test the string 'cks'.
The first logic would evaluate to false, the second would evaluate to true, since it does not contain the string 'aab' or 'lam'.

nested IFs or multiple condition in single IF - perl

I have some piece of code for multiple conditions in Perl
if (/abc/ && !/def/ && !/ghi/ && jkl) {
#### do something
}
Will every condition will be evaluated at once on every line?
I can prioritize the conditions using nested ifs
if (/abc/){
if (!/def/){
....so on
}
&& short-circuits. It only evaluates its RHS operand if needed. If it's LHS operand returns something false, && will that value.
For example,
use feature qw( say );
sub f1 { say "1"; 1 }
sub f2 { say "2"; 0 }
sub f3 { say "3"; 0 }
sub f4 { say "4"; 0 }
1 if f1() && f2() && f3() && f4();
Output:
1
2
So the following two lines are basically the same:
if (/abc/) { if (!/def/) { ... } }
if (/abc/ && !/def/) { ... }
In fact, if compiles into an and operator, so the above are very close to
(/abc/ and !/def/) and do { ... };
(/abc/ && !/def/) and do { ... };
No.
Think of it like this, if I said
"is the moon bigger than the sun?"
AND "is the pacific bigger than the mediterraan?"
AND "is russia bigger than england?"
AND ... many more AND ....
You could answer "no" very quickly, not having to figure out the answer to anything beyond the first question. It's called "short circuiting"
So in your case, unless an input line matches
/abc/ && !/def/ && !/ghi/
You won't need to evaluate whether it matches /jkl/.