简体   繁体   English

如何在 Ramda 中重写 object 道具的类型检查

[英]How can i rewrite type checks of object props in Ramda

I'm trying put my head around how rewrite the following TS code using Ramda:我正在尝试如何使用 Ramda 重写以下 TS 代码:

const uniqueIdentifierRegEx = /.*id.*|.*name.*|.*key.*/i;
const uniqueIdentifierPropTypes = ['string', 'number', 'bigint'];

const findUniqueProp = (obj: any) => Object.keys(obj)
    .filter(prop => uniqueIdentifierPropTypes.includes(typeof obj[prop]))
    .find(prop => uniqueIdentifierRegEx.test(prop));

I ended up with something like this, but it does not really work:我最终得到了这样的东西,但它并没有真正起作用:

const data = {a:1, name: 'name', nameFn: () => {}};
const uniqueIdentifierRegEx = /.*id.*|.*name.*|.*key.*/i;
const uniqueIdentifierPropTypes = ['string', 'number', 'bigint'];

const filterPred = curry((obj, prop_, types) => includes(type(prop(prop_, obj)), types));
const findProd = curry((prop_, regEx) => regEx.test(prop))

const findUniqueProp = (obj, types, regEx) =>
   pipe(
     keys,
     filter(filterPred(types)),
     find(findProd(regEx))
   )(obj)

findUniqueProp(data, uniqueIdentifierPropTypes, uniqueIdentifierRegEx)

Potentially, pickBy can be used to filter out props... but Im lost.潜在地, pickBy可以用来过滤掉道具……但我迷路了。 Please help to connect the dots.请帮助连接点。

To transform an object properties by key and value, it's often easiest to convert the object to an array of [key, value] pairs via R.toPairs , transform the pairs (using R.filter in this case), and then convert back to an object using R.fromPairs . To transform an object properties by key and value, it's often easiest to convert the object to an array of [key, value] pairs via R.toPairs , transform the pairs (using R.filter in this case), and then convert back to使用 R.fromPairs 的R.fromPairs

To filter the pairs, I'm using R.where to check the key (index 0 in the pair), and the value (index 1 in the pair) by wrapping the RegExp, and the array of permitted types in functions.为了过滤这些对,我使用R.where通过包装 RegExp 和函数中允许的类型数组来检查键(对中的索引 0)和值(对中的索引 1)。

Note: R.type returns the types in pascal case - String , Number , and BigInt .注意: R.type返回帕斯卡大小写的类型 - StringNumberBigInt

 const { test, pipe, type, flip, includes, toPairs, filter, where, fromPairs } = R const uniqueIdentifierRegEx = test(/.*id.*|.*name.*|.*key.*/i) // test if string matches regexp const uniqueIdentifierPropTypes = pipe(type, flip(includes)(['String', 'Number', 'BigInt'])) // test if the type of the value is in the array const fn = pipe( toPairs, // convert to [key, value] pairs filter(where([ // filter and use where to check the key (0) and the value (1) uniqueIdentifierRegEx, uniqueIdentifierPropTypes ])), fromPairs // convert back to object ) const data = {a: 1, name: 'name', nameFn: () => {}} const result = fn(data) console.log(result)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js"></script>

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

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