How can I conditionally show a section based on if a filtered array would show items? - adobe-documentgeneration

I'm using Document Generation and have used filtering in lists like so:
{% repeating-section cats[weight > 5] %}
This will loop over a cats array and only show items where the weight property is over 5. This works fine, but I'm using it in a "section" with an intro and I want to hide the entire section if that particular array is empty. How can I use that in a condition?

Using the JSONata docs, there is a $count operator. This can be used to count the number of items in an array, and include the filter as well. So for example:
{% conditional-section expr($count(cats[weight > 5]) > 0) %}
This can be used around the section that includes your list.

Related

How do I count the number of occurrence of an item in a list? - Dart

I would like to know how many occurrences of a single item consists in a list in dart. As far as I know, the contains method checks only up to the first occurrence and returns a bool.
Following counts how many "1"s are in list:
[1, 2, 1].where((item) => item == 1).length

How can I iterate over list items in Pandoc's lua-filter function?

Pandoc's lua filter makes it really easy to iterate over a document and munge the document as you go. My problem is I can't figure out how to isolate list item elements. I can find lists and the block level things inside each list item, but I can't figure out a way to iterate over list items.
For example let's say I had the following Markdown document:
1. One string
Two string
2. Three string
Four string
Lets say I want to make the first line of each list item bold. I can easily change how the paragraphs are handled inside OrderedLists, say using this filter and pandoc --lua-filter=myfilter.lua --to=markdown input.md
local i
OrderedList = function (element)
i = 0
return pandoc.walk_block(element, {
Para = function (element)
i = i + 1
if i == 1 then return pandoc.Para { pandoc.Strong(element.c) }
else return element end
end
})
end
This will indeed change the first paragraph element to bold, but it only changes the first paragraph of the first list item because it's iterating across all paragraphs in all list items in the list, not on each list item, then on each paragraph.
1. **One string**
Two string
2. Three string
Four string
If I separate the two list items into two separate lists again the first paragraph of the first item is caught, but I want to catch the first paragraph of every list item! I can't find anything in the documentation about iterating over list items. How is one supposed to do that?
The pandoc Lua filter docs have recently been updated with more info on the properties of each type. E.g., for OrderedList elements, the docs should say (it currently says items instead of content, which is a bug):
OrderedList
An ordered list.
content: list items (List of Blocks)
listAttributes: list parameters (ListAttributes)
start: alias for listAttributes.start (integer)
style: alias for listAttributes.style (string)
delimiter: alias for listAttributes.delimiter (string)
tag, t: the literal OrderedList (string)
So the easiest way is to iterate over the content field and change items therein:
OrderedList = function (element)
for i, item in ipairs(element.content) do
local first = item[1]
if first and first.t == 'Para' then
element.content[i][1] = pandoc.Para{pandoc.Strong(first.content)}
end
end
return element
end

.indexOf() equivalent in Neo4j Cypher

No matter how I swing it, I need some kind of function to find the index of a item in an array supplied as a parameter.
I am trying to simply update items in a collection based on the index of one of their properties in an array, and have been poring through Cypher docs for nearly 2 hours...
It would also be acceptable to order the items by that array, and then run a foreach on the ordered list...
Following #stefan-armbruster answer and great blog post, a slow but simple index_of can be done with:
reduce(x=[-1,0], i IN [1,2,7,5,21,5,1,435] |
CASE WHEN i = 21 THEN [x[1], x[1]+1] ELSE [x[0], x[1]+1] END
)[0]
Here reduce function works with a two elements array: the position and the current index. If an element in your array matches the given condition, the first element of the reduced array will be replaced with the current index.
I put an example on neo4j console http://console.neo4j.org/?id=34byv
I've blogged about that recently. You can use the reduce function with an three element array as state variable containing
the index of the highest occupation so far
the current index (aka iteration number)
the value of highest occupation so far
As an example to find the index of max element in an array:
RETURN reduce(x=[0,0,0], i IN [1,2,2,5,2,1] |
CASE WHEN i>x[2] THEN [x[1],x[1]+1,o] ELSE [x[0], x[1]+1,x[2]] END
)[0]

mongoid: select elements that have at least n elements in array

In mongoid you can query items that have at least one element in array:
Item.any_in(tag_ids: [id1,id2,id3])
You can also select elements that have all elements in array:
Item.all_in(tag_ids: [id1,id2,id3])
My Question: Is there any way to query elements that have at least n elements in array ?
I'd like to query something like Item.at_least(tag_ids: [id1,id2,id3], n: 2) to return any Item that share at least two ids with [id1,id2,id3]
Thanks !
I don't know a pure Mongoid-solution. I also haven't found such query in the MongoDB manual: http://docs.mongodb.org/manual/reference/operator/query-array/
I would use some mix of Mongoid and array operations.
The disadvantage of it is, that all items which have at least 1 of these tags will be loaded.
searched_tag_ids = ['54253ad452656b1d25000000','54253adc52656b1d25010000','54253ae352656b1d25020000']
items_with_min_1_searched_tag = Item.any_in(tag_ids: searched_tag_ids).to_a
items_with_min_2_searched_tag = items_with_min_1_searched_tag.select{|item| (item.tag_ids.collect{|tag_id| tag_id.to_s} & searched_tag_ids).size >=2}

Struts2 How to dynamically apply a comparator to a given List

Is it possible to apply one of the comparators on a List based on a drop down selection?
For example, I have two comparators defined:
<s:bean name="comparator.CreateDateComparator" var="createDateComparator"/>
<s:bean name="comparator.LastUpdatedDateComparator" var="lastDateComparator"/>
Based on a certain selected value from a dropdown, I would like use the right comparator for my sorting. Something like this:
<s:if test="dropDownValue=='CREATE_DATE'" >
<s:sort comparator="createDateComparator" source="dataList">

Resources