Skip to content
Permalink
Browse files

Refactoring + Enhance UTs + Enhance doc (denoland#230)

  • Loading branch information...
zekth authored and ry committed Mar 4, 2019
1 parent facc8ce commit e2fd507cfd28fa6f4900668d4a80b78a5e97f01b
Showing with 70 additions and 11 deletions.
  1. +43 −1 fs/glob.ts
  2. +26 −2 fs/glob_test.ts
  3. +1 −8 fs/globrex.ts
@@ -1,6 +1,48 @@
import { FileInfo } from "deno";
import { globrex, GlobOptions } from "./globrex.ts";
import { globrex } from "./globrex.ts";

export interface GlobOptions {
// Allow ExtGlob features
extended?: boolean;
// When globstar is true, '/foo/**' is equivelant
// to '/foo/*' when globstar is false.
// Having globstar set to true is the same usage as
// using wildcards in bash
globstar?: boolean;
// be laissez faire about mutiple slashes
strict?: boolean;
// Parse as filepath for extra path related features
filepath?: boolean;
// Flag to use in the generated RegExp
flags?: string;
}

/**
* Generate a regex based on glob pattern and options
* This was meant to be using the the `fs.walk` function
* but can be used anywhere else.
* @param glob - Glob pattern to be used
* @param options - Specific options for the glob pattern
* @returns A RegExp for the glob pattern
* @example
* Looking for all the `ts` files
* ```typescript
* walkSync(".", {
* match: [glob("*.ts")]
* })
* ```
* @example
* Looking for all the `.json` files in any subfolder
* of the `a` folder
* ```typescript
* walkSync(".", {
* match: [glob(join("a", "**", "*.json"),flags: "g",
* extended: true,
* globstar: true
* })]
* })
* ```
*/
export function glob(glob: string, options: GlobOptions = {}): RegExp {
return globrex(glob, options).regex;
}
@@ -106,7 +106,6 @@ testWalk(
match: [
glob(join("a", "**", "*.ts"), {
flags: "g",
extended: true,
globstar: true
})
]
@@ -116,6 +115,31 @@ testWalk(
}
);

testWalk(
async (d: string) => {
await mkdir(d + "/a");
await mkdir(d + "/a/unicorn");
await mkdir(d + "/a/deno");
await mkdir(d + "/a/raptor");
await touch(d + "/a/raptor/x.ts");
await touch(d + "/a/deno/x.ts");
await touch(d + "/a/unicorn/x.ts");
},
async function globInWalkFolderExtended() {
const arr = await walkArray(".", {
match: [
glob(join("a", "+(raptor|deno)", "*.ts"), {
flags: "g",
extended: true
})
]
});
assert.equal(arr.length, 2);
assert.equal(arr[0], "./a/deno/x.ts");
assert.equal(arr[1], "./a/raptor/x.ts");
}
);

testWalk(
async (d: string) => {
await touch(d + "/x.ts");
@@ -124,7 +148,7 @@ testWalk(
},
async function globInWalkWildcardExtension() {
const arr = await walkArray(".", {
match: [glob("x.*", { flags: "g", extended: true, globstar: true })]
match: [glob("x.*", { flags: "g", globstar: true })]
});
console.log(arr);
assert.equal(arr.length, 2);
@@ -3,6 +3,7 @@
// Copyright (c) 2018 Terkel Gjervig Nielsen

import * as deno from "deno";
import { GlobOptions } from "./glob.ts";

const isWin = deno.platform.os === "win";
const SEP = isWin ? `\\\\+` : `\\/`;
@@ -12,14 +13,6 @@ const WILDCARD = `([^/]*)`;
const GLOBSTAR_SEGMENT = `((?:[^${SEP_ESC}]*(?:${SEP_ESC}|$))*)`;
const WILDCARD_SEGMENT = `([^${SEP_ESC}]*)`;

export interface GlobOptions {
extended?: boolean;
globstar?: boolean;
strict?: boolean;
filepath?: boolean;
flags?: string;
}

/**
* Convert any glob pattern to a JavaScript Regexp object
* @param {String} glob Glob pattern to convert

0 comments on commit e2fd507

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.