ts-regex-builder
Version:
Maintainable regular expressions for TypeScript and JavaScript.
1 lines • 1.46 kB
Source Map (JSON)
{"version":3,"file":"choice-of.mjs","names":["encode","choiceOf","alternatives","length","Error","encodedAlternatives","map","c","precedence","pattern","n","join"],"sources":["../../../src/constructs/choice-of.ts"],"sourcesContent":["import { encode } from '../encoder';\nimport type { EncodedRegex, RegexSequence } from '../types';\n\n/**\n * Creates a disjunction (choice of) which matches any of the alternatives.\n *\n * @param alternatives - Alternatives to choose from.\n * @returns Choice of alternatives.\n */\nexport function choiceOf(...alternatives: RegexSequence[]): EncodedRegex {\n if (alternatives.length === 0) {\n throw new Error('Expected at least one alternative');\n }\n\n const encodedAlternatives = alternatives.map((c) => encode(c));\n if (encodedAlternatives.length === 1) {\n return encodedAlternatives[0]!;\n }\n\n return {\n precedence: 'disjunction',\n pattern: encodedAlternatives.map((n) => n.pattern).join('|'),\n };\n}\n"],"mappings":"SAASA,MAAM;AASf,OAAO,SAASC,QAAQA,CAAC,GAAGC,YAA6B,EAAgB;EACvE,IAAIA,YAAY,CAACC,MAAM,KAAK,CAAC,EAAE;IAC7B,MAAM,IAAIC,KAAK,CAAC,mCAAmC,CAAC;EACtD;EAEA,MAAMC,mBAAmB,GAAGH,YAAY,CAACI,GAAG,CAAEC,CAAC,IAAKP,MAAM,CAACO,CAAC,CAAC,CAAC;EAC9D,IAAIF,mBAAmB,CAACF,MAAM,KAAK,CAAC,EAAE;IACpC,OAAOE,mBAAmB,CAAC,CAAC,CAAC;EAC/B;EAEA,OAAO;IACLG,UAAU,EAAE,aAAa;IACzBC,OAAO,EAAEJ,mBAAmB,CAACC,GAAG,CAAEI,CAAC,IAAKA,CAAC,CAACD,OAAO,CAAC,CAACE,IAAI,CAAC,GAAG;EAC7D,CAAC;AACH","ignoreList":[]}