UNPKG

ts-regex-builder

Version:

Maintainable regular expressions for TypeScript and JavaScript.

1 lines 3.14 kB
{"version":3,"file":"unicode.mjs","names":["unicodeChar","codePoint","Number","isInteger","RangeError","escape","toString","padStart","precedence","pattern","chars","char","unicodeProperty","property","value"],"sources":["../../../src/constructs/unicode.ts"],"sourcesContent":["import type { CharacterEscape } from '../types';\n\n/**\n * Unicode character code point escape.\n *\n * Regex pattern:\n * - `\\uXXXX`: 4-digit hex escape for code points below 0x10000.\n * - `\\u{X}`: Unicode code point escape for code points above 0xFFFF.\n *\n * Note: for code points above 0xFFFF, the regex must be [unicode-aware](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode#unicode-aware_mode).\n *\n * @param codePoint The code point of the character to escape.\n * @returns A character class representing the unicode escape.\n */\nexport function unicodeChar(codePoint: number): CharacterEscape {\n if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 0x10ffff) {\n throw new RangeError(`Expected a valid unicode code point but received ${codePoint}`);\n }\n\n let escape =\n codePoint < 0x10000\n ? `\\\\u${codePoint.toString(16).padStart(4, '0')}` // 4-digit hex (works in all modes)\n : `\\\\u{${codePoint.toString(16)}}`; // 1-6 digit hex (requires unicode-aware mode)\n\n return {\n precedence: 'atom',\n pattern: escape,\n chars: [escape],\n };\n}\n\n/**\n * Alias for `unicodeChar`.\n */\nexport const char = unicodeChar;\n\n/**\n * Unicode property escape matching a set of characters specified by a Unicode property.\n *\n * Regex pattern: `\\p{Property}` or `\\p{Property=Value}`\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Unicode_character_class_escape\n *\n * Note: the regex must be [unicode-aware](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode#unicode-aware_mode).\n *\n * @param property Unicode property name.\n * @param value Unicode property value (optional).\n * @returns A character class representing the unicode property escape.\n */\nexport function unicodeProperty(property: string, value?: string): CharacterEscape {\n const escape = `\\\\p{${property}${value ? `=${value}` : ''}}`;\n\n return {\n precedence: 'atom',\n pattern: escape,\n chars: [escape],\n };\n}\n"],"mappings":"AAcA,OAAO,SAASA,WAAWA,CAACC,SAAiB,EAAmB;EAC9D,IAAI,CAACC,MAAM,CAACC,SAAS,CAACF,SAAS,CAAC,IAAIA,SAAS,GAAG,CAAC,IAAIA,SAAS,GAAG,QAAQ,EAAE;IACzE,MAAM,IAAIG,UAAU,CAAE,oDAAmDH,SAAU,EAAC,CAAC;EACvF;EAEA,IAAII,MAAM,GACRJ,SAAS,GAAG,OAAO,GACd,MAAKA,SAAS,CAACK,QAAQ,CAAC,EAAE,CAAC,CAACC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAE,EAAC,GAC9C,OAAMN,SAAS,CAACK,QAAQ,CAAC,EAAE,CAAE,GAAE;EAEtC,OAAO;IACLE,UAAU,EAAE,MAAM;IAClBC,OAAO,EAAEJ,MAAM;IACfK,KAAK,EAAE,CAACL,MAAM;EAChB,CAAC;AACH;AAKA,OAAO,MAAMM,IAAI,GAAGX,WAAW;AAc/B,OAAO,SAASY,eAAeA,CAACC,QAAgB,EAAEC,KAAc,EAAmB;EACjF,MAAMT,MAAM,GAAI,OAAMQ,QAAS,GAAEC,KAAK,GAAI,IAAGA,KAAM,EAAC,GAAG,EAAG,GAAE;EAE5D,OAAO;IACLN,UAAU,EAAE,MAAM;IAClBC,OAAO,EAAEJ,MAAM;IACfK,KAAK,EAAE,CAACL,MAAM;EAChB,CAAC;AACH","ignoreList":[]}