Comparing version 0.1.0 to 0.1.1
@@ -18,3 +18,12 @@ # {%= name %} {%= badge("fury") %} | ||
var list = require('{%= name %}'); | ||
console.log(list('fixtures')); | ||
// async | ||
list('fixtures', function (err, files) { | ||
if (err) console.log(err); | ||
console.log(files) | ||
}); | ||
// sync | ||
var files = list('fixtures'); | ||
console.log(files); | ||
``` | ||
@@ -30,4 +39,14 @@ | ||
var isDir = require('is-directory'); | ||
console.log(dirs('fixtures').filter(isDir.sync)); | ||
//=> [ 'fixtures/one', 'fixtures/three', 'fixtures/two' ] | ||
// async | ||
list('fixtures', function (err, files) { | ||
if (err) console.log(err); | ||
files = files.filter(isDir); | ||
console.log(files) | ||
//=> [ 'fixtures/one', 'fixtures/three', 'fixtures/two' ] | ||
}); | ||
// Sync | ||
console.log(list('fixtures').filter(isDir)); | ||
``` | ||
@@ -34,0 +53,0 @@ |
53
index.js
@@ -0,1 +1,8 @@ | ||
/*! | ||
* dirs <https://github.com/jonschlinkert/dirs> | ||
* | ||
* Copyright (c) 2014 Jon Schlinkert, contributors. | ||
* Licensed under the MIT License | ||
*/ | ||
'use strict'; | ||
@@ -6,15 +13,43 @@ | ||
var dirs = module.exports = function dirs(dir, cb) { | ||
if (typeof cb !== 'function') { | ||
return dirs.sync(dir); | ||
} | ||
module.exports = function dirs(dir) { | ||
return fs.readdirSync(dir) | ||
.reduce(function(acc, filepath) { | ||
filepath = [dir, filepath].join('/'); | ||
if (!dir.length) { | ||
return cb(null, []); | ||
} | ||
if (isDir.sync(filepath)) { | ||
acc = acc.concat(dirs(filepath)); | ||
fs.readdir(dir, function (err, files) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
acc.push(filepath); | ||
return acc; | ||
}, []); | ||
var arr = []; | ||
files.forEach(function (filepath) { | ||
filepath = [dir, filepath].join('/'); | ||
if (isDir(filepath)) { | ||
arr = arr.concat(dirs(filepath)); | ||
} | ||
arr.push(filepath); | ||
}); | ||
return cb(null, arr); | ||
}); | ||
}; | ||
dirs.sync = function dirsSync(dir) { | ||
return fs.readdirSync(dir) | ||
.reduce(function (acc, filepath) { | ||
filepath = [dir, filepath].join('/'); | ||
if (isDir(filepath)) { | ||
acc = acc.concat(dirs(filepath)); | ||
} | ||
acc.push(filepath); | ||
return acc; | ||
}, []); | ||
}; |
{ | ||
"name": "dirs", | ||
"description": "List directories or files.", | ||
"version": "0.1.0", | ||
"description": "List directories or files, async or sync.", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/jonschlinkert/dirs", | ||
@@ -31,3 +31,4 @@ "author": { | ||
"dependencies": { | ||
"is-directory": "^0.2.0" | ||
"async": "^0.9.0", | ||
"is-directory": "^0.2.1" | ||
}, | ||
@@ -34,0 +35,0 @@ "devDependencies": { |
# dirs [![NPM version](https://badge.fury.io/js/dirs.svg)](http://badge.fury.io/js/dirs) | ||
> List directories or files. | ||
> List directories or files, async or sync. | ||
@@ -23,3 +23,12 @@ ## Install | ||
var list = require('dirs'); | ||
console.log(list('fixtures')); | ||
// async | ||
list('fixtures', function (err, files) { | ||
if (err) console.log(err); | ||
console.log(files) | ||
}); | ||
// sync | ||
var files = list('fixtures'); | ||
console.log(files); | ||
``` | ||
@@ -35,4 +44,14 @@ | ||
var isDir = require('is-directory'); | ||
console.log(dirs('fixtures').filter(isDir.sync)); | ||
//=> [ 'fixtures/one', 'fixtures/three', 'fixtures/two' ] | ||
// async | ||
list('fixtures', function (err, files) { | ||
if (err) console.log(err); | ||
files = files.filter(isDir); | ||
console.log(files) | ||
//=> [ 'fixtures/one', 'fixtures/three', 'fixtures/two' ] | ||
}); | ||
// Sync | ||
console.log(list('fixtures').filter(isDir)); | ||
``` | ||
@@ -39,0 +58,0 @@ |
60
test.js
@@ -12,8 +12,6 @@ /*! | ||
var should = require('should'); | ||
var isDirectory = require('is-directory'); | ||
var isDir = require('is-directory'); | ||
var dirs = require('./'); | ||
function isDir (fp) { | ||
return isDirectory.sync(fp); | ||
} | ||
function isFile (fp) { | ||
@@ -29,21 +27,53 @@ return !isDir(fp); | ||
describe('dirs', function () { | ||
it('should return an array of directories and files', function (done) { | ||
dirs('fixtures', function (err, files) { | ||
files.should.be.an.array; | ||
assert.equal(files.length > 1, true); | ||
done(); | ||
}); | ||
}); | ||
it('should list directories', function (done) { | ||
dirs('fixtures', function (err, files) { | ||
files = files.filter(isDir); | ||
files.should.be.an.array; | ||
assert.equal(files.length > 1, true); | ||
assert.equal(files.filter(matches(/\.js/)).length === 0, true); | ||
done(); | ||
}); | ||
}); | ||
it('should list files', function (done) { | ||
dirs('fixtures', function (err, files) { | ||
files = files.filter(isFile); | ||
files.should.be.an.array; | ||
assert.equal(files.length > 1, true); | ||
assert.equal(files.filter(matches(/\.js/)).length > 1, true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('dirs sync', function () { | ||
it('should return an array of directories and files', function () { | ||
var actual = dirs('fixtures'); | ||
actual.should.be.an.array; | ||
assert.equal(actual.length > 1, true); | ||
var files = dirs('fixtures'); | ||
files.should.be.an.array; | ||
assert.equal(files.length > 1, true); | ||
}); | ||
it('should list directories', function () { | ||
var actual = dirs('fixtures').filter(isDir); | ||
actual.should.be.an.array; | ||
assert.equal(actual.length > 1, true); | ||
assert.equal(actual.filter(matches(/\.js/)).length === 0, true); | ||
var files = dirs('fixtures').filter(isDir); | ||
files.should.be.an.array; | ||
assert.equal(files.length > 1, true); | ||
assert.equal(files.filter(matches(/\.js/)).length === 0, true); | ||
}); | ||
it('should list files', function () { | ||
var actual = dirs('fixtures').filter(isFile); | ||
actual.should.be.an.array; | ||
assert.equal(actual.length > 1, true); | ||
assert.equal(actual.filter(matches(/\.js/)).length > 1, true); | ||
var files = dirs('fixtures').filter(isFile); | ||
files.should.be.an.array; | ||
assert.equal(files.length > 1, true); | ||
assert.equal(files.filter(matches(/\.js/)).length > 1, true); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8305
107
71
2
+ Addedasync@^0.9.0
+ Addedasync@0.9.2(transitive)
Updatedis-directory@^0.2.1