Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Latest commit

 

History

History

README.md

Streams

In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). (↪wiki)

In @fluentfixture, factories are small and valuable units, but they are very incapable of building complex and conditional data. Streams are factory wrappers (stream is a kind of factory) that provide a fluent interface. All methods of streams create other streams that wrap themselves. Like any factory, streams are also immutable and reusable types.

{% hint style="info" %} Stream instances cannot be initialized directly. Instead of this, generator functions can be used. {% endhint %}

Decomposition Of A Stream

In the following example, a stream is built by compositions of many components.

import { int } from '@fluentfixture/core';

const stream = int(1, 100)
  .add(0.5)
  .array(10)
  .sort((a,b) => a - b)
  .join('/')
  .format('numbers = ${}')
  .upperCase();

console.log(stream.single());
// Prints like;
// NUMBERS = 4.5/5.5/40.5/41.5/48.5/52.5/56.5/68.5/72.5/83.5

Let's decompose the stream above to understand the stream and factory concept.

CodeOutputSub-ComponentJob
int(1, 100)NumberStreamIntegerFactorygenerates an integer
.add(0.5)NumberStreamFunctionDecoratoradds 0.5 to the previous output
.array(10)ArrayStreamIteratorIterates to decorated factory
.sort(...)ArrayStreamFunctionDecoratorsorts the previous output
.join(...)StringStreamFunctionDecoratormerge the previos output
.format(...)StringStreamFormatDecoratorformats the previous output
.upperCase()StringStreamFunctionDecoratorconverts to upper case the previous outout