【发布时间】:2019-09-06 17:57:56
【问题描述】:
我正在寻找一种在 lodash 中将类似于 overSome 或 flow 的函数链接在一起的方法。
这是写出的类似函数。
const e = async ({planGroups, uuid, name, user, organization}) => {
const a = this.getPlanGroupByUuid({ planGroups, uuid })
if (a) return a
const b = this.getPlanGroupByName({ planGroups, name })
if (b) return b
const c = await this.createPlanGroup({ name, user, organization })
return c
}
e({planGroups, uuid, name, user, organization})
这将是作曲版本。
const x = await _.overSome(this.getPlanGroupByUuid, this.getPlanGroupByName, this.createPlanGroup)
x({planGroups, uuid, name, user, organization})
这个想法是该函数返回具有truthy 值的第一个函数。
这可能仅在 lodash 中实现吗?有没有更好的支持 typescript 的组合函数库?
【问题讨论】:
-
我认为这是 Maybe/Option monad 的完美用例。我建议查看
fp-ts、purify-ts或类似的。对于fp-ts,您可能需要使用Option、pipe、chain,如果混合使用异步,则使用Task。
标签: typescript lodash fp-ts