Skip to content

Commit

Permalink
added pipe package (#492)
Browse files Browse the repository at this point in the history
* added `pipe` package

NOTE: I did not rollup config because I wasn't sure how I should configure it

* fix md-variables and add more tests for pipe

* generate readme for pipe

Co-authored-by: Thomas Clark <thomas.clark@bigtincan.com>
  • Loading branch information
TClark1011 and Thomas Clark committed Jan 2, 2023
1 parent d3ab472 commit ca899a9
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 0 deletions.
11 changes: 11 additions & 0 deletions md-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -1599,5 +1599,16 @@
"const squareFn = (number) => number * number;",
"deepMapValues({ a: 1, b: { c: 2, d: { e: 3 } } }, squareFn); // => { a: 1, b: { c: 4, d: { e: 9 } } }"
]
},
"just-pipe": {
"packageName": "just-pipe",
"dir": "function-pipe",
"description": "Pass a value through a pipeline of functions",
"examples": [
"import pipe from 'just-pipe",
"",
"pipe(3, a => a+1, b => b*2) // 8",
"pipe('John Smith', a => a.split(' '), b => b.reverse(), c => c[0]) // 'Smith'"
]
}
}
21 changes: 21 additions & 0 deletions packages/function-pipe/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 angus croll

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions packages/function-pipe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- DO NOT EDIT THIS FILE! THIS FILE WAS AUTOGENERATED BY TEMPLATE-MATE -->
<!-- SEE https://github.com/angus-c/just/blob/master/CONTRIBUTING.md#readme-template -->

## just-pipe

Part of a [library](https://anguscroll.com/just) of zero-dependency npm modules that do just do one thing.
Guilt-free utilities for every occasion.

[`🍦 Try it`](https://anguscroll.com/just/just-pipe)

```shell
npm install just-pipe
```
```shell
yarn add just-pipe
```

Pass a value through a pipeline of functions

```js
import pipe from 'just-pipe
pipe(3, a => a+1, b => b*2) // 8
pipe('John Smith', a => a.split(' '), b => b.reverse(), c => c[0]) // 'Smith'
```
79 changes: 79 additions & 0 deletions packages/function-pipe/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
declare function pipe<A, B>(value: A, ab: (a: A) => B): B

declare function pipe<A, B, C>(
value: A,
ab: (a: A) => B,
bc: (b: B) => C,
): C

declare function pipe<A, B, C, D>(
value: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
): D

declare function pipe<A, B, C, D, E>(
value: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
): E

declare function pipe<A, B, C, D, E, F>(
value: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
): F

declare function pipe<A, B, C, D, E, F, G>(
value: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
): G

declare function pipe<A, B, C, D, E, F, G, H>(
value: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
): H

declare function pipe<A, B, C, D, E, F, G, H, I>(
value: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
): I

declare function pipe<A, B, C, D, E, F, G, H, I, J>(
value: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
): J

export default pipe;
24 changes: 24 additions & 0 deletions packages/function-pipe/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = pipe;

/*
pipe("hello", (a) => a.concat(" world")); // "hello world"
pipe(5, a => a * 2, b => b + 1); // 11
*/

function pipe(value, ...fns) {
if (!arguments.length) {
throw new Error('expected one value argument and least one function argument');
}
if (!fns.length) {
throw new Error(
'expected at least one (and probably more) function arguments'
);
}

var result = fns[0](value);
var len = fns.length;
for (var i = 1; i < len; i++) {
result = fns[i](result);
}
return result;
}
25 changes: 25 additions & 0 deletions packages/function-pipe/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var functionPipe = pipe;
/*
pipe("hello", (a) => a.concat(" world")); // "hello world"
pipe(5, a => a * 2, b => b + 1); // 11
*/

function pipe(value, ...fns) {
if (!arguments.length) {
throw new Error('expected one value argument and least one function argument');
}
if (!fns.length) {
throw new Error(
'expected at least one (and probably more) function arguments'
);
}

var result = fns[0](value);
var len = fns.length;
for (var i = 1; i < len; i++) {
result = fns[i](result);
}
return result;
}

export {functionPipe as default};
38 changes: 38 additions & 0 deletions packages/function-pipe/index.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pipe from './index';

// Case 1) Value and 1 function
pipe('hello', a => a.concat(' world'));

// Case 2) Value and 2 functions
pipe(
5,
a => a * 2,
b => b + 1
);

// Case 3) Value and 9 functions
pipe(
{},
a => ({
...a,
field: 'text',
}),
b => b.field,
c => c.concat(' value'),
d => d.length,
Math.sqrt,
f => f * f,
g => new Date(g),
h => h.toJSON(),
i => i.trim(),
);

//@ts-expect-error
pipe();

//@ts-expect-error
pipe(5);

const arr: unknown[] = [];
//@ts-expect-error
pipe(arr, Math.sqrt);
29 changes: 29 additions & 0 deletions packages/function-pipe/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "just-pipe",
"version": "1.0.0",
"description": "Pass a value through a pipeline of functions",
"main": "index.js",
"module": "index.mjs",
"exports": {
".": {
"require": "./index.js",
"default": "./index.mjs"
}
},
"types": "index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rollup -c"
},
"repository": "https://github.com/angus-c/just",
"keywords": [
"pipe",
"function",
"just"
],
"author": "Thomas Clark",
"license": "MIT",
"bugs": {
"url": "https://github.com/angus-c/just/issues"
}
}
44 changes: 44 additions & 0 deletions test/function-pipe/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var test = require('../util/test')(__filename);
var pipe = require('../../packages/function-pipe');
var camelCase = require('../../packages/string-camel-case');
var last = require('../../packages/array-last');

test('Correctly pipes value through one or more functions', function(t) {
t.plan(4);
t.equal(pipe('hello world', camelCase), 'helloWorld');

function addOne(a) {
return a + 1;
}
function double(b) {
return b * 2;
}
t.equal(pipe(3, addOne, double), 8);
t.equal(pipe(3, double, addOne), 7);

function prependZero(arr) {
return [0, ...arr];
}
function reverse(arr) {
return [...arr].reverse();
}

t.equal(pipe([1, 2, 3], prependZero, reverse, last), 0);
t.end();
});

test('Throws if passed bad parameters', function(t) {
t.plan(3);

t.throws(function() {
pipe();
}, Error);
t.throws(function() {
pipe(5);
}, Error);
t.throws(function() {
pipe(5, 'not a function');
});

t.end();
});

0 comments on commit ca899a9

Please sign in to comment.