diff options
author | Nayeem Rahman <muhammed.9939@gmail.com> | 2019-09-28 14:33:17 +0100 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-09-28 09:33:17 -0400 |
commit | a472b6732dd37636b7b31128f53d3e6bcf531a73 (patch) | |
tree | 11fa3fb56917582c0c88c871e71ff212b39eb841 /fs/globrex.ts | |
parent | af18093498c3fca103bd47305e447ddeda40d9a2 (diff) |
Test runner v2 (denoland/deno_std#604)
Original: https://github.com/denoland/deno_std/commit/17a214bbd5b3a058a8126e9f7210992b1b52ba11
Diffstat (limited to 'fs/globrex.ts')
-rw-r--r-- | fs/globrex.ts | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/fs/globrex.ts b/fs/globrex.ts index 32a129a67..e382dc82e 100644 --- a/fs/globrex.ts +++ b/fs/globrex.ts @@ -5,17 +5,18 @@ import { GlobOptions } from "./glob.ts"; const isWin = Deno.build.os === "win"; -const SEP = isWin ? `\\\\+` : `\\/`; +const SEP = isWin ? `(\\\\+|\\/)` : `\\/`; const SEP_ESC = isWin ? `\\\\` : `/`; -const GLOBSTAR = `((?:[^/]*(?:/|$))*)`; -const WILDCARD = `([^/]*)`; -const GLOBSTAR_SEGMENT = `((?:[^${SEP_ESC}]*(?:${SEP_ESC}|$))*)`; -const WILDCARD_SEGMENT = `([^${SEP_ESC}]*)`; +const SEP_RAW = isWin ? `\\` : `/`; +const GLOBSTAR = `((?:[^${SEP_ESC}/]*(?:${SEP_ESC}|\/|$))*)`; +const WILDCARD = `([^${SEP_ESC}/]*)`; +const GLOBSTAR_SEGMENT = `((?:[^${SEP_ESC}/]*(?:${SEP_ESC}|\/|$))*)`; +const WILDCARD_SEGMENT = `([^${SEP_ESC}/]*)`; export interface GlobrexResult { regex: RegExp; path?: { - regex: string | RegExp; + regex: RegExp; segments: RegExp[]; globstar?: RegExp; }; @@ -44,11 +45,8 @@ export function globrex( ): GlobrexResult { let regex = ""; let segment = ""; - let path: { - regex: string | RegExp; - segments: RegExp[]; - globstar?: RegExp; - } = { regex: "", segments: [] }; + let pathRegexStr = ""; + const pathSegments = []; // If we are doing extended matching, this boolean is true when we are inside // a group (eg {*.html,*.js}), and false otherwise. @@ -72,13 +70,13 @@ export function globrex( const { split, last, only } = options; if (only !== "path") regex += str; if (filepath && only !== "regex") { - path.regex += str === "\\/" ? SEP : str; + pathRegexStr += str.match(new RegExp(`^${SEP}$`)) ? SEP : str; if (split) { if (last) segment += str; if (segment !== "") { // change it 'includes' if (!flags.includes("g")) segment = `^${segment}$`; - path.segments.push(new RegExp(segment, flags)); + pathSegments.push(new RegExp(segment, flags)); } segment = ""; } else { @@ -267,9 +265,9 @@ export function globrex( let isGlobstar = starCount > 1 && // multiple "*"'s // from the start of the segment - (prevChar === "/" || prevChar === undefined) && + [SEP_RAW, "/", undefined].includes(prevChar) && // to the end of the segment - (nextChar === "/" || nextChar === undefined); + [SEP_RAW, "/", undefined].includes(nextChar); if (isGlobstar) { // it's a globstar, so match zero or more path segments add(GLOBSTAR, { only: "regex" }); @@ -292,20 +290,22 @@ export function globrex( if (!flags.includes("g")) { regex = `^${regex}$`; segment = `^${segment}$`; - if (filepath) path.regex = `^${path.regex}$`; + if (filepath) pathRegexStr = `^${pathRegexStr}$`; } const result: GlobrexResult = { regex: new RegExp(regex, flags) }; // Push the last segment if (filepath) { - path.segments.push(new RegExp(segment, flags)); - path.regex = new RegExp(path.regex.toString(), flags); - path.globstar = new RegExp( - !flags.includes("g") ? `^${GLOBSTAR_SEGMENT}$` : GLOBSTAR_SEGMENT, - flags - ); - result.path = path; + pathSegments.push(new RegExp(segment, flags)); + result.path = { + regex: new RegExp(pathRegexStr, flags), + segments: pathSegments, + globstar: new RegExp( + !flags.includes("g") ? `^${GLOBSTAR_SEGMENT}$` : GLOBSTAR_SEGMENT, + flags + ) + }; } return result; |