UNPKG

ts-regex-builder

Version:

Maintainable regular expressions for TypeScript and JavaScript.

36 lines (35 loc) 1.12 kB
"use strict"; exports.buildPattern = buildPattern; exports.buildRegExp = buildRegExp; var _encoder = require("./encoder.js"); function buildRegExp(sequence, flags) { const pattern = (0, _encoder.encode)(sequence).pattern; ensureUnicodeFlagIfNeeded(pattern, flags); const flagsString = encodeFlags(flags ?? {}); return new RegExp(pattern, flagsString); } function buildPattern(sequence) { return (0, _encoder.encode)(sequence).pattern; } function encodeFlags(flags) { let result = ''; if (flags.global) result += 'g'; if (flags.ignoreCase) result += 'i'; if (flags.multiline) result += 'm'; if (flags.hasIndices) result += 'd'; if (flags.dotAll) result += 's'; if (flags.sticky) result += 'y'; if (flags.unicode) result += 'u'; return result; } const unicodeModePatterns = /(?<!\\)(?:\\u|\\[pP])\{.+?\}/; function ensureUnicodeFlagIfNeeded(pattern, flags) { if (flags?.unicode) { return; } const match = pattern.match(unicodeModePatterns); if (match) { throw new Error(`Pattern "${match?.[0]}" requires "unicode" flag to be set.`); } } //# sourceMappingURL=builders.js.map