返回自定义的联合函数, 返回从提供的参数验证函数返回`true`的第一个参数

5年以前  |  阅读数:358 次  |  编程语言:JavaScript 

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

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

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

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8