Skip to content

Commit a79e0da

Browse files
committed
Added 'Filter XMLLists' code template
1 parent 2d58b77 commit a79e0da

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This Source Code Form is subject to the terms of the Mozilla Public
2+
License, v. 2.0. If a copy of the MPL was not distributed with this
3+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
Files in this subfolder are Copyright Tony Germano 2020.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Filter XMLLists
2+
3+
The xFilter() method creates a new XMLList with all elements that pass the test implemented by the provided function. This function has some advantages over native e4x filtering.
4+
- It passes the actual node to be filtered to the filter function rather than operating on it in a *with* context.
5+
- This allows calling methods on the node as well as referencing child elements that contain javascript operators (most notibly the . character, which is heavily used in hl7.)
6+
- Additional parameters let you limit the number of results returned or throw exceptions if there is an unexpected number of filtered results.
7+
8+
## Syntax
9+
10+
```javascript
11+
var filteredList = xFilter(xmlList, callback(node[, index[, list]])[, minLimit[, maxReturned[, enforceMax]]])
12+
```
13+
14+
### Parameters:
15+
16+
- **xmlList** The XMLList to be filtered
17+
- **callback** Function is a predicate, to test each node of the XMLList. Return `true` to keep the element, `false` otherwise. It accepts 3 arguments
18+
- **node** The current node being processed in the XMLList
19+
- **index** The index of the current node being processed in the XMLList
20+
- **list** The XMLList passed as the first parameter of xFilter()
21+
- **minLimit** Optional. Throw a RangeError if the length of the xmlList to be returned is fewer than this number. Defaults to no lower limit.
22+
- **maxReturned** Optional. Only return up to this many results. This can be used to short circuit the function if you do not need all of the results. Defaults to no upper limit.
23+
- **enforceMax** Optional. Throw a RangeError if more than maxReturned results will be returned. Defaults to false.
24+
25+
### Return Value
26+
27+
A new XMLList with the nodes that pass the test (up to maxReturned.) If no nodes pass the test and minLimit is not set, an empty XMLList will be returned.
28+
29+
30+
### Examples
31+
Get the id that represents the NPI number from the repetition of OBR-16 where OBR-16.9 equals `NPI` or empty string if it doesn't exist. Stop looking after the first match if more than one repetition qualifies.
32+
33+
```javascript
34+
var npiOrdering = xFilter(msg.OBR['OBR.16'], function(obr16) {return obr16['OBR.16.9']=='NPI'}, 0, 1);
35+
var npiNumber = npiOrdering['OBR.16.1'].toString();
36+
```
37+
38+
Compile the OBX-5 text of all OBX segments ignoring the first 5 OBX segments and any OBX segment where OBX-3.2 is not `Report`. Ensure at least one line is printed. Arrow functions can be used when ES6 mode is enabled.
39+
40+
```javascript
41+
var clippedReport = new java.lang.StringBuilder();
42+
try {
43+
for each (var obx in xFilter(msg.OBX, (obx,i) => (i>4 && obx['OBX.3']['OBX.3.2']=='Report'), 1)) {
44+
clippedReport.append(obx['OBX.5']['OBX.5.1']).append('\n');
45+
}
46+
}
47+
// let other exceptions go unchecked
48+
catch(e if e instanceof RangeError) {
49+
// handle report that does not meet requirements
50+
}
51+
```
52+
53+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
Improved XMLList filter over built-in e4x filters.
3+
4+
@copyright 2020 Tony Germano
5+
@license MPL-2.0
6+
7+
@param {XMLList} xmlList - The XMLList to be filtered
8+
@param {Function} callback - Function is a predicate, to test each node of the XMLList. Return
9+
true to keep the element, false otherwise. It accepts 3 arguments - the node, the index of the node,
10+
and xmlList as passed into xFilter
11+
@param {Number} minLimit - (Optional) throw RangeError if at least this many nodes do not pass the
12+
filter
13+
@param {Number} maxReturned - (Optional) only return up to this many results
14+
@param {Boolean} enforceMax - (Optional) throw RangeError if more than maxReturned results will
15+
be returned
16+
@return {XMLList} new XMLList containing filtered results
17+
*/
18+
function xFilter(xmlList, callback, minLimit, maxReturned, enforceMax) {
19+
var ret = new XMLList();
20+
for (var i = 0; i < xmlList.length(); i++) {
21+
var node = xmlList[i];
22+
if (callback(node, i, xmlList)) {
23+
ret += node;
24+
if (enforceMax) {
25+
if (ret.length() > maxReturned) {
26+
throw new RangeError('The number of filtered results is more than ' + maxReturned);
27+
}
28+
}
29+
else if (ret.length() == maxReturned) {
30+
break;
31+
}
32+
}
33+
}
34+
if (ret.length() < minLimit) {
35+
throw new RangeError('The number of filtered results is less than ' + minLimit);
36+
}
37+
return ret;
38+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This is a repository of example channels, code templates, and other scripts you
1212
- Encrypt PDF
1313
- Execute Runtime Command
1414
- Extract Text From PDF
15+
- Filter XMLLists
1516
- Fix HL7 Node Order
1617
- Get Num Pages In PDF
1718
- Get Segments After a Particular Segment

0 commit comments

Comments
 (0)