From 41fe13165f3baed605c7d02a6e54026b6b57e1f7 Mon Sep 17 00:00:00 2001 From: Ilya Mochalov Date: Thu, 30 Jul 2015 11:49:12 +0500 Subject: [PATCH] lodash: added _.flow() and _.flowRight() (aliases: _.backflow(), _.compose()) --- lodash/lodash-tests.ts | 38 +++++++++++++---------- lodash/lodash.d.ts | 68 +++++++++++++++++++++++++++++++++++------- 2 files changed, 80 insertions(+), 26 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index afdc3c0e8..d7d8e3562 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -754,6 +754,12 @@ _.forEach(saves, function (type) { asyncSave({ 'type': type, 'complete': done }); }); +// _.backflow +var testBackflowSquareFn = (n: number) => n * n; +var testBackflowAddFn = (n: number, m: number) => n + m; +result = _.backflow<(n: number, m: number) => number>(testBackflowSquareFn, testBackflowAddFn)(1, 2); +result = _(testBackflowSquareFn).backflow<(n: number, m: number) => number>(testBackflowAddFn).value()(1, 2); + var funcBind = function(greeting: string, punctuation: string) { return greeting + ' ' + this.user + punctuation; }; var funcBound1: (punctuation: string) => any = _.bind(funcBind, { 'name': 'moe' }, 'hi'); funcBound1('!'); @@ -795,21 +801,11 @@ funcBindKey(); funcBindKey = _(objectBindKey).bindKey('greet', 'hi').value(); funcBindKey(); -var realNameMap: { [index: string]: string; } = { - 'curly': 'jerome' -}; - -var format = function (name: string) { - name = realNameMap[name.toLowerCase()] || name; - return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase(); -}; - -var greet = function (formatted: string) { - return 'Hiya ' + formatted + '!'; -}; - -result = _.compose(greet, format); -result = <_.LoDashObjectWrapper>_(greet).compose(format); +// _.compose +var testComposeSquareFn = (n: number) => n * n; +var testComposeAddFn = (n: number, m: number) => n + m; +result = _.compose<(n: number, m: number) => number>(testComposeSquareFn, testComposeAddFn)(1, 2); +result = _(testComposeSquareFn).compose<(n: number, m: number) => number>(testComposeAddFn).value()(1, 2); var createCallbackObj: { [index: string]: string; } = { name: 'Joe' }; result = <() => any>_.createCallback('name'); @@ -858,6 +854,18 @@ var log = _.bind(console.log, console); result = _.delay(log, 1000, 'logged later'); result = <_.LoDashWrapper>_(log).delay(1000, 'logged later'); +// _.flow +var testFlowSquareFn = (n: number) => n * n; +var testFlowAddFn = (n: number, m: number) => n + m; +result = _.flow<(n: number, m: number) => number>(testFlowAddFn, testFlowSquareFn)(1, 2); +result = _(testFlowAddFn).flow<(n: number, m: number) => number>(testFlowSquareFn).value()(1, 2); + +// _.flowRight +var testFlowRightSquareFn = (n: number) => n * n; +var testFlowRightAddFn = (n: number, m: number) => n + m; +result = _.flowRight<(n: number, m: number) => number>(testFlowRightSquareFn, testFlowRightAddFn)(1, 2); +result = _(testFlowRightSquareFn).flowRight<(n: number, m: number) => number>(testFlowRightAddFn).value()(1, 2); + var fibonacci = _.memoize(function (n: any): number { return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2); }); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index c75ca9979..2fc5536bf 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -5183,6 +5183,21 @@ declare module _ { after(func: Function): LoDashObjectWrapper; } + //_.backflow + interface LoDashStatic { + /** + * @see _.flowRight + */ + backflow(...funcs: Function[]): TResult; + } + + interface LoDashObjectWrapper { + /** + * @see _.flowRight + **/ + backflow(...funcs: Function[]): LoDashObjectWrapper; + } + //_.bind interface LoDashStatic { /** @@ -5261,21 +5276,16 @@ declare module _ { //_.compose interface LoDashStatic { /** - * Creates a function that is the composition of the provided functions, where each function - * consumes the return value of the function that follows. For example, composing the functions - * f(), g(), and h() produces f(g(h())). Each function is executed with the this binding of the - * composed function. - * @param funcs Functions to compose. - * @return The new composed function. - **/ - compose(...funcs: Function[]): Function; + * @see _.flowRight + */ + compose(...funcs: Function[]): TResult; } interface LoDashObjectWrapper { /** - * @see _.compose - **/ - compose(...funcs: Function[]): LoDashObjectWrapper; + * @see _.flowRight + */ + compose(...funcs: Function[]): LoDashObjectWrapper; } //_.createCallback @@ -5442,6 +5452,42 @@ declare module _ { ...args: any[]): LoDashWrapper; } + //_.flow + interface LoDashStatic { + /** + * Creates a function that returns the result of invoking the provided functions with the this binding of the + * created function, where each successive invocation is supplied the return value of the previous. + * @param funcs Functions to invoke. + * @return Returns the new function. + */ + flow(...funcs: Function[]): TResult; + } + + interface LoDashObjectWrapper { + /** + * @see _.flow + **/ + flow(...funcs: Function[]): LoDashObjectWrapper; + } + + //_.flowRight + interface LoDashStatic { + /** + * This method is like _.flow except that it creates a function that invokes the provided functions from right + * to left. + * @param funcs Functions to invoke. + * @return Returns the new function. + */ + flowRight(...funcs: Function[]): TResult; + } + + interface LoDashObjectWrapper { + /** + * @see _.flowRight + **/ + flowRight(...funcs: Function[]): LoDashObjectWrapper; + } + //_.memoize interface LoDashStatic { /**