@quadient/evolve-data-transformations
Advanced tools
Comparing version 0.0.3 to 0.0.6
export * from "./csv-parser"; | ||
export * from "./string-to-csv-transformer"; | ||
export * from "./string-to-csv-transform-stream"; |
export * from "./csv-parser"; | ||
export * from "./string-to-csv-transformer"; | ||
export * from "./string-to-csv-transform-stream"; |
export * from "./json-writer"; | ||
export * from "./json-parser"; | ||
export * from "./json-parser-helper"; | ||
export * from "./json-to-string-transformer"; | ||
export * from "./string-to-json-transformer"; | ||
export * from "./json-to-string-transform-stream"; | ||
export * from "./string-to-json-transform-stream"; |
export * from "./json-writer"; | ||
export * from "./json-parser"; | ||
export * from "./json-parser-helper"; | ||
export * from "./json-to-string-transformer"; | ||
export * from "./string-to-json-transformer"; | ||
export * from "./json-to-string-transform-stream"; | ||
export * from "./string-to-json-transform-stream"; |
@@ -34,2 +34,3 @@ /** | ||
* XML document. | ||
* Default is true. | ||
*/ | ||
@@ -112,10 +113,13 @@ entityReplacement?: boolean; | ||
* ``` | ||
* import { XmlParser, Event } from "xml-parser"; | ||
* function handleEvent(event, details) { | ||
* console.log(Event[event] + " " + JSON.stringify(details)); | ||
* import { XmlParser, XmlEventType } from "@quadient/evolve-data-transformations"; | ||
* function handleEvent(event) { | ||
* console.log(XmlEventType[event.type] + " " + JSON.stringify(event.details)); | ||
* } | ||
* let p = new XmlParser(handleEvent, {entityReplacement:false}); | ||
* p.parse("<a>hello"); | ||
* p.parse("</a>"); | ||
* p.close(); // Not really needed for correct input, only to throw error in case of incomplete input. | ||
* | ||
* (async function() { | ||
* const p = new XmlParser(handleEvent, {entityReplacement:false}); | ||
* await p.parse("<a>hello"); | ||
* await p.parse("</a>"); | ||
* await p.flush(); | ||
* })() | ||
* ``` | ||
@@ -156,5 +160,10 @@ */ | ||
/** | ||
* Parses a chunk of input data. | ||
* @async | ||
*/ | ||
parse(text: string): Promise<void>; | ||
/** | ||
* Must be called at the end of parsing. | ||
* It finishes parsing. It checks that the input is properly finished and complete. | ||
*/ | ||
flush(): Promise<void>; | ||
@@ -161,0 +170,0 @@ /** |
@@ -5,3 +5,3 @@ { | ||
"description": "Library for data transformations.", | ||
"version": "0.0.3", | ||
"version": "0.0.6", | ||
"main": "dist/index.js", | ||
@@ -8,0 +8,0 @@ "types": "dist/index.d.ts", |
# Data Transformations Package | ||
## Overview | ||
The Data Transformations package contains helper utilities to wrestle with JSON, | ||
XML and CSV data formats. | ||
Add to project: | ||
``` | ||
npm install @quadient/evolve-data-transformations | ||
``` | ||
## Table of Contents | ||
- [XML](#xml) | ||
- [XML Example](#xml-example) | ||
- [JSON](#json) | ||
- [JSON Example](#json-example) | ||
- [CSV](#csv) | ||
- [Streams](#Streams) | ||
## Overview | ||
The Data Transformations package contains helper utilities to work with JSON, | ||
XML and CSV data formats. | ||
## XML | ||
@@ -18,12 +23,14 @@ | ||
`XmlParser` receives strings with parts of the XML content in the | ||
"write" method and the callback given to parser constructor receives events | ||
`XmlParser` can be used to process strings with parts of the XML content. | ||
The callback given to parser constructor receives events | ||
of type XmlEvent. | ||
`XmlWriter` is the opposite component. It receives `XmlEvent` objects in the | ||
`XmlWriter` is the opposite component. It receives `XmlEvent` objects through the | ||
`write` method and the callback given to constructor receives a string with the | ||
XML content. | ||
There are also predefined TransformStream classes `StringToXmlTransformStream` and | ||
`XmlToStringTransformStream` for convenient use with streams. | ||
To integrate XML processing with streams there are | ||
[TransformStream](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream) | ||
classes `StringToXmlTransformStream` and `XmlToStringTransformStream` for convenient | ||
use with streams. | ||
@@ -33,7 +40,7 @@ ### XML Example | ||
```javascript | ||
let writer = new XmlWriter(async (str) => { | ||
const writer = new XmlWriter(async (str) => { | ||
console.log(str); | ||
}); | ||
let parser = new XmlParser(async (event) => { | ||
const parser = new XmlParser(async (event) => { | ||
if (event.type === XmlEventType.START_TAG) { | ||
@@ -67,4 +74,4 @@ let elem = event.details as ElementDetails; | ||
`JsonParser` receives strings with parts of the JSON content in the | ||
`write` method and the callback given to parser constructor receives events | ||
`JsonParser` can be used to process strings with parts of the JSON content. | ||
The callback given to parser constructor receives events | ||
of type JsonEvent. | ||
@@ -76,2 +83,8 @@ | ||
To integrate XML processing with streams there are | ||
[TransformStream](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream) | ||
classes `StringToJsonTransformStream` and `JsonToStringTransformStream` for convenient | ||
use with streams. | ||
### JSON Example | ||
@@ -100,1 +113,53 @@ | ||
``` | ||
## CSV | ||
Async streaming CSV parser. | ||
`CsvParser` can be used to process csv content as strings. It produces | ||
event objects and sends them to a callback. | ||
### CSV Example | ||
```javascript | ||
const p = new CsvParser(async (event) => { | ||
console.log(event.type + " - " + event.data); | ||
}); | ||
await p.parse('head') | ||
await p.parse('er1,header2\nvalue1,value2'); | ||
await p.flush(); | ||
``` | ||
Output: | ||
``` | ||
header - [ 'header1', 'header2' ] | ||
values - [ 'value1', 'value2' ] | ||
``` | ||
## Streams | ||
The following example illustrates how the stream-compatible helper classes can be used | ||
in case the input is in the form of | ||
[ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream). | ||
```js | ||
import {StringToXmlTransformStream, XmlEventType} from "@quadient/evolve-data-transformations"; | ||
(async function () { | ||
const response = await fetch("https://httpbin.org/xml"); | ||
const stream = response.body; | ||
stream | ||
.pipeThrough(new TextDecoderStream()) | ||
.pipeThrough(new StringToXmlTransformStream()) | ||
.pipeTo(new ConsoleLogWritableStream()); | ||
})() | ||
class ConsoleLogWritableStream extends WritableStream { | ||
constructor() { | ||
super({ | ||
write(chunk) { | ||
console.log(chunk); | ||
} | ||
}) | ||
} | ||
} | ||
``` |
@@ -1,2 +0,2 @@ | ||
import { StringToCsvTransformer, CsvEvent } from "../../src"; | ||
import { StringToCsvTransformStream, CsvEvent } from "../../src"; | ||
import { describe, it, expect } from "../../../test/src"; | ||
@@ -13,3 +13,3 @@ | ||
}) | ||
.pipeThrough(new StringToCsvTransformer()) | ||
.pipeThrough(new StringToCsvTransformStream()) | ||
.pipeTo( | ||
@@ -16,0 +16,0 @@ new WritableStream<CsvEvent>({ |
@@ -1,2 +0,2 @@ | ||
import { JsonToStringTransformer, StringToJsonTransformer } from "../../src"; | ||
import { JsonToStringTransformStream, StringToJsonTransformStream } from "../../src"; | ||
import { JsonEvent, JsonEventType } from "../../src"; | ||
@@ -15,3 +15,3 @@ import { describe, it, expect, fail } from "../../../test/src"; | ||
}) | ||
.pipeThrough(new StringToJsonTransformer()) | ||
.pipeThrough(new StringToJsonTransformStream()) | ||
.pipeThrough( | ||
@@ -27,3 +27,3 @@ new TransformStream<JsonEvent, JsonEvent>({ | ||
) | ||
.pipeThrough(new JsonToStringTransformer()) | ||
.pipeThrough(new JsonToStringTransformStream()) | ||
.pipeTo( | ||
@@ -30,0 +30,0 @@ new WritableStream<string>({ |
@@ -38,5 +38,5 @@ import { ElementDetails, XmlEventType, XmlParser, XmlWriter, XmlEvent } from "../../src"; | ||
console.log = origConsoleLog; | ||
expect(output).toBe(`<person><fixedName>Fred</fixedName></person>\n`); | ||
console.log = origConsoleLog; | ||
}); | ||
}); |
export * from "./csv-parser"; | ||
export * from "./string-to-csv-transformer"; | ||
export * from "./string-to-csv-transform-stream"; |
export * from "./json-writer"; | ||
export * from "./json-parser"; | ||
export * from "./json-parser-helper"; | ||
export * from "./json-to-string-transformer"; | ||
export * from "./string-to-json-transformer"; | ||
export * from "./json-to-string-transform-stream"; | ||
export * from "./string-to-json-transform-stream"; |
@@ -35,2 +35,3 @@ /** | ||
* XML document. | ||
* Default is true. | ||
*/ | ||
@@ -94,10 +95,13 @@ public entityReplacement?: boolean; | ||
* ``` | ||
* import { XmlParser, Event } from "xml-parser"; | ||
* function handleEvent(event, details) { | ||
* console.log(Event[event] + " " + JSON.stringify(details)); | ||
* import { XmlParser, XmlEventType } from "@quadient/evolve-data-transformations"; | ||
* function handleEvent(event) { | ||
* console.log(XmlEventType[event.type] + " " + JSON.stringify(event.details)); | ||
* } | ||
* let p = new XmlParser(handleEvent, {entityReplacement:false}); | ||
* p.parse("<a>hello"); | ||
* p.parse("</a>"); | ||
* p.close(); // Not really needed for correct input, only to throw error in case of incomplete input. | ||
* | ||
* (async function() { | ||
* const p = new XmlParser(handleEvent, {entityReplacement:false}); | ||
* await p.parse("<a>hello"); | ||
* await p.parse("</a>"); | ||
* await p.flush(); | ||
* })() | ||
* ``` | ||
@@ -143,2 +147,3 @@ */ | ||
/** | ||
* Parses a chunk of input data. | ||
* @async | ||
@@ -150,2 +155,6 @@ */ | ||
/** | ||
* Must be called at the end of parsing. | ||
* It finishes parsing. It checks that the input is properly finished and complete. | ||
*/ | ||
async flush() { | ||
@@ -152,0 +161,0 @@ switch (this.state) { |
Sorry, the diff of this file is too big to display
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
256830
6437
161