Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@quadient/evolve-data-transformations

Package Overview
Dependencies
Maintainers
5
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@quadient/evolve-data-transformations

Library for data transformations.

  • 0.0.6
  • npm
  • Socket score

Version published
Maintainers
5
Created
Source

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
  • JSON
  • CSV
  • Streams

XML

Async streaming XML Parser and Writer.

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 through the write method and the callback given to constructor receives a string with the XML content.

To integrate XML processing with streams there are TransformStream classes StringToXmlTransformStream and XmlToStringTransformStream for convenient use with streams.

XML Example

const writer = new XmlWriter(async (str) => {
    console.log(str);
});

const parser = new XmlParser(async (event) => {
    if (event.type === XmlEventType.START_TAG) {
        let elem = event.details as ElementDetails;
        if (elem.name == "name") {
            elem.name = "fixedName"
        }
    } else if (event.type === XmlEventType.END_TAG) {
        if (event.details === "name") {
            event.details = "fixedName"
        }
    }
    await writer.write(event);
});

await parser.parse(`<person><name>Fred</name></person>`);
await parser.flush(); // must be called at the end of parsing
await writer.flush(); // must be called at the end of writing

Output:

<person><fixedName>Fred</fixedName></person>

JSON

Async streaming JSON Parser and Writer.

JsonParser can be used to process strings with parts of the JSON content. The callback given to parser constructor receives events of type JsonEvent.

JsonWriter is the opposite component. It receives JsonEvent objects in the write method and the callback given to constructor receives a string with the JSON content.

To integrate XML processing with streams there are TransformStream classes StringToJsonTransformStream and JsonToStringTransformStream for convenient use with streams.

JSON Example

let writer = new JsonWriter(async (str) => {
    console.log(str);
});

let parser = new JsonParser(async (event) => {
    if(event.type === JsonEventType.PROPERTY_NAME && event.data === "name") {
        event.data = "fixedName";
    }
    await writer.write(event);
})

await parser.parse(`{"person": {"name":"Fred"}}`);
await parser.flush(); // must be called at the end of parsing
await writer.flush(); // must be caleld at the end of writing

Output:

{"person":{"fixedName":"Fred"}}

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

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.

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);
            }
        })
    }
}

FAQs

Package last updated on 04 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc