Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Clean code

  • Loading branch information...
commit 98c47dee61d5e246cc75b4e4f23e2fad8484e471 1 parent 0dcebf3
@pvorb authored
View
1  .npmignore
@@ -0,0 +1 @@
+test/
View
37 README.mkd
@@ -1,28 +1,35 @@
-_dive_ is a tiny module for node that is able to recursively walk ("dive") a
-directory tree. You may use it as follows:
+**dive** is a tiny module for node that is able to recursively walk (_„dive“_) a
+directory tree.
- var dive = require("dive");
+## Example
- dive(process.cwd(), function(err, file) {
- if (err)
- throw err;
+```javascript
+var dive = require("dive");
- console.log(file);
- });
+dive(process.cwd(), function(err, file) {
+ if (err) throw err;
+
+ console.log(file);
+});
+```
This will list all files in your current working directory without an order.
You may also apply options to the function call.
- dive(dir, opt, action);
+```javascript
+dive(dir, opt, action);
+```
The default options are
- {
- recursive: true, // If set to false, this will ignore subdirectories.
- all: false // If set to true, this will show "dot files" and
- // files in "dot directories", e.g. ".gitinore" or
- // ".git/HEAD".
- }
+```javascript
+{
+ recursive: true, // If set to false, this will ignore subdirectories.
+ all: false // If set to true, this will show "dot files" and
+ // files in "dot directories", e.g. ".gitinore" or
+ // ".git/HEAD".
+}
+```
This argument is optional.
View
92 lib/dive.js
@@ -1,55 +1,51 @@
-var fs = require("fs");
+var fs = require('fs');
// General function
var dive = function(dir, opt, action) {
- // action is the last argument
- action = arguments[arguments.length - 1];
-
- // Assert that dir is a string
- if (typeof dir !== "string")
- dir = process.cwd();
-
- // Assert that opt is an object
- if (opt == undefined || typeof opt !== "object")
- opt = { };
-
- // Default settings
- if (opt.recursive == undefined)
- opt.recursive = true;
- if (opt.all == undefined)
- opt.all = false;
-
- // Assert that it's a function
- if (typeof action !== "function")
- action = function(error, file) { };
-
- // Read the directory
- fs.readdir(dir, function(err, list) {
- // Return the error if something went wrong
- if (err)
- return action(err);
-
- // For every file in the list
- list.forEach(function(file) {
- if (opt.all || file[0] != ".") {
- // Full path of that file
- var path = dir + "/" + file;
- // Get the file's stats
- fs.stat(path, function(err, stat) {
- // If the file is a directory
- if (stat && stat.isDirectory()) {
- // Dive into the directory
- if (opt.recursive)
- dive(path, opt, action);
- } else {
- // Call the action
- action(null, path);
- }
- });
- }
- });
- });
+ // action is the last argument
+ action = arguments[arguments.length - 1];
+
+ // Assert that dir is a string
+ if (typeof dir != 'string')
+ dir = process.cwd();
+
+ // Assert that opt is an object
+ if (typeof opt == 'undefined')
+ opt = { };
+
+ // Default settings
+ if (opt.recursive == undefined)
+ opt.recursive = true;
+ if (opt.all == undefined)
+ opt.all = false;
+
+ // Read the directory
+ fs.readdir(dir, function(err, list) {
+ // Return the error if something went wrong
+ if (err)
+ return action(err);
+
+ // For every file in the list
+ list.forEach(function(file) {
+ if (opt.all || file[0] != '.') {
+ // Full path of that file
+ var path = dir + '/' + file;
+ // Get the file's stats
+ fs.stat(path, function(err, stat) {
+ // If the file is a directory
+ if (stat && stat.isDirectory()) {
+ // Dive into the directory
+ if (opt.recursive)
+ dive(path, opt, action);
+ } else {
+ // Call the action
+ action(null, path);
+ }
+ });
+ }
+ });
+ });
};
module.exports = dive;
View
34 package.json
@@ -1,17 +1,21 @@
{
- "name": "dive",
- "description":
- "walk through directory trees and apply an action to every file",
- "author": "Paul Vorbach <paul@vorb.de>",
- "version": "0.0.3",
- "main": "./lib/dive.js",
- "repository": "git://github.com/pvorb/node-dive.git",
- "tags": [
- "recursive",
- "file walking",
- "directories"
- ],
- "bugs": {
- "web": "http://github.com/pvorb/dive/issues"
- }
+ "name": "dive",
+ "description":
+ "walk through directory trees and apply an action to every file",
+ "author": "Paul Vorbach <paul@vorb.de>",
+ "version": "0.0.4",
+ "main": "./lib/dive.js",
+ "repository": "git://github.com/pvorb/node-dive.git",
+ "tags": [
+ "recursive",
+ "file walking",
+ "directories",
+ "async"
+ ],
+ "bugs": {
+ "web": "http://github.com/pvorb/node-dive/issues"
+ },
+ "engines": {
+ "node": ">=0.4.0"
+ }
}
View
6 test/test.js
@@ -0,0 +1,6 @@
+var dive = require("../");
+
+dive(process.cwd(), { all: false, recursive: false }, function(err, file) {
+ if (err) throw err;
+ console.log(file);
+});
View
15 test/test2.js
@@ -0,0 +1,15 @@
+var dive = require("../");
+
+var res = [];
+
+dive(process.cwd(), function(err, file) {
+ if (err)
+ throw err;
+ res.push(file);
+});
+
+setTimeout(function() {
+ console.log(res);
+ console.log();
+ console.log(res.sort());
+}, 500);
Please sign in to comment.
Something went wrong with that request. Please try again.