Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added isGlob function #433

Merged
merged 4 commits into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions fs/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,35 @@ export interface GlobOptions {
export function glob(glob: string, options: GlobOptions = {}): RegExp {
return globrex(glob, options).regex;
}

/** Test whether the given string is a glob */
export function isGlob(str: string): boolean {
const chars: Record<string, string> = { "{": "}", "(": ")", "[": "]" };
const regex = /\\(.)|(^!|\*|[\].+)]\?|\[[^\\\]]+\]|\{[^\\}]+\}|\(\?[:!=][^\\)]+\)|\([^|]+\|[^\\)]+\))/;

if (str === "") {
return false;
}

let match: RegExpExecArray | null;

while ((match = regex.exec(str))) {
if (match[2]) return true;
let idx = match.index + match[0].length;

// if an open bracket/brace/paren is escaped,
// set the index to the next closing character
const open = match[1];
const close = open ? chars[open] : null;
if (open && close) {
const n = str.indexOf(close, idx);
if (n !== -1) {
idx = n + 1;
}
}

str = str.slice(idx);
}

return false;
}
119 changes: 117 additions & 2 deletions fs/glob_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { mkdir } = Deno;
type FileInfo = Deno.FileInfo;
import { test, runIfMain } from "../testing/mod.ts";
import { assertEquals } from "../testing/asserts.ts";
import { glob } from "./glob.ts";
import { assert, assertEquals } from "../testing/asserts.ts";
import { glob, isGlob } from "./glob.ts";
import { join } from "./path.ts";
import { testWalk } from "./walk_test.ts";
import { touch, walkArray } from "./walk_test.ts";
Expand Down Expand Up @@ -138,4 +138,119 @@ testWalk(
}
);

test({
name: "isGlob: pattern to test",
Copy link
Member

@bartlomieju bartlomieju May 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be some test cases testing that something is not glob, eg. assert(!isGlob("foo.js"));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases include both valid and invalid glob patterns,
I have used these test cases.

fn(): void {
// should be true if valid glob pattern
assert(isGlob("!foo.js"));
assert(isGlob("*.js"));
assert(isGlob("!*.js"));
assert(isGlob("!foo"));
assert(isGlob("!foo.js"));
assert(isGlob("**/abc.js"));
assert(isGlob("abc/*.js"));
assert(isGlob("@.(?:abc)"));
assert(isGlob("@.(?!abc)"));

// should be false if invalid glob pattern
assert(!isGlob(""));
assert(!isGlob("~/abc"));
assert(!isGlob("~/abc"));
assert(!isGlob("~/(abc)"));
assert(!isGlob("+~(abc)"));
assert(!isGlob("."));
assert(!isGlob("@.(abc)"));
assert(!isGlob("aa"));
assert(!isGlob("who?"));
assert(!isGlob("why!?"));
assert(!isGlob("where???"));
assert(!isGlob("abc!/def/!ghi.js"));
assert(!isGlob("abc.js"));
assert(!isGlob("abc/def/!ghi.js"));
assert(!isGlob("abc/def/ghi.js"));

// Should be true if path has regex capture group
assert(isGlob("abc/(?!foo).js"));
assert(isGlob("abc/(?:foo).js"));
assert(isGlob("abc/(?=foo).js"));
assert(isGlob("abc/(a|b).js"));
assert(isGlob("abc/(a|b|c).js"));
assert(isGlob("abc/(foo bar)/*.js"));

// Should be false if the path has parens but is not a valid capture group
assert(!isGlob("abc/(?foo).js"));
assert(!isGlob("abc/(a b c).js"));
assert(!isGlob("abc/(ab).js"));
assert(!isGlob("abc/(abc).js"));
assert(!isGlob("abc/(foo bar).js"));

// should be false if the capture group is imbalanced
assert(!isGlob("abc/(?ab.js"));
assert(!isGlob("abc/(ab.js"));
assert(!isGlob("abc/(a|b.js"));
assert(!isGlob("abc/(a|b|c.js"));

// should be true if the path has a regex character class
assert(isGlob("abc/[abc].js"));
assert(isGlob("abc/[^abc].js"));
assert(isGlob("abc/[1-3].js"));

// should be false if the character class is not balanced
assert(!isGlob("abc/[abc.js"));
assert(!isGlob("abc/[^abc.js"));
assert(!isGlob("abc/[1-3.js"));

// should be false if the character class is escaped
assert(!isGlob("abc/\\[abc].js"));
assert(!isGlob("abc/\\[^abc].js"));
assert(!isGlob("abc/\\[1-3].js"));

// should be true if the path has brace characters
assert(isGlob("abc/{a,b}.js"));
assert(isGlob("abc/{a..z}.js"));
assert(isGlob("abc/{a..z..2}.js"));

// should be false if (basic) braces are not balanced
assert(!isGlob("abc/\\{a,b}.js"));
assert(!isGlob("abc/\\{a..z}.js"));
assert(!isGlob("abc/\\{a..z..2}.js"));

// should be true if the path has regex characters
assert(isGlob("!&(abc)"));
assert(isGlob("!*.js"));
assert(isGlob("!foo"));
assert(isGlob("!foo.js"));
assert(isGlob("**/abc.js"));
assert(isGlob("*.js"));
assert(isGlob("*z(abc)"));
assert(isGlob("[1-10].js"));
assert(isGlob("[^abc].js"));
assert(isGlob("[a-j]*[^c]b/c"));
assert(isGlob("[abc].js"));
assert(isGlob("a/b/c/[a-z].js"));
assert(isGlob("abc/(aaa|bbb).js"));
assert(isGlob("abc/*.js"));
assert(isGlob("abc/{a,b}.js"));
assert(isGlob("abc/{a..z..2}.js"));
assert(isGlob("abc/{a..z}.js"));

assert(!isGlob("$(abc)"));
assert(!isGlob("&(abc)"));
assert(!isGlob("Who?.js"));
assert(!isGlob("? (abc)"));
assert(!isGlob("?.js"));
assert(!isGlob("abc/?.js"));

// should be false if regex characters are escaped
assert(!isGlob("\\?.js"));
assert(!isGlob("\\[1-10\\].js"));
assert(!isGlob("\\[^abc\\].js"));
assert(!isGlob("\\[a-j\\]\\*\\[^c\\]b/c"));
assert(!isGlob("\\[abc\\].js"));
assert(!isGlob("\\a/b/c/\\[a-z\\].js"));
assert(!isGlob("abc/\\(aaa|bbb).js"));
assert(!isGlob("abc/\\?.js"));
}
});

runIfMain(import.meta);