From eefe51e32eeb8904b7d99866929d7351447184fb Mon Sep 17 00:00:00 2001 From: datadiode Date: Wed, 10 Jul 2024 22:55:12 +0200 Subject: [PATCH 1/3] Allow non-recursive select() with lambdas, by accepting additional arrows to indicate the relative depth from the current node's perspective at which to search, like in r => r => r.name == "ChildTag" --- js/pjxml.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/js/pjxml.js b/js/pjxml.js index da90f5c..ef50b89 100644 --- a/js/pjxml.js +++ b/js/pjxml.js @@ -398,19 +398,26 @@ var pjXML = (function () { if (typeof xpath === 'function') { const ar = []; - function safWorker(nd) { - if (xpath(nd)) { + function safWorker(nd, xpath, depth) { + const res = xpath(nd); + + if (typeof res === 'function') { + xpath = res; + depth = 2; + } else if (res) { ar.push(nd); } - const el = nd.elements(); + if (--depth) { + const el = nd.elements(); - for (let i = 0; i < el.length; i++) { - safWorker(el[i]); + for (let i = 0; i < el.length; i++) { + safWorker(el[i], xpath, depth); + } } } - safWorker(this); + safWorker(this, xpath, 0); return ar; } From f6f75f4b8b188f09f2df3aad0ea6de4abd8ea4a0 Mon Sep 17 00:00:00 2001 From: datadiode Date: Fri, 12 Jul 2024 12:03:16 +0200 Subject: [PATCH 2/3] Demo on parsing MusicXML with pjXML using nested lambdas --- nested-lambda-demo.html | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 nested-lambda-demo.html diff --git a/nested-lambda-demo.html b/nested-lambda-demo.html new file mode 100644 index 0000000..e5a03e6 --- /dev/null +++ b/nested-lambda-demo.html @@ -0,0 +1,40 @@ + + + + + + +

Demo on parsing MusicXML with pjXML using nested lambdas

+

Usage: Download lily-1941d34e.xml and drop it into the below file input field.

+ +

+

+

+

+


From e5412485d28d3be099616ea7bfe4bb93f46190f6 Mon Sep 17 00:00:00 2001
From: datadiode 
Date: Wed, 24 Jul 2024 10:04:59 +0200
Subject: [PATCH 3/3] Give select() with lambdas a choice between either doing
 a wild search for nodes matching, or requiring the current node to already
 match the start condition, where a return value of null rather than false
 opts for the latter

---
 js/pjxml.js | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/js/pjxml.js b/js/pjxml.js
index ef50b89..baddac4 100644
--- a/js/pjxml.js
+++ b/js/pjxml.js
@@ -406,6 +406,8 @@ var pjXML = (function () {
           depth = 2;
         } else if (res) {
           ar.push(nd);
+        } else if (res === null) {
+          depth = 1;
         }
 
         if (--depth) {