Skip to content

Introduction

Value Pipes are functions responsible for modification of values generated by Value Generators.

What really Value Pipes are

Each Value Pipe can be described as this:

type ValuePipe = (value: GeneratedValue) => GeneratedValue;

Simply put, it's technically just a function performing a modification to the generated value.

In other words, the Value pipe is only performing some post-generation modifications to the value. In the end, this modified value is then returned as the result of the Value Generator.

1
2
3
4
5
6
7
// Let's imagine this is a Generated value
const value = "my testing value";

// Define your Value Pipe
const valuePipe = (value: string) => value.toUpperCase();

console.log(valuePipe(value));

Output

'MY TESTING VALUE'
1
2
3
4
5
6
7
// Let's imagine this is a Generated value
const value = "my testing value";

// Find your Value Pipe
const valuePipe = getValuePipe('uppercase');

console.log(valuePipe(value));

Output

'MY TESTING VALUE'
const config: ConstantValueConfig = {
    value: 'my testing value',
    pipes: [
        'uppercase'
    ]
}

const generator = getValueGenerator('constant-value', config);

console.log(generator.generate());

Output

'MY TESTING VALUE'
const schema: SchemaInput = {
    fields: {
        value: {
            type: 'constant-value',
            config: {
                value: 'my testing value',
                pipes: [
                    'uppercase'
                ]
            }
        }
    }
}

const fabricator = new Fabricator(schema);

console.log(fabricator.generate());

Output

{ value: 'MY TESTING VALUE' }

How to use Value Pipes

Value Pipes are defined as a part of Value Generator configuration; just after the Value Generator creates the value, it checks there are any Value pipes defined and if so, it pipes it through all of them, one by one.

Value Generator Configuration with Pipes

const generator = getValueGenerator('constant-value', {
    // Fields required by the Value Generator you choose
    value: 'my constant string',

    // Value Pipes declaration
    pipes: [
        'uppercase',
        'space-split'
    ]
});

console.log(generator.generate())

Output

[ 'MY', 'CONSTANT', 'STRING' ]

Tip

This example uses Constant Value Generator. You may want to check it out 😉

Note

Specifying the Value Pipes is optional. When none is provided, the Value Generator simply returns the value it generated.

On the other hand, when you specify it, keep in mind that an array of functions is expected.

As you can see above, the pipes can be organized into a one multistep pipeline updating the generated value

Order matters

Watch out! The order of the Value Pipes matter; when you don't keep this in mind, you can get into trouble very soon!

Custom Value Pipes

You may easily find yourself in situation when you can't find any predefined Value Pipe doing what you need. Then, it would be nice to define your own custom pipe doing exactly what you need, right?

Single-purpose Value Pipe

As you can see above, the Value pipe is simply just a function taking a value and returning a modified one. No big deal.

When you need, you can pass this to configuration of a Value Generator and it will try to use it.

Single-purpose Value Pipe Example

const generator = getValueGenerator('constant-value', {
    // Fields required by the Value Generator you choose
    value: 'my constant string',

    // Value Pipes declaration
    pipes: [
        (value: string) => value.toUpperCase()
    ]
});

console.log(generator.generate())

Output

'MY CONSTANT STRING'

Tip

This example uses Constant Value Generator. You may want to check it out 😉

Registration of Custom Value Pipes

However, at some places, you might think that some functions are kind of repetetive and you need to use the very same at many places.

For this reason, you can register your Value Pipe into the Falbricate internal ecosystem with a name assigned to it. Then, you only refer to your pipe just the same as you do to get some of the predefined ones.

Registration of a Custom Value Pipe

1
2
3
4
5
6
7
8
9
const myPipe = (value: number) => {

    // ... Do something with the given value ...
    const newValue = value * 7

    return newValue
}

registerValuePipe('my-custom-value-pipe', myPipe);

Unique names

Keep in mind the names has to be unique! When you try to use a name that is already reserved for another Value Pipe, it ends up in an Error!

When you define your custom pipe, then you can use it anywhere you want.

Using of a Custom Value Pipe

const config: ConstantValueGenerator = {
    value: 4,
    pipes: [
        'my-custom-value-pipe'
    ]
}

const generator = getValueGenerator('constant-value', config);

console.log(generator.generate());

Output

28

Tip

This example uses Constant Value Generator. You may want to check it out 😉