data-point icon indicating copy to clipboard operation
data-point copied to clipboard

Add toReducerFromPath as a helper function

Open acatl opened this issue 6 years ago • 0 comments
trafficstars

Problem description:

The path reducer allows us to get a specific path from an input object, but at times we want to manipulate the object afterward, because of the nature of reducers being async, adding multiple reducers could not be the most optimal.

['$sone.path.here', syncMethod]

Suggested solution:

Create a higher order function reducer that will output a reducer that executes a given method with its input being the result of a lookup on an Object's path:


// over simplified:
const toReducerFromPath = (method) => (valuePath, defaultValue) => (value) => method(get(valuePath, defaultValue))
// So  instead of this: 
dataPoint.resolve(['$some.path.zipCode',  _.toInteger], input)

// To this: 
const toIntegerFromPath = toReducerFromPath(_.toInteger)

dataPoint.resolve(toIntegerFromPath('$some.path.zipCode'), input)

is toPathReducer the best name?

AC:

  • This method could be exposed under require(data-point/helpers)
  • if no valuePath is received then execute the method against the reducer's value
  • should use same path parser the pathReducer uses

acatl avatar Dec 13 '18 18:12 acatl