Skip to content

Commit

Permalink
Merge pull request #1 from unsplash/fix/various
Browse files Browse the repository at this point in the history
Various small fixes
  • Loading branch information
OliverJAsh authored Jul 5, 2019
2 parents 5289c82 + 519fe44 commit 81228d8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ const result: number = pipeWith(1, add1, times2);
assert.strictEqual(result, 4);
```

## Note about type safety

`pipe` and `pipeWith` currently support up to 9 functions. If more than 9 functions are passed, type safety will be lost. If need be, we are open to adding more overloads to avoid this.

## Development

```
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
"files": [
"target"
],
"dependencies": {
"@types/node": "^12.0.10"
},
"devDependencies": {
"@types/node": "^12.0.10",
"prettier": "^1.18.2",
"source-map-support": "^0.5.12",
"tslint": "^5.18.0",
Expand Down
17 changes: 9 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
type AnyFunction = (...params: unknown[]) => unknown;
type AnyFunction = (...params: any[]) => any;
type UnknownFunction = (...params: unknown[]) => unknown;

// Copied from https://github.com/gcanti/fp-ts/blob/1.15.0/src/function.ts#L209 with some
// modifications to make the first function allow multiple parameters.
export function pipe<A extends unknown[], B>(ab: (...a: A) => B): (...args: A) => B;
export function pipe<A extends unknown[], B, C>(
ab: (...a: A) => B,
bc: (b: B) => C,
Expand Down Expand Up @@ -62,14 +64,16 @@ export function pipe<A extends unknown[], B, C, D, E, F, G, H, I, J>(
hi: (h: H) => I,
ij: (i: I) => J,
): (...args: A) => J;
export function pipe(...fns: AnyFunction[]): AnyFunction {
export function pipe(...fns: AnyFunction[]): UnknownFunction;
export function pipe(...fns: UnknownFunction[]): UnknownFunction {
return (...initialParams: unknown[]): unknown =>
fns.reduce<unknown>((value, fn, index) => {
const params = index === 0 ? (value as unknown[]) : [value];
return fn(...params);
}, initialParams);
}

export function pipeWith<A, B>(a: A, ab: (a: A) => B): B;
export function pipeWith<A, B, C>(a: A, ab: (a: A) => B, bc: (b: B) => C): C;
export function pipeWith<A, B, C, D>(a: A, ab: (a: A) => B, bc: (b: B) => C, cd: (c: C) => D): D;
export function pipeWith<A, B, C, D, E>(
Expand Down Expand Up @@ -129,10 +133,7 @@ export function pipeWith<A, B, C, D, E, F, G, H, I, J>(
hi: (h: H) => I,
ij: (i: I) => J,
): J;
export function pipeWith(value: unknown, ...fns: AnyFunction[]) {
return pipe(
// TODO:
// @ts-ignore
...fns,
)(value);
export function pipeWith(a: unknown, ...fns: AnyFunction[]): unknown;
export function pipeWith(value: unknown, ...fns: UnknownFunction[]): unknown {
return pipe(...fns)(value);
}
33 changes: 33 additions & 0 deletions src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const it = (name: string, fn: () => void) => {
describe('pipe', () => {
it('works when first function has single param', () => {
// One test for each overload
assert.strictEqual(pipe(singleParamFnAdd1)(1), 2);
assert.strictEqual(
pipe(
singleParamFnAdd1,
Expand Down Expand Up @@ -102,6 +103,21 @@ describe('pipe', () => {
)(1),
47,
);
assert.strictEqual(
pipe(
singleParamFnAdd1,
singleParamFnTimes2,
singleParamFnAdd1,
singleParamFnTimes2,
singleParamFnAdd1,
singleParamFnTimes2,
singleParamFnAdd1,
singleParamFnTimes2,
singleParamFnAdd1,
singleParamFnTimes2,
)(1),
94,
);
});

it('works when first function has multiple params', () => {
Expand All @@ -118,6 +134,7 @@ describe('pipe', () => {
describe(pipeWith.name, () => {
it('works', () => {
// One test for each overload
assert.strictEqual(pipeWith(1, singleParamFnAdd1), 2);
assert.strictEqual(pipeWith(1, singleParamFnAdd1, singleParamFnTimes2), 4);
assert.strictEqual(
pipeWith(1, singleParamFnAdd1, singleParamFnTimes2, singleParamFnAdd1),
Expand Down Expand Up @@ -198,5 +215,21 @@ describe(pipeWith.name, () => {
),
47,
);
assert.strictEqual(
pipeWith(
1,
singleParamFnAdd1,
singleParamFnTimes2,
singleParamFnAdd1,
singleParamFnTimes2,
singleParamFnAdd1,
singleParamFnTimes2,
singleParamFnAdd1,
singleParamFnTimes2,
singleParamFnAdd1,
singleParamFnTimes2,
),
94,
);
});
});

0 comments on commit 81228d8

Please sign in to comment.