jtlt 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGES.md +18 -0
  2. package/README.md +46 -78
  3. package/demo/codemirror.esm.js +28242 -0
  4. package/demo/codemirror.js +94 -0
  5. package/demo/index.css +11 -0
  6. package/demo/index.html +11 -14
  7. package/demo/index.js +210 -26
  8. package/demo/vendor/fontoxpath/dist/fontoxpath.esm.js +600 -0
  9. package/demo/vendor/jamilih/dist/jml.mjs +2341 -0
  10. package/demo/vendor/jhtml/src/SAJJ/SAJJ.ObjectArrayDelegator.js +356 -0
  11. package/demo/vendor/jhtml/src/SAJJ/SAJJ.Stringifier.js +186 -0
  12. package/demo/vendor/jhtml/src/SAJJ/SAJJ.js +746 -0
  13. package/demo/vendor/jhtml/src/SAJJ/testing/SAJJ.html +33 -0
  14. package/demo/vendor/jhtml/src/SAJJ/testing/SAJJ.testing.js +25 -0
  15. package/demo/vendor/jhtml/src/jhtml-browser.js +5 -0
  16. package/demo/vendor/jhtml/src/jhtml-node.cts +3 -0
  17. package/demo/vendor/jhtml/src/jhtml-node.js +8 -0
  18. package/demo/vendor/jhtml/src/jhtml-node.mts +1 -0
  19. package/demo/vendor/jhtml/src/jhtml.cts +3 -0
  20. package/demo/vendor/jhtml/src/jhtml.js +602 -0
  21. package/demo/vendor/jhtml/src/jhtml.mts +1 -0
  22. package/demo/vendor/jsonpath-plus/dist/index-browser-esm.js +2158 -0
  23. package/demo/vendor/prsc/dist/prsc.esm.js +2 -0
  24. package/demo/vendor/simple-get-json/dist/index-es.js +151 -0
  25. package/demo/vendor/whynot/dist/whynot.esm.js +2 -0
  26. package/demo/vendor/xspattern/dist/xspattern.esm.js +2 -0
  27. package/dist/AbstractJoiningTransformer.d.ts +27 -0
  28. package/dist/AbstractJoiningTransformer.d.ts.map +1 -1
  29. package/dist/DOMJoiningTransformer.d.ts +60 -8
  30. package/dist/DOMJoiningTransformer.d.ts.map +1 -1
  31. package/dist/JSONJoiningTransformer.d.ts +8 -9
  32. package/dist/JSONJoiningTransformer.d.ts.map +1 -1
  33. package/dist/JSONPathTransformer.d.ts.map +1 -1
  34. package/dist/JSONPathTransformerContext.d.ts +223 -4
  35. package/dist/JSONPathTransformerContext.d.ts.map +1 -1
  36. package/dist/StringJoiningTransformer.d.ts +7 -7
  37. package/dist/StringJoiningTransformer.d.ts.map +1 -1
  38. package/dist/XPathTransformer.d.ts +2 -2
  39. package/dist/XPathTransformer.d.ts.map +1 -1
  40. package/dist/XPathTransformerContext.d.ts +161 -8
  41. package/dist/XPathTransformerContext.d.ts.map +1 -1
  42. package/dist/index.d.ts +4 -4
  43. package/dist/index.d.ts.map +1 -1
  44. package/docs/API.expanded.md +5 -3
  45. package/docs/API.md +1 -1
  46. package/docs/TO-DO.md +62 -36
  47. package/eslint.config.js +3 -1
  48. package/package.json +28 -4
  49. package/rollup.config.js +13 -0
  50. package/src/AbstractJoiningTransformer.js +42 -0
  51. package/src/DOMJoiningTransformer.js +115 -23
  52. package/src/JSONJoiningTransformer.js +50 -26
  53. package/src/JSONPathTransformer.js +2 -0
  54. package/src/JSONPathTransformerContext.js +936 -5
  55. package/src/StringJoiningTransformer.js +25 -21
  56. package/src/XPathTransformer.js +3 -1
  57. package/src/XPathTransformerContext.js +884 -15
  58. package/src/index.js +16 -7
  59. package/tsconfig.json +5 -2
package/CHANGES.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # jtlt CHANGES
2
2
 
3
+ ## 0.5.0
4
+
5
+ - feat: `forEachGroup`
6
+ - feat: `characterMap` and element fixes
7
+ - feat: `namespace()`
8
+ - feat: `decimalFormat()`
9
+ - fix: `valueOf` and context when no arguments to `applyTemplates()`
10
+ - fix: add childNodes argument to dom joining transformer
11
+
12
+ ## 0.4.0
13
+
14
+ - feat: `number()` enhancements
15
+ - feat: `analyzeString()`
16
+ - feat: xpath 3.1
17
+ - feat: demo
18
+ - fix: set XPath default query to `//*`
19
+ - fix(types): outputType
20
+
3
21
  ## 0.3.0
