From 42f9dafc3f64deb0c37cad6e2e9ce9f9bad03eb2 Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Thu, 17 Aug 2017 16:05:37 -0700 Subject: [PATCH] feat(pipe): rename compose utility function to pipe --- spec/util/{compose-spec.ts => pipe-spec.ts} | 14 ++++----- src/Observable.ts | 4 +-- src/Rx.ts | 2 +- src/operators/reduce.ts | 6 ++-- src/util/compose.ts | 34 --------------------- src/util/pipe.ts | 34 +++++++++++++++++++++ 6 files changed, 47 insertions(+), 47 deletions(-) rename spec/util/{compose-spec.ts => pipe-spec.ts} (67%) delete mode 100644 src/util/compose.ts create mode 100644 src/util/pipe.ts diff --git a/spec/util/compose-spec.ts b/spec/util/pipe-spec.ts similarity index 67% rename from spec/util/compose-spec.ts rename to spec/util/pipe-spec.ts index 176875cf10..63288c7fc5 100644 --- a/spec/util/compose-spec.ts +++ b/spec/util/pipe-spec.ts @@ -1,16 +1,16 @@ 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); @@ -18,13 +18,13 @@ describe('compose', () => { 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 = {}; diff --git a/src/Observable.ts b/src/Observable.ts index c274698439..41ab5faf5c 100644 --- a/src/Observable.ts +++ b/src/Observable.ts @@ -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 { subscribe(observerOrNext?: PartialObserver | ((value: T) => void), @@ -325,6 +325,6 @@ export class Observable implements Subscribable { return this as any; } - return composeFromArray(operations)(this); + return pipeFromArray(operations)(this); } } diff --git a/src/Rx.ts b/src/Rx.ts index ff8e6ca712..5b486675d1 100644 --- a/src/Rx.ts +++ b/src/Rx.ts @@ -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'; diff --git a/src/operators/reduce.ts b/src/operators/reduce.ts index 04f043274e..cc95f9cd23 100644 --- a/src/operators/reduce.ts +++ b/src/operators/reduce.ts @@ -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(accumulator: (acc: T, value: T, index: number) => T, seed?: T): MonoTypeOperatorFunction; @@ -63,11 +63,11 @@ export function reduce(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): Observable { - 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): Observable { - return compose(scan((acc, value, index) => { + return pipe(scan((acc, value, index) => { return accumulator(acc, value, index + 1); }), takeLast(1))(source); }; diff --git a/src/util/compose.ts b/src/util/compose.ts deleted file mode 100644 index 157a039e9c..0000000000 --- a/src/util/compose.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { noop } from './noop'; -import { UnaryFunction } from '../interfaces'; - -/* tslint:disable:max-line-length */ -export function compose(): UnaryFunction; -export function compose(op1: UnaryFunction): UnaryFunction; -export function compose(op1: UnaryFunction, op2: UnaryFunction): UnaryFunction; -export function compose(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction): UnaryFunction; -export function compose(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction): UnaryFunction; -export function compose(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction): UnaryFunction; -export function compose(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction): UnaryFunction; -export function compose(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction, op7: UnaryFunction): UnaryFunction; -export function compose(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction, op7: UnaryFunction, op8: UnaryFunction): UnaryFunction; -export function compose(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction, op7: UnaryFunction, op8: UnaryFunction, op9: UnaryFunction): UnaryFunction; -/* tslint:enable:max-line-length */ - -export function compose(...fns: Array>): UnaryFunction { - return composeFromArray(fns); -} - -/* @internal */ -export function composeFromArray(fns: Array>): UnaryFunction { - if (!fns) { - return noop as UnaryFunction; - } - - if (fns.length === 1) { - return fns[0]; - } - - return function composed(input: T): R { - return fns.reduce((prev: any, fn: UnaryFunction) => fn(prev), input); - }; -} diff --git a/src/util/pipe.ts b/src/util/pipe.ts new file mode 100644 index 0000000000..c67e10c5e5 --- /dev/null +++ b/src/util/pipe.ts @@ -0,0 +1,34 @@ +import { noop } from './noop'; +import { UnaryFunction } from '../interfaces'; + +/* tslint:disable:max-line-length */ +export function pipe(): UnaryFunction; +export function pipe(op1: UnaryFunction): UnaryFunction; +export function pipe(op1: UnaryFunction, op2: UnaryFunction): UnaryFunction; +export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction): UnaryFunction; +export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction): UnaryFunction; +export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction): UnaryFunction; +export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction): UnaryFunction; +export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction, op7: UnaryFunction): UnaryFunction; +export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction, op7: UnaryFunction, op8: UnaryFunction): UnaryFunction; +export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction, op7: UnaryFunction, op8: UnaryFunction, op9: UnaryFunction): UnaryFunction; +/* tslint:enable:max-line-length */ + +export function pipe(...fns: Array>): UnaryFunction { + return pipeFromArray(fns); +} + +/* @internal */ +export function pipeFromArray(fns: Array>): UnaryFunction { + if (!fns) { + return noop as UnaryFunction; + } + + if (fns.length === 1) { + return fns[0]; + } + + return function piped(input: T): R { + return fns.reduce((prev: any, fn: UnaryFunction) => fn(prev), input); + }; +}