Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added pipe package #492

Merged
merged 3 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-flow": {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename just-flow to 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.
35 changes: 35 additions & 0 deletions packages/function-pipe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!-- 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
```

Output a new value by passing it 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"
}
}
38 changes: 38 additions & 0 deletions test/function-pipe/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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(3);
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);
Copy link
Owner

@angus-c angus-c Aug 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would also be good to test with args 2 and 3 swapped


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 only give 0 or 1 parameters', function(t) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the fail case where second param is a non-function

t.plan(2);
t.throws(function() {
pipe();
}, Error);
t.throws(function() {
pipe(5);
}, Error);
t.end();
});