我如何在 Ramda 中重写对象道具的类型检查
How can i rewrite type checks of object props in 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));
我最终得到了这样的东西,但它并没有真正起作用:
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)
可能,pickBy 可以用来过滤掉 props……但我迷路了。请帮助连接点。
要通过键和值转换对象属性,通常最简单的方法是通过 R.toPairs
将对象转换为 [key, value]
对数组,转换这些对(使用 R.filter
本例),然后使用 R.fromPairs
.
转换回对象
为了过滤对,我使用 R.where
通过包装 RegExp 和数组来检查键(对中的索引 0)和值(对中的索引 1)函数中允许的类型。
注意:R.type
returns 帕斯卡大小写的类型 - String
、Number
和 BigInt
。
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>
我正在思考如何使用 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));
我最终得到了这样的东西,但它并没有真正起作用:
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)
可能,pickBy 可以用来过滤掉 props……但我迷路了。请帮助连接点。
要通过键和值转换对象属性,通常最简单的方法是通过 R.toPairs
将对象转换为 [key, value]
对数组,转换这些对(使用 R.filter
本例),然后使用 R.fromPairs
.
为了过滤对,我使用 R.where
通过包装 RegExp 和数组来检查键(对中的索引 0)和值(对中的索引 1)函数中允许的类型。
注意:R.type
returns 帕斯卡大小写的类型 - String
、Number
和 BigInt
。
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>