How do you chain functions using lodash?

前端 未结 4 1599
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 19:36

I have an object that looks like

var foundUser = {
    charData: []
}

which then I load an object from a database using mysql and then I ca

4条回答
  •  时光取名叫无心
    2020-12-23 20:07

    As an alternative to the wrap-chain-unwrap pattern (nothing inherently wrong with it, but alternatives are always interesting) there's another way you can check.

    Try by leveraging _.flow.

    The idea is that every function inside flow will receive as input the output of the previous one, which is exactly what you need. An example, given this data:

    var foundUser = {
        charData: []
    };
    
    var rows = [{ok: 1, blah: 'nope'}];
    

    Using Lodash FP we can pass the data as last argument. This feature along with the auto-currying of each method in Lodash/fp makes our life easier when composing functions. This means we can have this succinct and expressive code:

    _.flow(
     _.assign(rows[0]),
     _.omit('blah')
    )(foundUser);
    
    // >> {"charData":[],"ok": 1}
    

    Using the standard version of Lodash we have to curry those functions ourselves using _.partial, and the code will certainly look less terse, but it is still possible to do so:

    _.flow(
     _.partialRight(_.assign, rows[0]),
     _.partialRight(_.omit, 'blah')
    )(foundUser);
    
    // >> {"charData":[],"ok": 1}
    

    A great benefit of using flow rather than chaining is that you can easily mix Lodash methods with your own custom functions, since - as said - all they need is just data as input and data as output, and nothing else:

    const yourFunction = d => ({ ...d, yourProp: 4 });
    
    _.flow(
     _.assign(rows[0]),
     yourFunction,
     _.omit('blah')
    )(foundUser);
    
    // >> {"charData":[],"ok": 1, yourProp: 4}
    

    This makes function composition much easier and more flexible and arguably will naturally lead to more expressive code.

    Another thing to note. If you are installing and importing only the Lodash methods you use, you will have to add just the flow package and not the whole Lodash library as you'd do with chaining.

    npm i --save lodash.flow
    

    Vs

    npm i --save lodash
    

    Perhaps a negligible advantage in many real-world applications where having the full build of Lodash is not a problem and arguably easier to maintain up to date, but very handy in case you are writing a library or a script that will be distributed to use as a third party tool. In that case you will be able to keep your footprint way lower in terms of distributed size.

    Lodash methods docs:

    • _.flow
    • _.partial
    • _.partialRight

    Articles worth checking out:

    • Lodash from chaining to piping (disclaimer: I wrote it)
    • 3 lodash functions you should be using in your Javascript
    • Why chaining is a mistake

    A few other things to note:

    • In Lodash/fp Flow is aliased as _.pipe, so you can pick whichever you prefer.

    • You can also use _.flowRight (or its fp alias _.compose) if you prefer right to left composition, as seen in Ramda's compose.

    Example:

    _.flow(
     _.assign(rows[0]), // #1st to execute
     yourFunction,  // #2
     _.omit('blah'),  // #3
    )(foundUser);
    
    // is the same as...
    
    _.flowRight(
     _.omit('blah'), // #3rd to execute
     yourFunction, // #2
     _.assign(rows[0]), // #1
    )(foundUser);
    

提交回复
热议问题