Skip to content

Commit 1b41552

Browse files
committed
path-reader
1 parent 6ad7dec commit 1b41552

5 files changed

Lines changed: 131 additions & 46 deletions

File tree

CHANGELOG.json renamed to CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# path-reader - Change Log
22
All notable changes to this project will be documented here.
33

4+
## [1.1.0] - 2018-02-14
5+
- valuetizer option when returns null, item is skipped
6+
- Happy Valentines Day
7+
48
## [1.0.6] - 2017-10-13
59
- empty folders containing empty items reads are now empty array instead of undefined
610

README.md

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[![hire me](https://ackerapple.github.io/resume/assets/images/hire-me-badge.svg)](https://ackerapple.github.io/resume/)
2+
[![npm downloads](https://img.shields.io/npm/dm/path-reader.svg)](https://npmjs.org/path-reader)
3+
[![Dependency Status](https://david-dm.org/ackerapple/path-reader.svg)](https://david-dm.org/ackerapple/path-reader)
14
[![Build Status](https://secure.travis-ci.org/AckerApple/path-reader.svg)](http://travis-ci.org/AckerApple/path-reader)
25
[![Build status](https://ci.appveyor.com/api/projects/status/6sa5pfcsrix5s8va?svg=true)](https://ci.appveyor.com/project/AckerApple/path-reader)
36
[![NPM version](https://img.shields.io/npm/v/path-reader.svg?style=flat-square)](https://www.npmjs.com/package/path-reader)
@@ -9,24 +12,28 @@ A lightweight Node.js module with methods for some common directory and file ope
912

1013
- [installation](#installation)
1114
- [usage](#usage)
12-
- [methods](#methods)
13-
- [readFiles](#readfiles)
14-
- [readFilesStream](#readfilesstream)
15-
- [readFilesStream examples](#readfilesstream-examples)
16-
- [files async](#files-async)
17-
- [files sync](#files-sync)
18-
- [promiseFiles](#promisefiles)
19-
- [subdirs](#subdirs)
20-
- [paths](#paths)
15+
- [methods](#methods)
16+
- [readFiles](#readfiles)
17+
- [options](#options)
18+
- [readFilesStream](#readfilesstream)
19+
- [readFilesStream examples](#readfilesstream-examples)
20+
- [readFiles valuetizer](#readfiles-valuetizer)
21+
- [files async](#files-async)
22+
- [files sync](#files-sync)
23+
- [promiseFiles](#promisefiles)
24+
- [subdirs](#subdirs)
25+
- [paths](#paths)
2126
- [API Docs](#api-docs)
22-
- [files](#files-api)
23-
- [promiseFiles](#promisefiles-api)
27+
- [files](#files-api)
28+
- [promiseFiles](#promisefiles-api)
2429
- [History](#history)
2530
- [License](#license)
2631

2732
#### installation
2833

29-
npm install path-reader
34+
```
35+
npm install path-reader
36+
```
3037

3138
### usage
3239

@@ -43,26 +50,45 @@ A variation on the method readFilesStream. See usage for [readFilesStream](#read
4350
readFiles( dir, [options], fileCallback, [finishedCallback] )
4451
```
4552

53+
#### Options
54+
- **encoding** - file encoding (defaults to 'utf8')
55+
- **exclude** - a regex pattern or array to specify filenames to ignore
56+
- **excludeDir** - a regex pattern or array to specify directories to ignore
57+
- **match** - a regex pattern or array to specify filenames to operate on
58+
- **matchDir** - a regex pattern or array to specify directories to recurse
59+
- **recursive** - whether to recurse subdirectories when reading files (defaults to true)
60+
- **reverse** - sort files in each directory in descending order
61+
- **shortName** - whether to aggregate only the base filename rather than the full filepath
62+
- **sort** - sort files in each directory in ascending order (defaults to true)
63+
- A reverse sort can also be achieved by setting the sort option to 'reverse', 'desc', or 'descending' string value.
64+
- **doneOnErr** - control if done function called on error (defaults to true)
65+
- **sync** : boolean = false - results are returned inline and no callbacks are used
66+
- **shortName** : boolean = false||'relative' - instead of fullpath file names, just get the names or relative item names
67+
- **recursive** : boolean = true - traverse through all children of given path
68+
- **excludeHidden** : boolean - hidden files will be ignored (files starting with a dot are ignored)
69+
- **valuetizer** : (stat, fileName, filePath) - A function of (stat, fileName, fileFullPath) . When null returned, item is skipped in end results
70+
4671
#### readFilesStream
4772
Sequentially read the content of each file in a directory, passing the contents to a callback, optionally calling a finished callback when complete. The options and finishedCallback arguments are not required.
4873

4974
```javascript
5075
readFilesStream( dir, [options], streamCallback, [finishedCallback] )
5176
```
5277

53-
Valid options are:
54-
- encoding: file encoding (defaults to 'utf8')
55-
- exclude: a regex pattern or array to specify filenames to ignore
56-
- excludeDir: a regex pattern or array to specify directories to ignore
57-
- match: a regex pattern or array to specify filenames to operate on
58-
- matchDir: a regex pattern or array to specify directories to recurse
59-
- recursive: whether to recurse subdirectories when reading files (defaults to true)
60-
- reverse: sort files in each directory in descending order
61-
- shortName: whether to aggregate only the base filename rather than the full filepath
62-
- sort: sort files in each directory in ascending order (defaults to true)
63-
- doneOnErr: control if done function called on error (defaults to true)
64-
65-
A reverse sort can also be achieved by setting the sort option to 'reverse', 'desc', or 'descending' string value.
78+
#### readFiles valuetizer
79+
An example of building an array of only items with an mtime
80+
```javascript
81+
var options = {
82+
valuetizer:function(stat, shortName, longPath){
83+
return stat.mtime ? stat : null
84+
}
85+
}
86+
87+
require('path-reader').promiseFiles(tdir, 'file', options)
88+
.then(function(results){
89+
console.log(results)//an array of file stat if the file has a mtime definition
90+
})
91+
```
6692

6793
#### readFilesStream examples
6894

@@ -256,16 +282,17 @@ files(dir, type, callback, options)
256282

257283
- **dir** - directory path to read
258284
- **type**='file'
259-
- 'file' returns only file listings
260-
- 'dir' returns only directory listings
261-
- 'all' returns {dirs:[], files:[]}
262-
- 'combine' returns []
285+
- 'file' returns only file listings
286+
- 'dir' returns only directory listings
287+
- 'all' returns {dirs:[], files:[]}
288+
- 'combine' returns []
263289
- **callback** -
264290
- **options**
265-
- **sync**=false - results are returned inline and no callbacks are used
266-
- **shortName**=false||'relative' - instead of fullpath file names, just get the names or relative item names
267-
- **recursive**=true - traverse through all children of given path
268-
- **excludeHidden** - hidden files will be ignored (files starting with a dot are ignored)
291+
- **sync**=false - results are returned inline and no callbacks are used
292+
- **shortName**=false||'relative' - instead of fullpath file names, just get the names or relative item names
293+
- **recursive**=true - traverse through all children of given path
294+
- **excludeHidden** - hidden files will be ignored (files starting with a dot are ignored)
295+
- **valuetizer** - A function of (stat, fileShortName, fileFullPath) . When null returned, item is skipped in end results
269296

270297
### promiseFiles API
271298

@@ -283,6 +310,7 @@ promiseFiles(dir, type||options, options)
283310
- **sync**=false - DO NOT USE for promiseFiles, will cause unexpected behavior
284311
- **shortName**=false||'relative' - instead of fullpath file names, just get the names or relative item names
285312
- **recursive**=true - traverse through all children of given path
313+
- **valuetizer** - A function of (stat, fileShortName, fileFullPath) . When null returned, item is skipped in end results
286314

287315
## History
288316
path-reader is a fork of node-dir. The original maintainer of node-dir, @fshost, has not updated nor been heard from in some time. Use path-reader, it is far superior to node-dir.

lib/paths.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ exports.files = function files(dir, type, callback, options) {
9393
var isDir = stat && stat.isDirectory() && stat.mode !== 17115
9494
var pushVal = statHanOptions.valuetizer(stat, name, statPath, isDir)//options.shortName ? name : statPath
9595

96+
if( pushVal==null ){
97+
if (!--pending){
98+
done();
99+
}
100+
return
101+
}
102+
96103
if (isDir) {
97104
if (type !== 'file') {
98105
results.dirs.push(pushVal);
@@ -161,6 +168,13 @@ exports.files = function files(dir, type, callback, options) {
161168
return list
162169
}
163170

171+
var statHanOptions = {}
172+
if( options.valuetizer ){
173+
statHanOptions.valuetizer = options.valuetizer
174+
}else{
175+
statHanOptions.valuetizer = getValuetizerByOptions(options, dir)
176+
}
177+
/*
164178
var statHanOptions = {}
165179
if(options.shortName){
166180
if(options.shortName=='relative'){
@@ -183,7 +197,7 @@ exports.files = function files(dir, type, callback, options) {
183197
return longPath
184198
}
185199
}
186-
200+
*/
187201
for (var file, i = 0, l = list.length; i < l; i++) {
188202
var fname = list[i].toString();
189203
file = path.join(dir, fname);
@@ -295,4 +309,28 @@ exports.subdirs = function subdirs(dir, callback, type, options) {
295309
function assign(c0, c1){
296310
for(var x in c1)c0[x] = c1[x]
297311
return c0
298-
}
312+
}
313+
314+
function getValuetizerByOptions(options, dir){
315+
if(options.shortName){
316+
if( options.shortName=='relative' ){
317+
var dirBase = (options.basePath||dir)
318+
var startPos = dirBase.length
319+
if(dirBase.substring(dirBase.length-path.sep.length, dirBase.length)!=path.sep){
320+
startPos = startPos + path.sep.length
321+
}
322+
323+
return function(stat, shortName, longPath, isDir){
324+
return longPath.substring(startPos, longPath.length)
325+
}
326+
}else{
327+
return function(stat, shortName, longPath){
328+
return shortName
329+
}
330+
}
331+
}
332+
333+
return function(stat, shortName, longPath){
334+
return longPath
335+
}
336+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "path-reader",
3-
"version": "1.0.6",
3+
"version": "1.1.0",
44
"description": "asynchronous file and directory operations for Node.js",
55
"main": "index",
66
"homepage": "https://github.com/ackerapple",

test/test.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,27 +1362,42 @@ describe('paths method', function() {
13621362
});
13631363
});
13641364

1365-
describe('when called with combine argument set to true', function() {
1366-
1365+
describe('when called with combine argument set to true', function(){
13671366
it('should pass an array of filepaths of all subdirs and files in a directory and its subdirs to a callback', function(done) {
13681367
dir.paths(tdir, true, function(err, paths) {
1369-
should.not.exist(err);
1368+
should.not.exist(err)
13701369

13711370
paths.should.be.a.array;
13721371
var relPaths = paths.map(function(curPath) {
13731372
return path.relative(fixturesDir, curPath);
1374-
});
1373+
})
1374+
13751375
relPaths.sort().should.eql([
13761376
'testdir'+path.sep+'file1.txt',
13771377
'testdir'+path.sep+'file2.text',
13781378
'testdir'+path.sep+'subdir',
13791379
'testdir'+path.sep+'subdir'+path.sep+'file3.txt',
13801380
'testdir'+path.sep+'subdir'+path.sep+'file4.text'
1381-
]);
1382-
});
1383-
done();
1384-
});
1385-
1386-
});
1381+
])
1382+
})
1383+
done()
1384+
})
1385+
})
1386+
1387+
describe('valuetizer',function(){
1388+
it('files by mtime', function(done) {
1389+
var foundMtime = false
1390+
var options = {
1391+
valuetizer:function(stat, shortName, longPath){
1392+
return stat.mtime && !foundMtime ? foundMtime=true && stat : null
1393+
}
1394+
}
13871395

1396+
dir.promiseFiles(tdir, 'file', options)
1397+
.then(function(results){
1398+
assert.equal(results.length, 1)
1399+
})
1400+
.then(done).catch(done)
1401+
})
1402+
})
13881403
});

0 commit comments

Comments
 (0)