summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent LE GOFF <g_n_s@hotmail.fr>2019-03-04 19:06:05 +0100
committerRyan Dahl <ry@tinyclouds.org>2019-03-04 13:06:05 -0500
commit2bbde0c226d220307e46cc74e414320ac74aeddd (patch)
treefe46f711f56aad0779b14e717f3d9b32136468eb
parent87d044ec2405c2ff5bd18e9b7876156283a3d5ff (diff)
Refactoring + Enhance UTs + Enhance doc (denoland/deno_std#230)
Original: https://github.com/denoland/deno_std/commit/e2fd507cfd28fa6f4900668d4a80b78a5e97f01b
-rw-r--r--fs/glob.ts44
-rw-r--r--fs/glob_test.ts28
-rw-r--r--fs/globrex.ts9
3 files changed, 70 insertions, 11 deletions
diff --git a/fs/glob.ts b/fs/glob.ts
index 1031bd75d..8c64c45b3 100644
--- a/fs/glob.ts
+++ b/fs/glob.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;
}
diff --git a/fs/glob_test.ts b/fs/glob_test.ts
index 50e6abef8..bd5e2543c 100644
--- a/fs/glob_test.ts
+++ b/fs/glob_test.ts
@@ -106,7 +106,6 @@ testWalk(
match: [
glob(join("a", "**", "*.ts"), {
flags: "g",
- extended: true,
globstar: true
})
]
@@ -118,13 +117,38 @@ 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");
await touch(d + "/x.js");
await touch(d + "/b.js");
},
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);
diff --git a/fs/globrex.ts b/fs/globrex.ts
index 06a6b79bf..9905cbb73 100644
--- a/fs/globrex.ts
+++ b/fs/globrex.ts
@@ -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