https://github.com/do-/node-dir-list
Iterating over file paths in a given set of directories, lambda filters
https://github.com/do-/node-dir-list
fs iterator
Last synced: about 2 months ago
JSON representation
Iterating over file paths in a given set of directories, lambda filters
- Host: GitHub
- URL: https://github.com/do-/node-dir-list
- Owner: do-
- License: other
- Created: 2024-07-28T11:37:11.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-28T13:57:23.000Z (over 1 year ago)
- Last Synced: 2025-10-25T06:32:23.088Z (5 months ago)
- Topics: fs, iterator
- Language: JavaScript
- Homepage:
- Size: 45.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README


`fs-iterators` is a zero dependency node.js library implementing [file system](https://nodejs.org/api/fs.html) [iterators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) over given sets or root paths with functions as filtering conditions.
It features two classes:
* [DirList](https://github.com/do-/node-dir-list/wiki/DirList), the directory tree iterator;
* [FilePaths](https://github.com/do-/node-dir-list/wiki/FilePaths), the files iterator.
# Installation
```sh
npm install fs-iterators
```
# Usage
```js
const {DirList} = require ('fs-iterators')
const myDir = new DirList ({
root: ['/opt/myProject'],
filter: (str, arr) =>
/src/.test (str) // contains 'src'
&& arr.at (-2) === 'Model', // **/Model/*
// live: true, // avoid caching, scan every time
})
for (const dir of myDir.paths) console.log ({dir})
for (const file of myDir.files ()) console.log ({file})
for (const file of myDir.files ('index.js')) console.log ({file})
for (const file of myDir.files (_ => /js$/.test (_))) console.log ({file})
```
# See also
* [fs-iterator](https://www.npmjs.com/package/fs-iterator) seems to solve a pretty similar problem, with some restrictions (e. g. a single root path instead of multiple ones);
* [filelist](https://www.npmjs.com/package/filelist) is a much more popular solution, but it implements the complete [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) interface instead of only Iterator and uses masks (globs) instead of functional filters.