Skip to content

Commit

Permalink
yoloing at 2024-11-26T10.51
Browse files Browse the repository at this point in the history
  • Loading branch information
byt3h3ad committed Nov 26, 2024
1 parent 367f5de commit 45bbe40
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions javascript/pipe-function.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Pipe function in JavaScript

```javascript
const surprise =
(...fns) =>
(input) =>
fns.reduce((acc, fn) => fn(acc), input);
```

A pipe function allows you to chain multiple operations together by taking a series of functions as arguments and applying them in a specific order to the input.

Instead of doing something like this.

```javascript
const toUpperCase = (str) => str.toUpperCase();
const removeSpaces = (str) => str.replace(/\s/g, "");
const addExclamation = (str) => str + "!";

toUpperCase(removeSpaces(addExclamation("Subscribe to Bytes")));
```

You can do something like this.

```javascript
const pipe =
(...fns) =>
(input) =>
fns.reduce((acc, fn) => fn(acc), input);

const formatString = pipe(toUpperCase, removeSpaces, addExclamation);

formatString("Subscribe to Bytes"); // SUBSCRIBETOBYTES!
```

[source](https://bytes.dev/archives/341)

0 comments on commit 45bbe40

Please sign in to comment.