需求场景:因为业务中需要去调用第三方的接口,某些字段需要做校验,但是接口返回的规则都是一些提示,像这样的: '金额区间: 140 x 350USD'、'最低金额: x 140.89USD'、'三边之和: 10.0cm x 20.0cm'...需要自己去里面提取规则和上下限值来判断。
范围总共有 16 种情况:
function isRangeIn(x, str) {
if (!/^[+-]?(0|([1-9]d*))(.d+)?$/.test(x)) {
console.log('当前值非数字')
return
}
if (!str || (str && typeof str !== 'string')) return true // 规则字符串不存在默认通过
const arr = str.match(/d+(.d+)?/g)
if (!arr || !arr.length) return true // 未提取到范围数据默认通过
const numFirst = +arr[0]
const numTwo = +arr[1]
let isVali = true
if (str.includes(' x ')) {
isVali = (numFirst <= x) && (x <= numTwo)
} else if (str.includes(' x<')) {
isVali = (numFirst <= x) && (x < numTwo)
} else if (str.includes('= x) && (x >= numTwo)
} else if (str.includes(' x>')) {
isVali = (numFirst >= x) && (x > numTwo)
} else if (str.includes('>x ')) {
isVali = (numFirst > x) && (x >= numTwo)
} else if (str.includes('>x>')) {
isVali = (numFirst > x) && (x > numTwo)
} else if (str.includes('x ')) {
isVali = x <= numFirst
} else if (str.includes('x<')) {
isVali = x < numFirst
} else if (str.includes('x ')) {
isVali = x >= numFirst
} else if (str.includes('x>')) {
isVali = x > numFirst
} else if (str.includes(' x')) {
isVali = numFirst <= x
} else if (str.includes('= x
} else if (str.includes('>x')) {
isVali = numFirst > x
}
return isVali
}
// 测试
// const a = isRangeIn(6.9999999999, '第三长边: 4.0cm10.0cm')
// const a = isRangeIn(9, '两长边之和: 10.0cm x 20.0cm')
// const a = isRangeIn('141', '金额区间: 140 x 350USD')
// console.log(a)
面对这种很无奈的需求,一定要自己从逻辑上梳理清楚,不要漏掉任何一种可能的情况
TODO
规则里如果有数字,上面提取出来的 numFirst 和 numTwo 会不对,如:两短边之和*2:x 120cm,方法里的数字提取需优化
页面更新:2024-04-23
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号