data-point
data-point copied to clipboard
Reducer lists with undefined as input
Problem description:
Reducer lists are not evaluated correctly when the input is undefined. This happens because they use Promise.reduce (from Bluebird), which uses the first array element as the input when the actual input is undefined.
https://github.com/ViacomInc/data-point/blob/master/packages/data-point/lib/reducer-types/reducer-list/resolve.js#L24
Reproduction:
const DataPoint = require('data-point')
const dp = DataPoint.create()
const functionA = (value) => 'A'
return 'A'
}
const functionB = (value) => {
// value will NOT be 'A', as might be expected
// instead, it will be the functionA function
console.log({ value })
return 'B'
}
// functionA will never be called; instead, because
// acc.value is undefined, bluebird will use functionA
// as the input to functionB
const reducer = [functionA, functionB]
dp.resolve(reducer, undefined)
Suggested solution:
undefined should never be used as the input to Promise.reduce; instead, if the input is undefined, it should be replaced with null.