3.0.3 • Published 2 years ago

@szilanor/stream v3.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years 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

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.0.44

4 years ago

1.0.43

4 years ago

1.0.42

4 years ago

1.0.41

4 years ago

1.0.40

4 years ago

1.0.39

4 years ago

1.0.38

4 years ago

1.0.37

4 years ago

1.0.36

4 years ago

1.0.35

4 years ago

1.0.34

4 years ago

1.0.33

4 years ago

1.0.32

4 years ago

1.0.31

4 years ago

1.0.30

4 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.27

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.3

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago