Skip to content

Commit

Permalink
feat(pipe): rename compose utility function to pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Aug 17, 2017
1 parent 65c5023 commit 42f9daf
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 47 deletions.
14 changes: 7 additions & 7 deletions spec/util/compose-spec.ts → spec/util/pipe-spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { expect } from 'chai';
import { compose } from '../../dist/cjs/util/compose';
import { pipe } from '../../dist/cjs/util/pipe';

describe('compose', () => {
describe('pipe', () => {
it('should exist', () => {
expect(compose).to.be.a('function');
expect(pipe).to.be.a('function');
});

it('should compose two functions together', () => {
it('should pipe two functions together', () => {
const a = x => x + x;
const b = x => x - 1;

const c = compose(a, b);
const c = pipe(a, b);
expect(c).to.be.a('function');
expect(c(1)).to.equal(1);
expect(c(10)).to.equal(19);
});

it('should return the same function if only one is passed', () => {
const a = x => x;
const c = compose(a);
const c = pipe(a);

expect(c).to.equal(a);
});

it('should return a noop if not passed a function', () => {
const c = compose();
const c = pipe();

expect(c('whatever')).to.equal('whatever');
const someObj = {};
Expand Down
4 changes: 2 additions & 2 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IfObservable } from './observable/IfObservable';
import { ErrorObservable } from './observable/ErrorObservable';
import { observable as Symbol_observable } from './symbol/observable';
import { OperatorFunction } from './interfaces';
import { composeFromArray } from './util/compose';
import { pipeFromArray } from './util/pipe';

export interface Subscribable<T> {
subscribe(observerOrNext?: PartialObserver<T> | ((value: T) => void),
Expand Down Expand Up @@ -325,6 +325,6 @@ export class Observable<T> implements Subscribable<T> {
return this as any;
}

return composeFromArray(operations)(this);
return pipeFromArray(operations)(this);
}
}
2 changes: 1 addition & 1 deletion src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export {Timestamp} from './operators/timestamp';
export {TestScheduler} from './testing/TestScheduler';
export {VirtualTimeScheduler} from './scheduler/VirtualTimeScheduler';
export {AjaxRequest, AjaxResponse, AjaxError, AjaxTimeoutError} from './observable/dom/AjaxObservable';
export { compose } from './util/compose';
export { pipe } from './util/pipe';

import { asap } from './scheduler/asap';
import { async } from './scheduler/async';
Expand Down
6 changes: 3 additions & 3 deletions src/operators/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { scan } from './scan';
import { takeLast } from './takeLast';
import { defaultIfEmpty } from './defaultIfEmpty';
import { OperatorFunction, MonoTypeOperatorFunction } from '../interfaces';
import { compose } from '../util/compose';
import { pipe } from '../util/pipe';

/* tslint:disable:max-line-length */
export function reduce<T>(accumulator: (acc: T, value: T, index: number) => T, seed?: T): MonoTypeOperatorFunction<T>;
Expand Down Expand Up @@ -63,11 +63,11 @@ export function reduce<T, R>(accumulator: (acc: R, value: T, index?: number) =>
// means they didn't provide anything or if they literally provided `undefined`
if (arguments.length >= 2) {
return function reduceOperatorFunctionWithSeed(source: Observable<T>): Observable<R> {
return compose(scan(accumulator, seed), takeLast(1), defaultIfEmpty(seed))(source);
return pipe(scan(accumulator, seed), takeLast(1), defaultIfEmpty(seed))(source);
};
}
return function reduceOperatorFunction(source: Observable<T>): Observable<R> {
return compose(scan<T, T | R>((acc, value, index) => {
return pipe(scan<T, T | R>((acc, value, index) => {
return accumulator(<R>acc, value, index + 1);
}), takeLast(1))(source);
};
Expand Down
34 changes: 0 additions & 34 deletions src/util/compose.ts

This file was deleted.

34 changes: 34 additions & 0 deletions src/util/pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { noop } from './noop';
import { UnaryFunction } from '../interfaces';

/* tslint:disable:max-line-length */
export function pipe<T>(): UnaryFunction<T, T>;
export function pipe<T, A>(op1: UnaryFunction<T, A>): UnaryFunction<T, A>;
export function pipe<T, A, B>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>): UnaryFunction<T, B>;
export function pipe<T, A, B, C>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>): UnaryFunction<T, C>;
export function pipe<T, A, B, C, D>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>): UnaryFunction<T, D>;
export function pipe<T, A, B, C, D, E>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>, op5: UnaryFunction<D, E>): UnaryFunction<T, E>;
export function pipe<T, A, B, C, D, E, F>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>, op5: UnaryFunction<D, E>, op6: UnaryFunction<E, F>): UnaryFunction<T, F>;
export function pipe<T, A, B, C, D, E, F, G>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>, op5: UnaryFunction<D, E>, op6: UnaryFunction<E, F>, op7: UnaryFunction<F, G>): UnaryFunction<T, G>;
export function pipe<T, A, B, C, D, E, F, G, H>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>, op5: UnaryFunction<D, E>, op6: UnaryFunction<E, F>, op7: UnaryFunction<F, G>, op8: UnaryFunction<G, H>): UnaryFunction<T, H>;
export function pipe<T, A, B, C, D, E, F, G, H, I>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>, op5: UnaryFunction<D, E>, op6: UnaryFunction<E, F>, op7: UnaryFunction<F, G>, op8: UnaryFunction<G, H>, op9: UnaryFunction<H, I>): UnaryFunction<T, I>;
/* tslint:enable:max-line-length */

export function pipe<T, R>(...fns: Array<UnaryFunction<T, R>>): UnaryFunction<T, R> {
return pipeFromArray(fns);
}

/* @internal */
export function pipeFromArray<T, R>(fns: Array<UnaryFunction<T, R>>): UnaryFunction<T, R> {
if (!fns) {
return noop as UnaryFunction<any, any>;
}

if (fns.length === 1) {
return fns[0];
}

return function piped(input: T): R {
return fns.reduce((prev: any, fn: UnaryFunction<T, R>) => fn(prev), input);
};
}

0 comments on commit 42f9daf

Please sign in to comment.