作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我认为这里的冗余可以通过使用 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/
我认为这里的冗余可以通过使用 insde ramda 中的一些函数来删除,但我对这个库非常陌生,所以我不知道如何实现。如果有一些帮助,我们将不胜感激 let lib = { getFormat
我是一名优秀的程序员,十分优秀!