@thi.ng/sax
Advanced tools
Comparing version 0.3.5 to 0.3.6
@@ -6,2 +6,10 @@ # Change Log | ||
<a name="0.3.6"></a> | ||
## [0.3.6](https://github.com/thi-ng/umbrella/compare/@thi.ng/sax@0.3.5...@thi.ng/sax@0.3.6) (2018-07-11) | ||
**Note:** Version bump only for package @thi.ng/sax | ||
<a name="0.3.5"></a> | ||
@@ -8,0 +16,0 @@ ## [0.3.5](https://github.com/thi-ng/umbrella/compare/@thi.ng/sax@0.3.4...@thi.ng/sax@0.3.5) (2018-07-09) |
{ | ||
"name": "@thi.ng/sax", | ||
"version": "0.3.5", | ||
"version": "0.3.6", | ||
"description": "Transducer-based, SAX-like, non-validating, speedy & tiny XML parser", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -23,2 +23,6 @@ # @thi.ng/sax | ||
Additionally, since by default the parser emits any children as part of | ||
"element end" events, it can be used like a tree-walking DOM parser as | ||
well (see SVG parsing example further below). The choice is yours! | ||
## Installation | ||
@@ -96,3 +100,5 @@ | ||
transformations to only the `<circle>` elements in the first group | ||
(`<g>`) element. | ||
(`<g>`) element. Btw. The transformed elements can be serialized back | ||
into SVG syntax using | ||
[@thi.ng/hiccup](https://github.com/thi-ng/umbrella/tree/master/packages/hiccup)... | ||
@@ -133,3 +139,3 @@ Given the composed transducer below, parsing stops immediately after the | ||
...e.attribs, | ||
cx: parseFloat(e.attribs.cy), | ||
cx: parseFloat(e.attribs.cx), | ||
cy: parseFloat(e.attribs.cy), | ||
@@ -141,7 +147,90 @@ r: parseFloat(e.attribs.r), | ||
)] | ||
// [ [ 'circle', { cx: 150, cy: 150, r: 50 } ], | ||
// [ 'circle', { cx: 150, cy: 150, r: 50 } ], | ||
// [ [ 'circle', { cx: 50, cy: 150, r: 50 } ], | ||
// [ 'circle', { cx: 250, cy: 150, r: 50 } ], | ||
// [ 'circle', { cx: 150, cy: 150, fill: 'rgba(0,255,255,0.25)', r: 100, stroke: '#ff0000' } ] ] | ||
``` | ||
### DOM-style tree parsing using `defmulti` | ||
```ts | ||
import { defmulti, DEFAULT } from "@thi.ng/defmulti"; | ||
// coerces given attribute IDs into numeric values and | ||
// keeps all other attribs | ||
const numericAttribs = (e, ...ids: string[]) => | ||
ids.reduce( | ||
(acc, id) => (acc[id] = parseFloat(e.attribs[id]), acc), | ||
{ ...e.attribs } | ||
); | ||
// returns iterator of parsed & filtered children of given element | ||
// (iterator is used to avoid extraneous copying at call sites) | ||
const parsedChildren = (e) => | ||
tx.iterator( | ||
tx.comp( | ||
tx.map(parseElement), | ||
tx.filter((e)=> !!e), | ||
), | ||
e.children | ||
); | ||
// define multiple dispatch function, based on element tag name | ||
const parseElement = defmulti((e) => e.tag); | ||
// tag specific implementations | ||
parseElement.add("circle", (e) => | ||
[e.tag, numericAttribs(e, "cx", "cy", "r")]); | ||
parseElement.add("rect", (e) => | ||
[e.tag, numericAttribs(e, "x", "y", "width", "height")]); | ||
parseElement.add("g", (e) => | ||
[e.tag, e.attribs, ...parsedChildren(e)]); | ||
parseElement.add("svg", (e) => | ||
[e.tag, numericAttribs(e, "width", "height"), ...parsedChildren(e)]); | ||
// implementation for unhandled elements | ||
parseElement.add(DEFAULT, () => null); | ||
// using the same SVG source as in previous example: | ||
// the `last()` reducer just returns the ultimate value | ||
// which in this case is the SVG root element's ELEM_END parse event | ||
// this also contains all children (by default) | ||
parseElement(tx.transduce(sax.parse(), tx.last(), svg)); | ||
// ["svg", | ||
// { | ||
// version: "1.1", | ||
// height: 300, | ||
// width: 300, | ||
// xmlns: "http://www.w3.org/2000/svg" | ||
// }, | ||
// ["g", | ||
// { fill: "yellow" }, | ||
// ["circle", { cx: 50, cy: 150, r: 50 }], | ||
// ["circle", { cx: 250, cy: 150, r: 50 }], | ||
// ["circle", | ||
// { | ||
// cx: 150, | ||
// cy: 150, | ||
// fill: "rgba(0,255,255,0.25)", | ||
// r: 100, | ||
// stroke: "#ff0000" | ||
// }], | ||
// ["rect", | ||
// { | ||
// x: 80, | ||
// y: 80, | ||
// width: 140, | ||
// height: 140, | ||
// fill: "none", | ||
// stroke: "black" | ||
// }]], | ||
// ["g", | ||
// { fill: "none", stroke: "black" }, | ||
// ["circle", { cx: 150, cy: 150, r: 50 }], | ||
// ["circle", { cx: 150, cy: 150, r: 25 }]]] | ||
``` | ||
### Error handling | ||
@@ -148,0 +237,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40654
278