javascript - 咖喱我的函数没有 lodash
问题描述
我一直在涉足函数式编程,我正试图了解部分应用程序和柯里化。
我正在寻找一些关于如何将部分应用程序和柯里化应用于以下功能的见解:
var Page_Validators = [{
"controltovalidate": "Content_C002_txtAddress",
"focusOnError": "t",
"errormessage": "No Address Entered",
"display": "Dynamic",
"validationGroup": "ProfileControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_txtCity",
"focusOnError": "t",
"errormessage": "No City Entered",
"display": "Dynamic",
"validationGroup": "ProfileControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_drpState",
"focusOnError": "t",
"errormessage": "State Required",
"display": "Dynamic",
"validationGroup": "ProfileControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_txtZipcode",
"focusOnError": "t",
"errormessage": "No Zipcode Entered",
"display": "Dynamic",
"validationGroup": "ProfileControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_phoneNumberFull",
"focusOnError": "t",
"errormessage": "Missing Phone Number",
"display": "Dynamic",
"validationGroup": "ProfileControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_txtSupFullName",
"focusOnError": "t",
"errormessage": "No Name Entered",
"display": "Dynamic",
"validationGroup": "SupervisorControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_txtSupTitle",
"focusOnError": "t",
"errormessage": "No Title Entered",
"display": "Dynamic",
"validationGroup": "SupervisorControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_txtSupPhoneFull",
"focusOnError": "t",
"errormessage": "Missing Phone Number",
"display": "Dynamic",
"validationGroup": "SupervisorControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_txtSupEmail",
"focusOnError": "t",
"errormessage": "No Email Entered",
"display": "Dynamic",
"validationGroup": "SupervisorControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_SignatureField",
"focusOnError": "t",
"errormessage": "Signature Required",
"display": "Dynamic",
"validationGroup": "ProfileControl",
"initialvalue": "",
"isvalid": true
}];
function filterArrayBy(arr, searchString) {
return arr.filter(key => key.controltovalidate.toLowerCase().includes(searchString.toLowerCase()));
}
function toggleValidatorsState(arr, condtion) {
return arr.map(el => ValidatorEnable(el, condition));
}
const supervisorValidators = filterArrayBy(Page_Validators, "txtSup");
const disabledValidators = toggleValidatorsState(supervisorValidators, true);
console.log(disabledValidators);
在旁边
toggleValidatorState 函数正在调用 ASP.NET 函数,因此代码片段将不起作用(除非您有 asp.net)
除了 ASP.NET 功能之外,这些功能可以简化或使用带有柯里化的部分应用程序吗?
我觉得我在重复自己,必须将一个数组传递给这两个函数。
我一直在研究使用 Lodash 或 Ramda,但我可以在不使用外部库的情况下实现同样的效果吗?
解决方案
这在这里可能有点矫枉过正,因为代码看起来已经很简单了。
但是你可以filterArayBy
通过将它绑定到Page_Validators
. 因此,您可以通过不同的 searchStrings 对其进行过滤,而无需每次都传递数组。
disableValidators
此外,您可以通过部分应用toggleValidatorsState
with来创建一个新函数condition = true
。
实现这一目标的最简单方法是使用.bind
一个简单的 lambda,如下所示:
function filterArrayBy(arr, searchString) {
return arr.filter(key => key.controltovalidate.toLowerCase().includes(searchString.toLowerCase()));
}
// We are going to partially apply filterArrayBy function as the first argument is going to be the same.
// (in this context partial application does the same as currying)
const filterPageValidators = filterArrayBy.bind(null, Page_Validators);
function toggleValidatorsState(arr, condtion) {
return arr.map(el => ValidatorEnable(el, condition));
}
// Also partially applying toggleValidatorsState to make the code more readable
const disableValidators = arr => toggleValidatorsState(arr, true);
// The usage would look like this
const disabledValidators = disableValidators(filterPageValidators('txtSup'));
如果你想让它更复杂,你可以查看这篇文章实现你自己的curry
方法并使用它来制作filterPageValidators
.
然后你需要类似于 Ramda 的partialRight
. 因此,您可以将此答案用作参考。
最后,你的函数看起来像这样:
const filterPageValidators = curry(filterArrayBy)(Page_Validators);
const disableValidators = bind_trailing_args(toggleValidatorsState, true);
但以我的拙见,对于这种特殊情况来说,这实在是太多了。
推荐阅读
- r - 基于ggplot2中最大距离的空间网络
- javascript - 以块的形式发送 API 响应以响应本机应用程序
- java - 在 while 循环的条件下找不到符号错误
- python - 我如何从 QListWidgetItem 获取文本
- c# - 将 C# 对象转换为 Json 格式
- stata - 通过采用某些观察值的组生成变量
- javascript - 如何在单击列表中更改选项的颜色
- javascript - 通过在 react 类中通过 innerHTML 添加 onclick 事件来调用函数
- algorithm - 为什么 Dijkstra 算法的(最短路径)时间复杂度与 DFS 不同?
- visual-studio - 调试 Nuget 库