UNPKG

ts-regex-builder

Version:

Maintainable regular expressions for TypeScript and JavaScript.

1 lines 1.87 kB
{"version":3,"file":"repeat.mjs","names":["encodeAtomic","ensureElements","repeat","sequence","options","elements","precedence","pattern","min","max","greedy"],"sources":["../../../src/constructs/repeat.ts"],"sourcesContent":["import { encodeAtomic } from '../encoder';\nimport type { EncodedRegex, RegexSequence } from '../types';\nimport { ensureElements } from '../utils';\n\n/**\n * Options for the `repeat` function.\n *\n * @param min - Minimum number of times to match.\n * @param max - Maximum number of times to match (default: unlimited).\n * @param greedy - Whether to use greedy quantifiers (default: true).\n */\nexport type RepeatOptions = number | { min: number; max?: number; greedy?: boolean };\n\n/**\n * Creates a quantifier which matches the given sequence a specific number of times.\n *\n * @param sequence - Sequence to match.\n * @param options - Quantifier options.\n */\nexport function repeat(sequence: RegexSequence, options: RepeatOptions): EncodedRegex {\n const elements = ensureElements(sequence);\n\n if (typeof options === 'number') {\n return {\n precedence: 'sequence',\n pattern: `${encodeAtomic(elements)}{${options}}`,\n };\n }\n\n return {\n precedence: 'sequence',\n pattern: `${encodeAtomic(elements)}{${options.min},${options?.max ?? ''}}${\n options.greedy === false ? '?' : ''\n }`,\n };\n}\n"],"mappings":"SAASA,YAAY;AAAA,SAEZC,cAAc;AAiBvB,OAAO,SAASC,MAAMA,CAACC,QAAuB,EAAEC,OAAsB,EAAgB;EACpF,MAAMC,QAAQ,GAAGJ,cAAc,CAACE,QAAQ,CAAC;EAEzC,IAAI,OAAOC,OAAO,KAAK,QAAQ,EAAE;IAC/B,OAAO;MACLE,UAAU,EAAE,UAAU;MACtBC,OAAO,EAAG,GAAEP,YAAY,CAACK,QAAQ,CAAE,IAAGD,OAAQ;IAChD,CAAC;EACH;EAEA,OAAO;IACLE,UAAU,EAAE,UAAU;IACtBC,OAAO,EAAG,GAAEP,YAAY,CAACK,QAAQ,CAAE,IAAGD,OAAO,CAACI,GAAI,IAAGJ,OAAO,EAAEK,GAAG,IAAI,EAAG,IACtEL,OAAO,CAACM,MAAM,KAAK,KAAK,GAAG,GAAG,GAAG,EAClC;EACH,CAAC;AACH","ignoreList":[]}