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

Add ignore option to tiny-glob #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Options = {
absolute?: boolean;
filesOnly?: boolean;
flush?: boolean;
ignore?: string[];
};

type FilePath = string;
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function walk(output, prefix, lexer, opts, dirname='', level=0) {
const rgx = lexer.segments[level];
const dir = resolve(opts.cwd, prefix, dirname);
const files = await readdir(dir);
const { dot, filesOnly } = opts;
const { dot, filesOnly, ignore } = opts;

let i=0, len=files.length, file;
let fullpath, relpath, stats, isMatch;
Expand All @@ -21,6 +21,7 @@ async function walk(output, prefix, lexer, opts, dirname='', level=0) {
fullpath = join(dir, file=files[i]);
relpath = dirname ? join(dirname, file) : file;
if (!dot && isHidden.test(relpath)) continue;
if (ignore && ignore.some(pattern => new RegExp(pattern).test(relpath))) continue;
isMatch = lexer.regex.test(relpath);

if ((stats=CACHE[relpath]) === void 0) {
Expand Down Expand Up @@ -48,6 +49,7 @@ async function walk(output, prefix, lexer, opts, dirname='', level=0) {
* @param {Boolean} [options.absolute=false] Return absolute paths
* @param {Boolean} [options.filesOnly=false] Do not include folders if true
* @param {Boolean} [options.flush=false] Reset cache object
* @param {Array} [options.ignore=[]] Patterns to ignore
* @returns {Array} array containing matching files
*/
module.exports = async function (str, opts={}) {
Expand All @@ -56,6 +58,7 @@ module.exports = async function (str, opts={}) {
let glob = globalyzer(str);

opts.cwd = opts.cwd || '.';
opts.ignore = opts.ignore || [];

if (!glob.isGlob) {
try {
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ Default: `false`

Flush the internal cache object.

#### options.ignore

Type: `Array`<br>
Default: `[]`

Patterns to ignore.


## Windows

Expand Down
1 change: 1 addition & 0 deletions sync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Options = {
absolute?: boolean;
filesOnly?: boolean;
flush?: boolean;
ignore?: string[];
};

type FilePath = string;
Expand Down
5 changes: 4 additions & 1 deletion sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function walk(output, prefix, lexer, opts, dirname='', level=0) {
const rgx = lexer.segments[level];
const dir = resolve(opts.cwd, prefix, dirname);
const files = fs.readdirSync(dir);
const { dot, filesOnly } = opts;
const { dot, filesOnly, ignore } = opts;

let i=0, len=files.length, file;
let fullpath, relpath, stats, isMatch;
Expand All @@ -19,6 +19,7 @@ function walk(output, prefix, lexer, opts, dirname='', level=0) {
fullpath = join(dir, file=files[i]);
relpath = dirname ? join(dirname, file) : file;
if (!dot && isHidden.test(relpath)) continue;
if (ignore && ignore.some(pattern => new RegExp(pattern).test(relpath))) continue;
isMatch = lexer.regex.test(relpath);

if ((stats=CACHE[relpath]) === void 0) {
Expand Down Expand Up @@ -46,6 +47,7 @@ function walk(output, prefix, lexer, opts, dirname='', level=0) {
* @param {Boolean} [options.absolute=false] Return absolute paths
* @param {Boolean} [options.filesOnly=false] Do not include folders if true
* @param {Boolean} [options.flush=false] Reset cache object
* @param {Array} [options.ignore=[]] Patterns to ignore
* @returns {Array} array containing matching files
*/
module.exports = function (str, opts={}) {
Expand All @@ -54,6 +56,7 @@ module.exports = function (str, opts={}) {
let glob = globalyzer(str);

opts.cwd = opts.cwd || '.';
opts.ignore = opts.ignore || [];

if (!glob.isGlob) {
try {
Expand Down
21 changes: 21 additions & 0 deletions test/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,24 @@ test('glob: deep match with higher level siblings', async t => {
'test/fixtures/deep/b/c/d'
]);
});

test('glob: options.ignore', async t => {
t.plan(2);

await isMatch(t, 'test/fixtures/**/*.{js,txt}', { ignore: ['**/a.js', '**/a.txt'] }, [
'test/fixtures/b.js',
'test/fixtures/b.txt',
'test/fixtures/one/b.txt',
'test/fixtures/one/child/a.js',
'test/fixtures/one/child/a.txt',
'test/fixtures/two/a.txt'
]);

await isMatch(t, 'test/fixtures/**/*.{js,txt}', { ignore: ['**/one/**'] }, [
'test/fixtures/a.js',
'test/fixtures/a.txt',
'test/fixtures/b.js',
'test/fixtures/b.txt',
'test/fixtures/two/a.txt'
]);
});