coalesceFactory - 自定义的 coalesce 函数

优质
小牛编辑
128浏览
2023-12-01

返回自定义的 coalesce 函数,该函数返回从提供的参数验证函数返回 true 的第一个参数。

使用 Array.find() 返回从提供的参数验证函数返回 true 的第一个参数。

const coalesceFactory = valid => (...args) => args.find(valid);
const customCoalesce = coalesceFactory(_ => ![null, undefined, '', NaN].includes(_));
customCoalesce(undefined, null, NaN, '', 'Waldo'); // "Waldo"