I have a twig batch in my code used to style a list of images. I need to know the position of each image in the batch since they will all be styled differently.
{% for mediaItem in mediaItems|batch(9, 'empty') %}
{{ dump(mediaItem) }}
{% endfor %}
This returns the following result:
array:9 [▼
0 => MediaItem {#1135 ▶}
1 => MediaItem {#1155 ▶}
2 => MediaItem {#1169 ▶}
3 => MediaItem {#1180 ▶}
4 => MediaItem {#1191 ▶}
5 => MediaItem {#1202 ▶}
6 => MediaItem {#1213 ▶}
7 => MediaItem {#1224 ▶}
8 => MediaItem {#1235 ▶}
]
array:9 [▼
9 => MediaItem {#1246 ▶}
10 => MediaItem {#1257 ▶}
11 => MediaItem {#1268 ▶}
12 => MediaItem {#1279 ▶}
13 => MediaItem {#1290 ▶}
14 => MediaItem {#1301 ▶}
15 => MediaItem {#1312 ▶}
16 => MediaItem {#1323 ▶}
17 => MediaItem {#1334 ▶}
]
array:9 [▼
0 => MediaItem {#1345 ▶}
1 => "empty"
2 => "empty"
3 => "empty"
4 => "empty"
5 => "empty"
6 => "empty"
7 => "empty"
8 => "empty"
]
The first and last batch are as I expect but the keys of the second batch don't start from 0.
I've read in the twig documentation that there is an argument preserve_keys which I have tried as follows: {% for mediaItem in mediaItems|batch(9, 'empty', false) %} but with no result.
Is there anything I'm doing wrong or another way to achieve this?
This is on twig 2.4.8.
Related
Here is the code piece:
const fs = require('fs')
process.nextTick(console.log, 1)
setImmediate(() => {
console.log(2)
})
fs.stat('./xxx.js', () => {
process.nextTick(console.log, 3)
queueMicrotask(() => {
console.log(4)
})
setTimeout(() => {
console.log(5)
}, 0)
})
the output: 1 3 4 2 5
I don't understand why 3 and 4 runs before 2, because according to what-is-the-event-loop, when event loop hits the Poll Phase and scheduled with setImmediate, it will not wait but go directly into Check Phase, so setImmediate callbacks should be run first, but obviously it's not, but why?
I guess the fs.stat have something special behavior. You could use other fs function like fs.readFile, then you'll get the 1 2 3 4 5 like below. (Note: your xxx.js file should exist)
const fs = require('fs')
process.nextTick(console.log, 1)
setImmediate(() => {
console.log(2)
})
fs.readFile('./xxx.js', (e, s) => {
process.nextTick(console.log, 3)
queueMicrotask(() => {
console.log(4)
})
setTimeout(() => {
console.log(5)
}, 0)
})
Because setImmediate is not in a I/O callback, its callbacks will be executed in next Eventloop iteration when executed outside of Polling phase.
We recently upgraded to Angular 6.0.3, RxJs 6.2.0 and jest 23.1.0 (upgrading from RxJS 5 & Angular 4).
There seems to be a problem with Jest & RxJs as failing expect-statements inside a subscribe-Block do not mark the test as failed. Here's a minimal example:
it("should fail", () => {
const obs = Observable.create((observer) => {
observer.next(false);
});
obs.subscribe((value) => {
console.log(value); // => false
expect(value).toBeTruthy();
});
});
The expect-Statement gets executed, but the test still passes. We didn't observe this behaviour with the previous RxJs version and Jest.
Try to use done.
it("should fail", (done) => {
const obs = Observable.create((observer) => {
observer.next(false);
});
obs.subscribe((value) => {
console.log(value); // => false
expect(value).toBeTruthy();
done();
});
});
more info
In Logstash, let's say I have the following lines in my logs:
Message: msisdn: 111111111
Message: msisdn: 222222222
Answer: msisdn: 111111111
Answer: msisdn: 222222222
Now, whenever I get the Message, I'd like to wait for X seconds.
If within this time period I get the matching Answer (i.e. with the same msisdn), mark it as OK, else mark it as ERROR.
How can I do that?
Thanks
**EDIT**
Fairy, I tried to work with the aggregate filter but with no success, can you please help me with that?
input {
stdin {}
}
filter {
grok {
match => {"message" => "%{GREEDYDATA:type}: msisdn: %{INT:msisdn}"}
}
if [type] == "Message" {
aggregate {
task_id => "%{msisdn}"
code => "event.set('result', 'OK')"
timeout => 5
timeout_code => "event.set('result', 'error')"
}
}
if [logger] == "Answer" {
aggregate {
task_id => "%{msisdn}"
code => "event.set('result', 'OK')"
end_of_task => true
}
}
if "_grokparsefailure" in [tags] {
drop {}
}
}
output {
stdout { codec => rubydebug }
}
After writing this line to the stdin:
Message: msisdn: 111111111
There was a response immediately (it didn't wait 5 seconds) with status OK
{
"result" => "OK",
"#timestamp" => 2017-03-30T17:58:39.940Z,
"#version" => "1",
"host" => "31634cf481d5",
"message" => "Message: msisdn: 111111111",
"type" => "Message",
"msisdn" => "111111111"
}
Should I write it differently?
Many thanks ;)
The aggregate filter pretty much does what you are describing. First you need to identify the task_id to which you correlate your messages. Then you set up your logstash filter so it get the type of the message and your task_id. Then we have to seperate aggregate filters, one for the first theother for the second event.
The first has the timeout and the code to set the event to error should it timeout. The second filter checks for the Answer message and ends the aggregation.
grok {
match => {%{DATA:type}: %{DATA:name}: %{DATA:id}}
}
if [type] == "Message" {
aggregate {
task_id => "%{id}"
code => "event.set('result', 'OK')"
timeout => 5
timeout_code => "event.set('result', 'error')"
}
}
if [type] == "Answer" {
aggregate {
task_id => "%{id}"
code => "event.set('result', 'OK')"
end_of_task => true
}
}
Check out the documentation for the aggregate filter here.
I have tried something similar to AutoMapper Custom Mappings
however, what i really want is to not map to another property but ignore it.
i have tried:
.ForMember(m=>m.BillingAddress,m=>m.ResolveUsing((result, card) => {
if (!string.IsNullOrEmpty(card.BillingDetails?.Address1)) {
return card.BillingDetails.Address1;
}
else {
return result.Ignore();
}
}))
but this just sets some type of resolution result to the property i'm trying to map to.
What i'd really like to do is what I attempted to ask in this issue:
https://github.com/AutoMapper/AutoMapper/issues/1690
i.e.
.ForMember(m=>m.BillingAddress, m=>{
m.Condition(s=>!String.IsNullOrEmpty(s.BillingDetails?.Address1), m.MapFrom(...), m.Ignore())
}
right now it's nulling out anything i have in those fields if i use the .condition
and a .MapFrom after it.
This isn't really how i'd like this to work, but it worked for this particular situation. It would still be nice to have what i wanted before but it looks like if you don't do a mapFrom at all it simply ignores it.
.ForMember(m => m.BillingAddress, m => {
m.Condition(s => !String.IsNullOrEmpty(s.BillingDetails?.Address1));
m.MapFrom(i => i.BillingDetails.Address1);
})
.ForMember(m => m.BillingAddress2, m => {
m.Condition(s => !String.IsNullOrEmpty(s.BillingDetails?.Address2));
m.MapFrom(i => i.BillingDetails.Address2);
})
.ForMember(m => m.City, m => {
m.Condition(s => !String.IsNullOrEmpty(s.BillingDetails?.City));
m.MapFrom(i => i.BillingDetails.City);
})
.ForMember(m => m.State, m => {
m.Condition(s => !String.IsNullOrEmpty(s.BillingDetails?.State));
m.MapFrom(i => i.BillingDetails.State);
})
.ForMember(m => m.Zip, m => {
m.Condition(s => !String.IsNullOrEmpty(s.BillingDetails?.Zip));
m.MapFrom(i => i.BillingDetails.Zip);
})
This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 6 years ago.
I read nodejs 5.4 docs, where I found a => symbol in an example and a new way to define callback function. I don't know why => is used ? is it really a new way to define callbacks ? This is the example from node 5.4 docs.
const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data',(data) => {
console.log(`stdout: ${data}`);
});
ls.on('close',(code) => {
console.log(`child process exited with code ${code}`);
});
It is an EcmaScript 6 arrow function.
It isn't just node.js specific. It works in modern web browsers too.