diff options
| author | Vincent LE GOFF <g_n_s@hotmail.fr> | 2019-03-02 20:56:19 +0100 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2019-03-02 14:56:19 -0500 |
| commit | c131b8f3b6664dfa69d80c2643b3261540b58fd7 (patch) | |
| tree | 897e89ca7d5a4e02cb87d24a027e1f8cacbcaf39 /fs/walk.ts | |
| parent | 2db147a0018b591c5039acf2b75706d960b1dec3 (diff) | |
Glob integration for the FS walker (denoland/deno_std#219)
Original: https://github.com/denoland/deno_std/commit/0c3ba838fa7e74a859d2a6dbfec3941a521c7988
Diffstat (limited to 'fs/walk.ts')
| -rw-r--r-- | fs/walk.ts | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/fs/walk.ts b/fs/walk.ts index 10393ae0b..f2a9b6c57 100644 --- a/fs/walk.ts +++ b/fs/walk.ts @@ -95,15 +95,26 @@ function include(f: FileInfo, options: WalkOptions): Boolean { if (options.exts && !options.exts.some(ext => f.path.endsWith(ext))) { return false; } - if (options.match && !options.match.some(pattern => pattern.test(f.path))) { + if (options.match && !patternTest(options.match, f.path)) { return false; } - if (options.skip && options.skip.some(pattern => pattern.test(f.path))) { + if (options.skip && patternTest(options.skip, f.path)) { return false; } return true; } +function patternTest(patterns: RegExp[], path: string) { + // Forced to reset last index on regex while iterating for have + // consistent results. + // See: https://stackoverflow.com/a/1520853 + return patterns.some(pattern => { + let r = pattern.test(path); + pattern.lastIndex = 0; + return r; + }); +} + async function resolve(f: FileInfo): Promise<FileInfo> { // This is the full path, unfortunately if we were to make it relative // it could resolve to a symlink and cause an infinite loop. |
