const Intervals = (str, val) => { // console.log("匹配 =number:", str.match(/^=(\d+)$/));…
- User
const Intervals = (str, val) => { // console.log("匹配 =number:", str.match(/^=(\d+)/)); // console.log("匹配 [number,number]:", str.match(/^\[(\d+),(\d+)\]/)); // console.log("匹配 [number:", str.match(/^[(\d+)/)); // console.log("匹配 number]:", str.match(/^(\d+)\]/)); // console.log("匹配 [number,number):", str.match(/^[(\d+),(\d+))/)); // console.log("匹配 (number,number]:", str.match(/^\((\d+),(\d+)\]/)); // console.log("匹配 (number:", str.match(/^((\d+)/)); // console.log("匹配 number):", str.match(/^(\d+)\)/));
let result; switch (str) { /** =number / case str.match(/^=(\d+)/)); result = val == str.match(/^=(\d+)/)[1]; break; /** [number,number] */ case str.match(/^\[(\d+),(\d+)\]/)?.input: // console.log("匹配 [number,number]:", str.match(/^[(\d+),(\d+)]/)); result = val >= str.match(/^\[(\d+),(\d+)\]/)[1] && val <= str.match(/^[(\d+),(\d+)]/)?.input: // console.log("匹配 [number:", str.match(/^[(\d+)/)[1]; break; /* number] / case str.match(/^(\d+)]/)?.input: // console.log("匹配 number]:", str.match(/^(\d+)\]/)); result = val < str.match(/^(\d+)]/)[1]; break; /** [number,number) */ case str.match(/^\[(\d+),(\d+)\)/)?.input: // console.log("匹配 [number,number):", str.match(/^[(\d+),(\d+))/)); result = val >= str.match(/^\[(\d+),(\d+)\)/)[1] && val < str.match(/^[(\d+),(\d+))/)[2]; break; /** (number,number] */ case str.match(/^\((\d+),(\d+)\]/)?.input: // console.log("匹配 [number,number):", str.match(/^((\d+),(\d+)]/)); result = val > str.match(/^\((\d+),(\d+)\]/)[1] && val <= str.match(/^((\d+),(\d+)]/)[2]; break; /** (number */ case str.match(/^\((\d+)/)?.input: // console.log("匹配 [number:", str.match(/^((\d+)/)); result = val > str.match(/^\((\d+)/)[1]; break; /* number) */ case str.match(/^(\d+))/)?.input: // console.log("匹配 number]:", str.match(/^(\d+)\)/)); result = val < str.match(/^(\d+))$/)[1]; break;
default: break;}
return result; }; Help me optimize
- Assistant
Here's an optimized version of the
Intervalsfunction:const Intervals = (str, val) => { const singleEquals = str.match(/^=(\d+)$/); const bothInclusive = str.match(/^\[(\d+),(\d+)\]$/); const leftInclusive = str.match(/^\[(\d+)$/); const rightInclusive = str.match(/^(\d+)\]$/); const leftExclusiveRightInclusive = str.match(/^\((\d+),(\d+)\]$/); const rightExclusiveLeftInclusive = str.match(/^\[(\d+),(\d+)\)$/); const leftExclusive = str.match(/^\((\d+)$/); const rightExclusive = str.match(/^(\d+)\)$/); if (singleEquals) { return val === parseInt(singleEquals[1]); } else if (bothInclusive) { return val >= parseInt(bothInclusive[1]) && val <= parseInt(bothInclusive[2]); } else if (leftInclusive) { return val >= parseInt(leftInclusive[1]); } else if (rightInclusive) { return val < parseInt(rightInclusive[1]); } else if (leftExclusiveRightInclusive) { return val > parseInt(leftExclusiveRightInclusive[1]) && val <= parseInt(leftExclusiveRightInclusive[2]); } else if (rightExclusiveLeftInclusive) { return val >= parseInt(rightExclusiveLeftInclusive[1]) && val < parseInt(rightExclusiveLeftInclusive[2]); } else if (leftExclusive) { return val > parseInt(leftExclusive[1]); } else if (rightExclusive) { return val < parseInt(rightExclusive[1]); } return false; };In this optimized version, I've combined the
switchstatement into a series ofifstatements to make the code more readable. I've also stored the results of eachmatchin a variable to avoid repeating thematchcalls, which can help improve performance. Finally, I added a return value offalsein case none of the conditions are met, so the function always returns a boolean value.