gpt4 book ai didi

javascript - 这里有更好的方法来使用 applySpec

转载 作者:行者123 更新时间:2023-12-02 23:50:42 26 4
gpt4 key购买 nike

我认为这里的冗余可以通过使用 insde ramda 中的一些函数来删除,但我对这个库非常陌生,所以我不知道如何实现。如果有一些帮助,我们将不胜感激

let lib = {
getFormattedPropsForUser: R.compose(
R.pickBy(R.identity),
R.applySpec({
username: R.prop('username'),
password: R.prop('password')
})),
getFormattedQueryParamsForUser: R.compose(
R.pickBy(R.identity),
R.applySpec({
_id: R.prop('_id'),
username: R.prop('username'),
password: R.prop('password')
})
)

};

最佳答案

将两个应用程序的公共(public)部分提取到一个函数中,并添加使用部分应用程序和object spread向规范添加更多项目的功能。 .

示例:

const forUser = spec => R.compose(
R.pickBy(R.identity),
R.applySpec({
...spec,
username: R.prop('username'),
password: R.prop('password')
})
)

const lib = {
getFormattedPropsForUser: forUser(),
getFormattedQueryParamsForUser: forUser({ _id: R.prop('_id') }),
}

const test = { _id: 'id', username: 'username', password: 'password' }

console.log(lib.getFormattedPropsForUser(test))

console.log(lib.getFormattedQueryParamsForUser(test))
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

关于javascript - 这里有更好的方法来使用 applySpec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55658919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com