简体   繁体   English

Ramda - 如何验证 object?

[英]Ramda - How to validate an object?

I would like to refactor the code below using Ramda.我想使用 Ramda 重构下面的代码。 I want to compare the list of keys and values with the data that I receive from the server, if the data matches, the function run.我想将键和值列表与从服务器接收的数据进行比较,如果数据匹配,则 function 运行。 To do this, I created a list of keys and values and using include() I get a boolean .为此,我创建了一个键和值列表,并使用include()我得到一个boolean

//example data

data = { fieldName: 'secretKey', fieldValue: 'ymt' } 


const fieldNames = ['secretKey', 'activateCode']
const fieldValues = ['ymt', 'IdTYj']

if ( fieldNames.includes(data.fieldName) || fieldValues.includes(data.fieldValue)) {
    yield put(isLoading(true));
}

Instead of include() I used R.include but I always get false而不是include()我使用R.include但我总是得到false

const field = R.equals( fieldNames, data.fieldName) // return false

Which method in Ramda is more suitable for comparing keys and values to return a boolean ? Ramda 中的哪种方法更适合比较键和值以返回boolean

I think you're looking for where eg我认为where正在寻找例如

const check = where({ username: either(equals('foo'), equals('FOO'))
                    , password: either(equals('bar'), equals('BAR'))});

check({username: 'foo', password: 'qux'}); // false
check({username: 'qux', password: 'bar'}); // false
check({username: 'foo', password: 'bar'}); // true
check({username: 'foo', password: 'BAR'}); // true
check({username: 'FOO', password: 'BAR'}); // true

I'd rather do something alike, You need to R.flip(R.includes) to use it within your algorithm我宁愿做类似的事情,你需要R.flip(R.includes)在你的算法中使用它

 const put = R.identity; const isLoading = R.identity; function* validate(data) { const keys = ['secretKey', 'activateCode']; const values = ['ymt', 'IdTYj']; const predicate = R.where({ fieldName: R.includes(R.__, keys), fieldValue: R.includes(R.__, values), }); if (predicate(data)) { yield put(isLoading(true)); } }; const iterator = validate({ fieldName: 'secretKey', fieldValue: 'ymt', }); console.log( iterator.next().value );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM