3.0.3 • Published 12 months ago

@szilanor/stream v3.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Stream API

Type-safe API for processing Iterable and AsyncIterable data (Arrays, Sets, Maps) similarly to Java 8 Stream API, LINQ or Kotlin Sequences.

Classic JS vs Stream API solution

// Classic
let result;
result = [1, 2, 3].filter(x => x % 2 === 0).map(x => x * 2);

// Stream API
result = stream([1, 2, 3])
    .pipe(
        filter(x => x % 2 === 0),
        map(x => x * 2)
    )
    .collect(toArray());

Why Stream API?

  • Can achieve faster results and lower memory usage due to sequential processing.
const input = [1, 2, 3, .... 10000];
let allOdd: boolean;

// Classic JS
allOdd = input
    .map(x => x + 1)
    .every(x => x % 2 === 1);

// Result: 2, 3, 4 .... 10000 false

// Stream API
allOdd = from(input)
  .pipe(map(x => x + 1))
  .collect(every(x => x % 2 === 1));

// Result: 2, false
  • Async support
const result = await stream([1, 2, 3, 4])
  .pipeAsync(mapAsync(async (id) => 
      await fetch(`https://jsonplaceholder.typicode.com/users/${id}`)
      .then(result => result.json()))
  )
  .collectAsync(toArrayAsync());
  • More readable and fewer lines of code
const input = [1, 1, 1, 1, 2, 3, 4, 4, 5];
let oddOrEvenWithoutDuplicates: Map<string, number[]>;

// Classic JS
oddOrEvenWithoutDuplicates = new Map<string, number[]>();
for (let x of new Set<number>(input))
  const key = x % 2 === 0 ? 'even' : 'odd';
  if (resultClassic.has(key)) {
    resultClassic.get(key).push(x);
  } else {
    resultClassic.set(key, [x]);
  }
}

// Stream API
oddOrEvenWithoutDuplicates = stream(input)
  .pipe(distinct())
  .collect(groupBy(x => (x % 2 === 0 ? 'even' : 'odd')));
  • You can create your own operators and collectors if you don't find what you need
import {CollectorFunction, OperationFunction} from '@szilanor/stream';

const myAwesomeCollector: CollectorFunction<unknown, unknown> = {
  /* your own implementation */
};
const myAwesomeOperation: OperationFunction<unknown, unknown> = {
  /* your own implementation */
};

const result = of(1, 2, 3)
  .pipe(myAwesomeOperation())
  .collect(myAwesomeCollector());
3.0.3

12 months ago

3.0.2

12 months ago

3.0.1

12 months ago

3.0.0

12 months ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.0.44

3 years ago

1.0.43

3 years ago

1.0.42

3 years ago

1.0.41

3 years ago

1.0.40

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.3

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago