{"version":3,"file":"glob-to-regex.mjs","names":[],"sources":["../src/glob-to-regex.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n                       ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website:                  https://stormsoftware.com\n Repository:               https://github.com/storm-software/stryke\n Documentation:            https://docs.stormsoftware.com/projects/stryke\n Contact:                  https://stormsoftware.com/contact\n\n SPDX-License-Identifier:  Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isSetString } from \"@stryke/type-checks/is-set-string\";\n\n/**\n * Options for the `globToRegex` function.\n */\nexport interface GlobToRegexOptions {\n  /**\n   * Enables globstar support (`**`).\n   *\n   * @defaultValue true\n   */\n  globstar?: boolean;\n\n  /**\n   * Flags to use for the generated regular expression.\n   */\n  flags?: string;\n}\n\n/**\n * Converts a glob pattern to a regular expression.\n *\n * @see https://mergify.com/blog/origin-and-evolution-of-the-globstar\n * @see https://en.wikipedia.org/wiki/Glob_(programming)\n *\n * @remarks\n * This function converts a glob pattern (like `*.{js,ts}` or `**\\/src/**`) into a regular expression\n *\n * @example\n * ```ts\n * import { globToRegex } from \"@stryke/path/glob-to-regex\";\n *\n * const test1 = globToRegex(\"*.{js,ts}\");\n * console.log(test1); // Output: /^([^/]*)\\.(js|ts)$/\n *\n * const test2 = globToRegex(\"**\\/src/**\");\n * console.log(test2); // Output: /^((?:[^/]*(?:\\/|$))*)?\\/src\\/((?:[^/]*(?:\\/|$))*)?$/\n * ```\n *\n * @param glob - The glob pattern to convert.\n * @param options - The options for the conversion.\n * @returns The converted regular expression.\n */\nexport function globToRegex(\n  glob: string,\n  options: GlobToRegexOptions = {}\n): RegExp {\n  if (!isSetString(glob)) {\n    throw new TypeError(\"A string was not provided as a glob pattern.\");\n  }\n\n  let regex = \"\";\n  let inGroup = false;\n  for (let i = 0; i < glob.length; i++) {\n    let count = 1;\n    switch (glob[i]) {\n      case \"/\":\n      case \"$\":\n      case \"^\":\n      case \"+\":\n      case \".\":\n      case \"(\":\n      case \")\":\n      case \"=\":\n      case \"!\":\n      case \"|\":\n        regex += `\\\\${glob[i]}`;\n        break;\n\n      case \"?\":\n        regex += \".\";\n        break;\n\n      case \"[\":\n      case \"]\":\n        regex += glob[i];\n        break;\n\n      case \"{\":\n        inGroup = true;\n        regex += \"(\";\n        break;\n\n      case \"}\":\n        inGroup = false;\n        regex += \")\";\n        break;\n\n      case \",\":\n        if (inGroup) {\n          regex += \"|\";\n        } else {\n          regex += `\\\\${glob[i]}`;\n        }\n\n        break;\n\n      case \"*\":\n        while (glob[i + 1] === \"*\") {\n          count++;\n          i++;\n        }\n\n        if (options.globstar === false) {\n          regex += \".*\";\n        } else if (\n          count > 1 &&\n          i - count >= 0 &&\n          (glob[i - count] === \"/\" || glob[i - count] === undefined) &&\n          i + 1 < glob.length &&\n          (glob[i + 1] === \"/\" || glob[i + 1] === undefined)\n        ) {\n          regex += \"((?:[^\\/]*(?:\\/|$))*)\";\n          i++;\n        } else {\n          regex += \"([^\\/]*)\";\n        }\n\n        break;\n\n      case undefined:\n      default:\n        regex += glob[i];\n    }\n  }\n\n  const flags = isSetString(options.flags) ? options.flags : \"\";\n  if (!flags || !~flags.indexOf(\"g\")) {\n    regex = `^${regex}$`;\n  }\n\n  return new RegExp(regex, flags);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA6DA,SAAgB,YACd,MACA,UAA8B,EAAE,EACxB;AACR,KAAI,CAAC,YAAY,KAAK,CACpB,OAAM,IAAI,UAAU,+CAA+C;CAGrE,IAAI,QAAQ;CACZ,IAAI,UAAU;AACd,MAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;EACpC,IAAI,QAAQ;AACZ,UAAQ,KAAK,IAAb;GACE,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK;AACH,aAAS,KAAK,KAAK;AACnB;GAEF,KAAK;AACH,aAAS;AACT;GAEF,KAAK;GACL,KAAK;AACH,aAAS,KAAK;AACd;GAEF,KAAK;AACH,cAAU;AACV,aAAS;AACT;GAEF,KAAK;AACH,cAAU;AACV,aAAS;AACT;GAEF,KAAK;AACH,QAAI,QACF,UAAS;QAET,UAAS,KAAK,KAAK;AAGrB;GAEF,KAAK;AACH,WAAO,KAAK,IAAI,OAAO,KAAK;AAC1B;AACA;;AAGF,QAAI,QAAQ,aAAa,MACvB,UAAS;aAET,QAAQ,KACR,IAAI,SAAS,MACZ,KAAK,IAAI,WAAW,OAAO,KAAK,IAAI,WAAW,WAChD,IAAI,IAAI,KAAK,WACZ,KAAK,IAAI,OAAO,OAAO,KAAK,IAAI,OAAO,SACxC;AACA,cAAS;AACT;UAEA,UAAS;AAGX;GAEF,KAAK;GACL,QACE,UAAS,KAAK;;;CAIpB,MAAM,QAAQ,YAAY,QAAQ,MAAM,GAAG,QAAQ,QAAQ;AAC3D,KAAI,CAAC,SAAS,CAAC,CAAC,MAAM,QAAQ,IAAI,CAChC,SAAQ,IAAI,MAAM;AAGpB,QAAO,IAAI,OAAO,OAAO,MAAM"}