Skip to content

Commit 5d7dfde

Browse files
jonschlinkertphated
authored andcommitted
New: Add support for a filter function & update project structure (closes #1)
1 parent 444e0b1 commit 5d7dfde

File tree

5 files changed

+85
-30
lines changed

5 files changed

+85
-30
lines changed

.npmignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
44
[![NPM](https://nodei.co/npm/empty-dir.png)](https://nodei.co/npm/empty-dir/)
55

6+
Note that directories with `.DS_Store` on mac are considered empty.
7+
68
## Example
79
```js
810
const emptyDir = require('empty-dir');
@@ -17,7 +19,31 @@ emptyDir('./', function (err, result) {
1719

1820
var result = emptyDir.sync('./test/empty');
1921
console.log('Directory is empty:', result);
22+
```
23+
24+
**Filter function**
25+
26+
Both async and sync take a filter function as the second argument.
27+
28+
_(This gives you the ability to eliminate files like `.DS_Store` on mac, or `Thumbs.db` on windows from causing the result to be "not empty" (`.DS_Store` is already filtered by default).)_
29+
30+
```js
31+
const emptyDir = require('empty-dir');
32+
33+
function filter(filepath) {
34+
return !/Thumbs\.db$/i.test(filepath);
35+
}
2036

37+
emptyDir('./', filter, function (err, result) {
38+
if (err) {
39+
console.error(err);
40+
} else {
41+
console.log('Directory is empty:', result);
42+
}
43+
});
44+
45+
var result = emptyDir.sync('./test/empty', filter);
46+
console.log('Directory is empty:', result);
2147
```
2248

2349
## Release History

index.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,47 @@
11
const fs = require('fs');
2+
const tryOpen = require('try-open');
23

3-
function isEmpty (path, callback) {
4-
fs.readdir(path, function (err, files) {
5-
if (err === null) {
6-
callback(null, !!!files.length)
7-
} else {
4+
function isEmpty(path, fn, callback) {
5+
if (arguments.length === 2) {
6+
callback = fn;
7+
fn = null;
8+
}
9+
10+
if (!exists(path)) {
11+
callback(null, false);
12+
return;
13+
}
14+
15+
fs.readdir(path, function(err, files) {
16+
if (err) {
817
callback(err);
18+
return;
19+
}
20+
21+
if (typeof fn === 'function') {
22+
files = files.filter(fn);
923
}
24+
25+
callback(null, files.length === 0);
1026
});
1127
};
1228

13-
isEmpty.sync = function (path) {
14-
var files = fs.readdirSync(path);
15-
return !!!files.length;
29+
isEmpty.sync = function(path, fn) {
30+
if (!exists(path)) {
31+
return false;
32+
}
33+
try {
34+
var files = fs.readdirSync(path);
35+
if (typeof fn === 'function') {
36+
files = files.filter(fn);
37+
}
38+
return files.length === 0;
39+
} catch (err) {};
40+
return false;
1641
};
1742

43+
function exists(path) {
44+
return path && typeof tryOpen(path, 'r') === 'number';
45+
}
46+
1847
module.exports = isEmpty;

package.json

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,23 @@
22
"name": "empty-dir",
33
"description": "Check if a directory is empty.",
44
"version": "0.1.0",
5-
"homepage": "https://github.com/tkellen/node-empty-dir",
6-
"author": {
7-
"name": "Tyler Kellen",
8-
"url": "http://goingslowly.com/"
9-
},
10-
"repository": {
11-
"type": "git",
12-
"url": "git://github.com/tkellen/node-empty-dir.git"
13-
},
5+
"homepage": "https://github.com/tkellen/js-empty-dir",
6+
"author": "Tyler Kellen <http://goingslowly.com/>",
7+
"repository": "tkellen/js-empty-dir",
148
"bugs": {
15-
"url": "https://github.com/tkellen/node-empty-dir/issues"
9+
"url": "https://github.com/tkellen/js-empty-dir/issues"
1610
},
17-
"licenses": [
18-
{
19-
"type": "MIT",
20-
"url": "https://github.com/tkellen/node-empty-dir/blob/master/LICENSE"
21-
}
22-
],
11+
"license": "MIT",
2312
"main": "index.js",
13+
"files": ["index.js"],
2414
"engines": {
2515
"node": ">= 0.8.0"
2616
},
2717
"scripts": {
28-
"test": "mocha -R spec test"
18+
"test": "mocha"
19+
},
20+
"dependencies": {
21+
"try-open": "^0.1.0"
2922
},
3023
"devDependencies": {
3124
"chai": "~1.9.1",

test/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,24 @@ try {
99
describe('emptyDir', function () {
1010
it('should call back with true if a directory is empty', function (done) {
1111
expect(emptyDir.sync('./')).to.be.false;
12-
emptyDir('./', function (err, result) {
13-
expect(result).to.be.false;
12+
emptyDir('./', function (err, empty) {
13+
expect(empty).to.be.false;
14+
done();
15+
});
16+
});
17+
18+
it('should be false if a directory does not exist', function (done) {
19+
expect(emptyDir.sync('./foo/bar/baz')).to.be.false;
20+
emptyDir('./foo/bar/baz', function (err, empty) {
21+
expect(empty).to.be.false;
1422
done();
1523
});
1624
});
1725

1826
it('should call back with false if a directory is not empty', function (done) {
1927
expect(emptyDir.sync('./test/empty')).to.be.true;
20-
emptyDir('./test/empty', function (err, result) {
21-
expect(result).to.be.true;
28+
emptyDir('./test/empty', function (err, empty) {
29+
expect(empty).to.be.true;
2230
done();
2331
});
2432
});

0 commit comments

Comments
 (0)