4
22
 
5
23
  - feat: `if()`
package/README.md CHANGED
@@ -2,14 +2,16 @@
2
2
 
3
3
  JavaScript Template Language Transformations (JTLT, pronounced as
4
4
  "Jetlet")—a JavaScript equivalent of [XSLT](https://www.w3.org/Style/XSL/),
5
- for JSON/JavaScript object data sources.
5
+ for JSON/JavaScript object or XML data sources.
6
6
 
7
7
  As with XSLT, allows for declarative, linear declaration of
8
8
  (recursive) templates and can be transformed into different
9
- formats (e.g., HTML strings, JSON, DOM objects, etc.).
9
+ formats (e.g., strings, JSON, or DOM objects).
10
10
 
11
11
  ***Beta state!!!***
12
12
 
13
+ See the [Demo](https://brettz9.github.io/jtlt/demo/).
14
+
13
15
  ## Credits
14
16
 
15
17
  Packaged with [JSONPath Plus](https://github.com/s3u/JSONPath).
@@ -19,11 +21,11 @@ The sample file is from <https://goessner.net/articles/JsonPath/>
19
21
  ## Installation
20
22
 
21
23
  ```shell
22
- npm install .
24
+ npm install jtlt
23
25
  ```
24
26
 
25
27
  In the browser, you will also need to include the dependencies.
26
- See the [test file](test/test.html).
28
+ See the [test file](./test/browser/index.html).
27
29
 
28
30
  ## Basic usage
29
31
 
@@ -44,9 +46,9 @@ JTLT has two layers:
44
46
  - JSONPathTransformerContext: The execution context passed to templates. It mirrors the joiner API (e.g., string(), object(), array()) so templates can emit results. It also provides helpers like applyTemplates(), callTemplate(), valueOf(), variable(), and forEach().
45
47
  - XPathTransformer (experimental): Applies templates to
46
48
  XML/HTML DOM by matching XPath selectors (and optional modes).
47
- Supports two evaluation modes: version 1 (native
48
- XPathEvaluator) and version 2 (via xpath2.js). Falls back to
49
- built‑in default rules when no template matches.
49
+ Supports three evaluation modes: version 1 (native
50
+ XPathEvaluator), version 2 (via xpath2.js), and version 3 (via fontoxpath).
51
+ Falls back to built‑in default rules when no template matches.
50
52
  - XPathTransformerContext (experimental): Execution context for
51
53
  XPath. Offers get(), forEach(), valueOf(), variable(), key()
52
54
  and the same joiner helpers as the JSONPath context.
@@ -93,55 +95,54 @@ Provide joiningConfig when constructing JTLT:
93
95
  - joiningConfig.xmlElements: Switch element() to XML serialization mode in the String joiner.
94
96
  - joiningConfig.preEscapedAttributes: Skip escaping attribute values in the String joiner.
95
97
 
96
- ### XPath (experimental)
97
-
98
- You can run templates against XML/HTML using XPath instead of JSONPath.
99
-
100
- - Construct with `new XPathTransformer({data, templates,
101
- joiningTransformer, xpathVersion})`.
102
- - `data` should be a Document or Element (e.g., from DOMParser with
103
- `text/xml`).
104
- - `xpathVersion`: `1` uses native XPath (browser like). `2` uses
105
- `xpath2.js` for XPath 2.0‑style evaluation.
106
- - In version 2, some functions may be missing; prefer simple path
107
- expressions. Use version 1 for wide XPath 1.0 function support.
108
-
109
- Example (string output):
98
+ ## Quick start (JSON source)
110
99
 
111
100
  ```js
112
- import {JSDOM} from 'jsdom';
113
- import {StringJoiningTransformer, XPathTransformer} from 'jtlt';
101
+ import {jtlt} from 'jtlt';
114
102
 
115
- const {window} = new JSDOM('<!doctype><html><body></body></html>');
116
- const parser = new window.DOMParser();
117
- const doc = parser.parseFromString(
118
- '<root><item>a</item><item>b</item></root>', 'text/xml'
119
- );
103
+ const data = {title: 'Hello', items: ['a', 'b']};
120
104
 
121
- const joiner = new StringJoiningTransformer('');
122
105
  const templates = [
123
- {name: 'root', path: '/', template () {
124
- this.applyTemplates('//item');
106
+ {path: '$', template () {
107
+ this.applyTemplates();
108
+ }},
109
+ {path: '$.title', template (v) {
110
+ this.string('<h1>', () => this.text(v));
111
+ this.string('</h1>');
125
112
  }},
126
- {name: 'item', path: '//item', template (node) {
127
- this.element('li', {}, [], () => this.text(node.textContent));
113
+ {path: '$.items[*]', template (v) {
114
+ this.element('li', {}, [], () => this.text(v));
128
115
  }}
129
116
  ];
130
117
 
131
- const out = new XPathTransformer({
132
- data: doc,
133
- templates,
134
- joiningTransformer: joiner,
135
- xpathVersion: 1 // or 2
136
- }).transform('');
137
- // -> <li>a</li><li>b</li>
118
+ const out = await jtlt({data, templates, outputType: 'string'});
119
+
120
+ console.log(out);
138
121
  ```
139
122
 
140
- Using the JTLT facade with XPath (no manual joiner needed):
123
+ Notes:
124
+
125
+ - Modes let you organize multiple passes or output targets.
126
+ - You can also call templates by name via this.callTemplate('name').
127
+ - For DOM output, use outputType: 'dom'. For JSON output, use 'json'.
128
+
129
+ ### Quick start (XML source with XPath)
130
+
131
+ You can run templates against XML/HTML using XPath instead of JSONPath.
132
+
133
+ - `data` should be a Document or Element (e.g., from `DOMParser` with
134
+ `text/xml`).
135
+ - `xpathVersion`: `1` uses native XPath (browser like). `2` uses
136
+ `xpath2.js` for XPath 2.0‑style evaluation. and `3.1` uses `fontoxpath`
137
+ for XPath 3.1.
138
+ - In version 2, some functions may be missing; prefer simple path
139
+ expressions. Use version 1 for standard XPath 1.0 function support.
140
+
141
+ Example (string output) using the JTLT facade with XPath:
141
142
 
142
143
  ```js
143
144
  import {JSDOM} from 'jsdom';
144
- import JTLT from 'jtlt';
145
+ import {jtlt} from 'jtlt';
145
146
 
146
147
  const {window} = new JSDOM('<!doctype><html><body></body></html>');
147
148
  const parser = new window.DOMParser();
@@ -165,49 +166,17 @@ const templates = [
165
166
  }
166
167
  ];
167
168
 
168
- const out = new JTLT({
169
+ const out = await jtlt({
169
170
  data: doc,
170
171
  templates,
171
172
  outputType: 'string',
172
173
  engineType: 'xpath',
173
174
  xpathVersion: 1, // or 2
174
175
  success: (res) => res
175
- }).transform('');
176
+ });
176
177
  // -> <li>a</li><li>b</li>
177
178
  ```
178
179
 
179
- ## Quick start
180
-
181
- ```js
182
- import JTLT from 'jtlt';
183
-
184
- const data = {title: 'Hello', items: ['a', 'b']};
185
-
186
- const templates = [
187
- {path: '$', template () {
188
- this.applyTemplates({mode: 'html'});
189
- }},
190
- {mode: 'html', path: '$.title', template (v) {
191
- this.string('<h1>', () => this.text(v));
192
- this.string('</h1>');
193
- }},
194
- {mode: 'html', path: '$.items[*]', template (v) {
195
- this.element('li', {}, [], () => this.text(v));
196
- }}
197
- ];
198
-
199
- const out = new JTLT({data, templates, outputType: 'string'}).
200
- transform('html');
201
-
202
- console.log(out);
203
- ```
204
-
205
- Notes:
206
-
207
- - Modes let you organize multiple passes or output targets.
208
- - You can also call templates by name via this.callTemplate('name').
209
- - For DOM output, use outputType: 'dom'. For JSON output, use 'json'.
210
-
211
180
  ## One-off queries with forQuery (XQuery-like)
212
181
 
213
182
  If you just want to run a single, non-recursive query (similar to an XQuery "for … where … return …"), you can skip defining templates and use `forQuery` to seed a root function that iterates a JSONPath and emits results.
@@ -447,8 +416,7 @@ Differences / current limitations:
447
416
 
448
417
  ## Differences between an exact equivalence with XSLT
449
418
 
450
- JTLT, having the freedom to start a new pattern from XSLT, and though
451
- seeking to learn from it, deviates somewhat from making an exact
419
+ JTLT deviates somewhat from making an exact
452
420
  equivalence with XSLT (to the extent JTLT and JSONPath implement
453
421
  what could possibly be transferred to JSON-based transformations
454
422
  from XSLT):