Skip to content

Commit

Permalink
Remove glob support
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Jan 18, 2022
1 parent e28f928 commit 703dee9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 85 deletions.
23 changes: 4 additions & 19 deletions index.js
Expand Up @@ -4,7 +4,6 @@ const fs = require('fs');
const { Readable } = require('stream');
const sysPath = require('path');
const { promisify } = require('util');
const picomatch = require('picomatch');

const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
Expand All @@ -20,7 +19,6 @@ const realpath = promisify(fs.realpath);
* @property {String} basename
*/

const BANG = '!';
const RECURSIVE_ERROR_CODE = 'READDIRP_RECURSIVE_ERROR';
const NORMAL_FLOW_ERRORS = new Set(['ENOENT', 'EPERM', 'EACCES', 'ELOOP', RECURSIVE_ERROR_CODE]);
const FILE_TYPE = 'files';
Expand All @@ -38,30 +36,17 @@ const normalizeFilter = filter => {
if (typeof filter === 'function') return filter;

if (typeof filter === 'string') {
const glob = picomatch(filter.trim());
return entry => glob(entry.basename);
const fl = filter.trim();
return entry => (entry.basename) === fl;
}

if (Array.isArray(filter)) {
const positive = [];
const negative = [];
for (const item of filter) {
const trimmed = item.trim();
if (trimmed.charAt(0) === BANG) {
negative.push(picomatch(trimmed.slice(1)));
} else {
positive.push(picomatch(trimmed));
}
}

if (negative.length > 0) {
if (positive.length > 0) {
return entry =>
positive.some(f => f(entry.basename)) && !negative.some(f => f(entry.basename));
}
return entry => !negative.some(f => f(entry.basename));
positive.push(trimmed);
}
return entry => positive.some(f => f(entry.basename));
return entry => positive.some(f => entry.basename === f);
}
};

Expand Down
20 changes: 8 additions & 12 deletions package.json
Expand Up @@ -18,7 +18,7 @@
],
"main": "index.js",
"engines": {
"node": ">=8.10.0"
"node": ">= 14.16.0"
},
"files": [
"index.js",
Expand All @@ -41,19 +41,15 @@
"lint": "eslint --report-unused-disable-directives --ignore-path .gitignore .",
"test": "npm run lint && nyc npm run mocha"
},
"dependencies": {
"picomatch": "^2.2.1"
},
"devDependencies": {
"@types/node": "^14",
"chai": "^4.2",
"@types/node": "^16.11.7",
"chai": "4.3.4",
"chai-subset": "^1.6",
"dtslint": "^3.3.0",
"eslint": "^7.0.0",
"mocha": "^7.1.1",
"nyc": "^15.0.0",
"rimraf": "^3.0.0",
"typescript": "^4.0.3"
"eslint": "^8.7.0",
"mocha": "^9.1.4",
"nyc": "15.1.0",
"rimraf": "3.0.2",
"typescript": "~4.5.3"
},
"nyc": {
"reporter": [
Expand Down
55 changes: 1 addition & 54 deletions test.js
Expand Up @@ -238,62 +238,9 @@ describe('filtering', () => {
beforeEach(async () => {
await touch(['a.js', 'b.txt', 'c.js', 'd.js', 'e.rb']);
});
it('glob', async () => {
const expect1 = ['a.js', 'c.js', 'd.js'];
const res = await read({fileFilter: '*.js'});
res.should.have.lengthOf(expect1.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect1[index], currPath))
);

const res2 = await read({fileFilter: ['*.js']});
res2.should.have.lengthOf(expect1.length);
res2.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect1[index], currPath))
);

const expect2 = ['b.txt'];
const res3 = await read({fileFilter: ['*.txt']});
res3.should.have.lengthOf(expect2.length);
res3.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect2[index], currPath))
);
});
it('leading and trailing spaces', async () => {
const expect = ['a.js', 'c.js', 'd.js', 'e.rb'];
const res = await read({fileFilter: [' *.js', '*.rb ']});
res.should.have.lengthOf(expect.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect[index], currPath))
);
});
it('multiple glob', async () => {
const expect = ['a.js', 'b.txt', 'c.js', 'd.js'];
const res = await read({fileFilter: ['*.js', '*.txt']});
res.should.have.lengthOf(expect.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect[index], currPath))
);
});
it('negated glob', async () => {
const expect = ['a.js', 'b.txt', 'c.js', 'e.rb'];
const res = await read({fileFilter: ['!d.js']});
res.should.have.lengthOf(expect.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect[index], currPath))
);
});
it('glob & negated glob', async () => {
const expect = ['a.js', 'c.js'];
const res = await read({fileFilter: ['*.js', '!d.js']});
res.should.have.lengthOf(expect.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect[index], currPath))
);
});
it('two negated glob', async () => {
const expect = ['b.txt'];
const res = await read({fileFilter: ['!*.js', '!*.rb']});
const res = await read({fileFilter: (a => a.basename.endsWith('.js') || a.basename.endsWith('.rb'))});
res.should.have.lengthOf(expect.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect[index], currPath))
Expand Down

0 comments on commit 703dee9

Please sign in to comment.