ts-regex-builder
Version:
Maintainable regular expressions for TypeScript and JavaScript.
1 lines • 2.03 kB
Source Map (JSON)
{"version":3,"file":"capture.mjs","names":["encode","capture","sequence","options","name","precedence","pattern","ref"],"sources":["../../../src/constructs/capture.ts"],"sourcesContent":["import { encode } from '../encoder';\nimport type { EncodedRegex, RegexSequence } from '../types';\n\nexport type CaptureOptions = {\n /**\n * Name to be given to the capturing group.\n */\n name?: string;\n};\n\nexport interface Reference extends EncodedRegex {\n name: string;\n}\n\n/**\n * Creates a capturing group which allows the matched pattern to be available:\n * - in the match results (`String.match`, `String.matchAll`, or `RegExp.exec`)\n * - in the regex itself, through {@link ref}\n */\nexport function capture(sequence: RegexSequence, options?: CaptureOptions): EncodedRegex {\n const name = options?.name;\n if (name) {\n return {\n precedence: 'atom',\n pattern: `(?<${name}>${encode(sequence).pattern})`,\n };\n }\n\n return {\n precedence: 'atom',\n pattern: `(${encode(sequence).pattern})`,\n };\n}\n\n/**\n * Creates a reference, also known as backreference, which allows matching\n * again the exact text that a capturing group previously matched.\n *\n * In order to form a valid regex, the reference must use the same name as\n * a capturing group earlier in the expression.\n *\n * @param name - Name of the capturing group to reference.\n */\nexport function ref(name: string): Reference {\n return {\n precedence: 'atom',\n pattern: `\\\\k<${name}>`,\n name,\n };\n}\n"],"mappings":"SAASA,MAAM;AAmBf,OAAO,SAASC,OAAOA,CAACC,QAAuB,EAAEC,OAAwB,EAAgB;EACvF,MAAMC,IAAI,GAAGD,OAAO,EAAEC,IAAI;EAC1B,IAAIA,IAAI,EAAE;IACR,OAAO;MACLC,UAAU,EAAE,MAAM;MAClBC,OAAO,EAAG,MAAKF,IAAK,IAAGJ,MAAM,CAACE,QAAQ,CAAC,CAACI,OAAQ;IAClD,CAAC;EACH;EAEA,OAAO;IACLD,UAAU,EAAE,MAAM;IAClBC,OAAO,EAAG,IAAGN,MAAM,CAACE,QAAQ,CAAC,CAACI,OAAQ;EACxC,CAAC;AACH;AAWA,OAAO,SAASC,GAAGA,CAACH,IAAY,EAAa;EAC3C,OAAO;IACLC,UAAU,EAAE,MAAM;IAClBC,OAAO,EAAG,OAAMF,IAAK,GAAE;IACvBA;EACF,CAAC;AACH","ignoreList":[]}