UNPKG

@putout/operator-regexp

Version:

🐊Putout operator adds ability to check if provided regexp can be converted to string without losing it's sense

21 lines (15 loc) 575 B
import regexpTree from 'regexp-tree'; const notSimpleChar = ({type, kind}) => type !== 'Char' || kind !== 'simple'; export const isSimpleRegExp = (regexp) => { let containsMoreThenSimpleChars = false; const ast = regexpTree.parse(regexp); if (ast.body.type !== 'Alternative') return false; regexpTree.traverse(ast, { RegExp({node}) { const {expressions} = node.body; containsMoreThenSimpleChars = Boolean(expressions.find(notSimpleChar)); }, }); return !containsMoreThenSimpleChars; };