* parse LIKE expressions * @param {string} regex - regex pattern * @return {object} MongoDB search object
(regex)
| 4 | * @return {object} MongoDB search object |
| 5 | */ |
| 6 | function parseLikeExpression(regex) { |
| 7 | if (typeof regex !== 'string') { |
| 8 | return null; |
| 9 | } |
| 10 | const split = regex.split('/'); |
| 11 | if (split.length < 3 || split[0] !== '') { |
| 12 | return { $regex: regex }; |
| 13 | } |
| 14 | const pattern = split.slice(1, split.length - 1).join('/'); |
| 15 | const regexOpt = split[split.length - 1]; |
| 16 | return { $regex: new RegExp(pattern), $options: regexOpt }; |
| 17 | } |
| 18 | |
| 19 | module.exports = parseLikeExpression; |
no outgoing calls
no test coverage detected