Is it correct to use lodash inside a pure function to display the input value?

Is it possible to use lodash inside a pure function up to the _.map

input value or can I only use the native Array.map

one which is slower?

For example:

let shortcuts = _.map(state.shortcuts, (shortcut: any) => {
    switch(shortcut.page){
      case "Transfers":  return tassign(shortcut, { badge: action.payload.transfers }); 
      case "Payments":  return tassign(shortcut, { badge: action.payload.payments }); 
      case "Inbox":  return tassign(shortcut, { badge: action.payload.inbox }); 
      case "ConsolidatedPosition":  return tassign(shortcut, { badge: action.payload.consolidatedPosition }); 
      default: return shortcut; 
    }
  });

  return tassign(state, { shortcuts: shortcuts });

      

+3


source to share


2 answers


Lodash map

has no condition and no side effects, so that's okay.



+1


source


A pure function is a function in which only the return value is determined by its input values, with no observable side effects.

Lodash map works in a similar way to Array.prototype.map (), which creates a new array with the results of calling the provided function on each element in that array.



As a general rule of thumb, to keep a function clean, always return a new value and do not perform any other operations outside the scope of that function, such as DOM-related operations, etc.

Pure functions and reduction: http://redux.js.org/docs/introduction/ThreePrinciples.html

+1


source







All Articles