structure-stream

1.3.3 • Public • Published

StructureStreamJS

Java Stream-like API for JavaScript programmers.

Description

A lightweight and easy-to-use JavaScript library for data manipulation using stream processing. StructureStreamJS provides a simple and intuitive way to manipulate arrays and objects using functional programming concepts such as map, filter, and reduce.

StructureStreamJS provides functionality like Java Stream in JavaScript which will certainly be very useful especially for those who have a development background in Java.

JavaScript already has a number of built-in features such as map, filter, and reduce which are similar to Java Stream, but incomplete and not as complete as Java Stream. Also, with this package, JavaScript programmers can use a syntax that is more familiar to those with experience with Java Stream.

This package is expected to help increase productivity and efficiency in application development, especially for processing complex data and processing a lot of data in a short time.

Installation

You can install StructureStreamJS using npm:

npm install structure-stream

Usage

To use StructureStreamJS, simply require the package in your JavaScript file:

const structureStream = require("structure-stream");

Or you can directly use it with static method of ArrayStreamer & ObjectStreamer class:

Array Stream

const ArrayStreamer = require("structure-stream/src/class/ArrayStreamer");

let exampleArray = [1, 2, 3];
ArrayStreamer.of(exampleArray);

Object Stream

const ObjectStreamer = require("structure-stream/src/class/ObjectStreamer");

let exampleArray = { name: "Linda", surname: "Dian" };
ObjectStreamer.of(exampleArray);

Creating a stream

To create a stream, pass an array or an object to the structureStream() function:

const data = [1, 2, 3, 4, 5];
const myStream = structureStream(data);

Using basic operators

After creating a stream, you can use stream operators such as map, filter, and reduce to manipulate the data:

// Multiply each item in the stream by 2
const result = myStream
    .map((item) => item * 2)
    .toArray();
console.log(result); // [2, 4, 6, 8, 10]

// Filter the stream to include only even numbers
const result2 = myStream
    .filter((item) => item % 2 === 0)
    .toArray();
console.log(result2); // [2, 4]

// Reduce the stream to find the sum of all items
const result3 = myStream
    .reduce((acc, item) => acc + item, 0);
console.log(result3); // 15

Chaining stream operators

You can chain multiple stream operators together to create more complex data manipulation:

const data2 = [
    { name: "John", age: 23 },
    { name: "Jane", age: 31 },
    { name: "Bob", age: 28 },
    { name: "Alice", age: 19 },
];

const myStream2 = structureStream(data2);

// Filter the stream to include only people under 30,
// map the stream to include only their names,
// and reduce the stream to concatenate the names
const result4 = myStream2
    .filter((person) => person.age < 30)
    .map((person) => person.name)
    .reduce((acc, name) => acc + ", " + name, "");
console.log(result4); // "John, Alice"

Converting a stream to Array or Object

To convert a stream back to an array, use the toArray() method:

const data3 = [1, 2, 3, 4, 5];
const myStream3 = structureStream(data3);

const result5 = myStream3
    .map((item) => item * 2)
    .toArray();
console.log(result5); // [2, 4, 6, 8, 10]

To convert a stream back to an object, use the toObject() method:

const data4 = {
    name: "John",
    age: 23,
    gender: "male",
};

const myStream4 = structureStream(data4);

const result6 = myStream4
    .filter((value, key) => typeof value === "string")
    .toObject();
console.log(result6); // {name: "John", gender: "male"}

License

This package is released under the MIT license.

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i structure-stream

      Weekly Downloads

      0

      Version

      1.3.3

      License

      MIT

      Unpacked Size

      9.87 kB

      Total Files

      8

      Last publish

      Collaborators

      • erlanggariansyah