mirror of
https://github.com/denoland/deno.git
synced 2025-09-28 21:24:48 +00:00
Refactoring + Enhance UTs + Enhance doc (denoland/deno_std#230)
Original: e2fd507cfd
This commit is contained in:
parent
87d044ec24
commit
2bbde0c226
3 changed files with 70 additions and 11 deletions
44
fs/glob.ts
44
fs/glob.ts
|
@ -1,6 +1,48 @@
|
||||||
import { FileInfo } from "deno";
|
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 {
|
export function glob(glob: string, options: GlobOptions = {}): RegExp {
|
||||||
return globrex(glob, options).regex;
|
return globrex(glob, options).regex;
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,7 +106,6 @@ testWalk(
|
||||||
match: [
|
match: [
|
||||||
glob(join("a", "**", "*.ts"), {
|
glob(join("a", "**", "*.ts"), {
|
||||||
flags: "g",
|
flags: "g",
|
||||||
extended: true,
|
|
||||||
globstar: 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(
|
testWalk(
|
||||||
async (d: string) => {
|
async (d: string) => {
|
||||||
await touch(d + "/x.ts");
|
await touch(d + "/x.ts");
|
||||||
|
@ -124,7 +148,7 @@ testWalk(
|
||||||
},
|
},
|
||||||
async function globInWalkWildcardExtension() {
|
async function globInWalkWildcardExtension() {
|
||||||
const arr = await walkArray(".", {
|
const arr = await walkArray(".", {
|
||||||
match: [glob("x.*", { flags: "g", extended: true, globstar: true })]
|
match: [glob("x.*", { flags: "g", globstar: true })]
|
||||||
});
|
});
|
||||||
console.log(arr);
|
console.log(arr);
|
||||||
assert.equal(arr.length, 2);
|
assert.equal(arr.length, 2);
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
// Copyright (c) 2018 Terkel Gjervig Nielsen
|
// Copyright (c) 2018 Terkel Gjervig Nielsen
|
||||||
|
|
||||||
import * as deno from "deno";
|
import * as deno from "deno";
|
||||||
|
import { GlobOptions } from "./glob.ts";
|
||||||
|
|
||||||
const isWin = deno.platform.os === "win";
|
const isWin = deno.platform.os === "win";
|
||||||
const SEP = isWin ? `\\\\+` : `\\/`;
|
const SEP = isWin ? `\\\\+` : `\\/`;
|
||||||
|
@ -12,14 +13,6 @@ const WILDCARD = `([^/]*)`;
|
||||||
const GLOBSTAR_SEGMENT = `((?:[^${SEP_ESC}]*(?:${SEP_ESC}|$))*)`;
|
const GLOBSTAR_SEGMENT = `((?:[^${SEP_ESC}]*(?:${SEP_ESC}|$))*)`;
|
||||||
const WILDCARD_SEGMENT = `([^${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
|
* Convert any glob pattern to a JavaScript Regexp object
|
||||||
* @param {String} glob Glob pattern to convert
|
* @param {String} glob Glob pattern to convert
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue