Socket
Socket
Sign inDemoInstall

fdir

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fdir - npm Package Compare versions

Comparing version 1.0.3 to 1.1.0

.github/workflows/nodejs.yml

12

index.d.ts

@@ -8,11 +8,9 @@ declare module "fdir" {

resolvePaths?: boolean;
excludedDirs?: { [dirName: string]: boolean };
isExcludedDir?: (dirPath: string) => boolean;
};
type FDir = {
sync: (dir: String) => Array<String>;
async: (dir: String) => Promise<Array<String>>;
};
function fdir(options: Options): FDir;
export = fdir;
function sync(dir: String, options: Options): Array<String>;
function async(dir: String, options: Options): Promise<Array<String>>;
export = sync;
export = async;
}

@@ -6,6 +6,2 @@ const util = require("util");

function push(item) {
this.push(item);
}
function sync(dir, options) {

@@ -18,10 +14,8 @@ const dirents = fs.readdirSync(dir, { withFileTypes: true });

dirents.forEach(async dirent => {
const res = `${dir}${path.sep}${dirent.name}`;
if (dirent.isDirectory()) {
if (options.excludedDirs && options.excludedDirs[dirent.name]) return;
sync(res, options).forEach(push.bind(paths));
} else {
if (!options.includeBasePath) res = dirent.name;
if (!options.searchFn || options.searchFn(res)) paths.push(res);
dirents.forEach(function(dirent) {
const dirPath = recurse(dirent, dir, paths, options);
if (dirPath) {
sync(dirPath, options).forEach(function(item) {
paths.push(item);
});
}

@@ -37,12 +31,11 @@ });

if (options.includeDirs) paths.push(dir);
if (--options.maxDepth < 0) resolve(paths);
if (--options.maxDepth < 0) return paths;
await Promise.all(
dirents.map(async dirent => {
const res = `${dir}${path.sep}${dirent.name}`;
if (dirent.isDirectory()) {
(await async(res, options)).forEach(push.bind(paths));
} else {
if (!options.includeBasePath) res = dirent.name;
if (!options.searchFn || options.searchFn(res)) paths.push(res);
dirents.map(async function(dirent) {
const dirPath = recurse(dirent, dir, paths, options);
if (dirPath) {
(await async(dirPath, options)).forEach(function(item) {
paths.push(item);
});
}

@@ -54,3 +47,20 @@ })

function fdir(options) {
function recurse(dirent, dir, paths, options) {
// In node < 10, Dirent is not present. Instead we get string paths
const dirName = dirent.name || dirent;
let fullPath = `${dir}${path.sep}${dirName}`;
const isDirectory = dirent.isDirectory
? dirent.isDirectory()
: fs.lstatSync(fullPath).isDirectory();
if (isDirectory) {
if (options.isExcludedDir && options.isExcludedDir(dirName)) return;
return fullPath;
} else {
if (!options.includeBasePath) fullPath = dirName;
if (!options.searchFn || options.searchFn(fullPath)) paths.push(fullPath);
}
}
function getOptions(options) {
const defaultOptions = {

@@ -64,15 +74,14 @@ includeDirs: false,

};
options = !options ? defaultOptions : { ...defaultOptions, ...options };
return {
sync: dir => {
if (options.resolvePaths) dir = path.resolve(dir);
return sync(dir, options);
},
async: dir => {
if (options.resolvePaths) dir = path.resolve(dir);
return async(dir, options);
}
};
return !options ? defaultOptions : Object.assign(defaultOptions, options);
}
module.exports = fdir;
function getFunction(type, dir, options) {
options = getOptions(options);
if (options.resolvePaths) dir = path.resolve(dir);
return type(dir, options);
}
module.exports = {
sync: getFunction.bind(this, sync),
async: getFunction.bind(this, async)
};
{
"name": "fdir",
"version": "1.0.3",
"description": "The fastest directory crawler written for NodeJS. Zero dependencies. Grabs 10k files in 13ms.",
"version": "1.1.0",
"description": "The fastest directory crawler for NodeJS. Crawls 10k files in 13ms.",
"main": "index.js",
"scripts": {
"test": "test",
"test": "jest",
"test:coverage": "jest --coverage",
"benchmark": "node benchmark.js"

@@ -35,3 +36,2 @@ },

"devDependencies": {
"@folder/readdir": "^2.1.0",
"all-files-in-tree": "^1.1.2",

@@ -41,2 +41,3 @@ "benny": "^3.6.14",

"get-all-files": "^1.0.7",
"jest": "24.0.0",
"klaw-sync": "^6.0.0",

@@ -46,5 +47,5 @@ "recur-readdir": "0.0.1",

"recursive-readdir": "^2.2.2",
"rrdir": "^5.0.0",
"rrdir": "^2.0.0",
"walk-sync": "^2.0.2"
}
}

@@ -8,2 +8,4 @@ <p align="center">

<a href="https://www.npmjs.com/package/fdir"><img src="https://img.shields.io/npm/dt/fdir?style=for-the-badge"/></a>
<a href="https://codeclimate.com/github/thecodrr/fdir/maintainability"><img src="https://img.shields.io/codeclimate/maintainability-percentage/thecodrr/fdir?style=for-the-badge"/></a>
<a href="https://coveralls.io/github/thecodrr/fdir?branch=master"><img src="https://img.shields.io/coveralls/github/thecodrr/fdir?style=for-the-badge"/></a>
<a href="./LICENSE"><img src="https://img.shields.io/github/license/thecodrr/fdir?style=for-the-badge"/></a>

@@ -51,25 +53,31 @@ </p>

> I recently discovered a quirky side in how NodeJS works. It gives different performance when the machine is on direct power and when purely on battery. (If someone knows anything about that, do tell me.) So for the sake of completeness and so that no one disputes my claims, I included benchmarks for both cases.
```sh
$ yarn benchmark
```
**Specs:**
- Intel i7 7th Generation
- Intel i7 7th Generation (7700HQ)
- 16 GB of RAM
- 256 GB SSD
- Directory Size: 14.1 MB / 2400 files
- Directory Size: 7386 files
### On Power:
**Notes:**
<img src="https://github.com/thecodrr/fdir/raw/master/assets/power.png"/>
- Some people asked that I benchmark `no-op` (without options) version of `fdir`. I did and found no performance difference. The results were identical. (I didn't include it here as it wasn't anything special.)
### On Battery:
### Node v13.11.0:
<img src="https://github.com/thecodrr/fdir/raw/master/assets/battery.png"/>
| Synchronous (7386 files) | Asynchronous (7386 files) |
| :---------------------------: | :----------------------------: |
| ![](./assets/node13-sync.png) | ![](./assets/node13-async.png) |
### Run them Yourself:
### Node v8.3.0:
```sh
$ yarn benchmark
```
**Note: As latest version of `rrdir` doesn't support Node < 8, I had to use version 2.0.0. Everything else is fully updated.**
| Synchronous (7386 files) | Asynchronous (7386 files) |
| :--------------------------: | :---------------------------: |
| ![](./assets/node8-sync.png) | ![](./assets/node8-async.png) |
## 🚒 API:

@@ -123,3 +131,3 @@

#### `excludedDirs: Object`
#### `isExcludedDir: Function`

@@ -133,7 +141,4 @@ A list of directories to exclude.

```js
const excludedDirs = {
node_modules: true
};
fdir.sync("node_modules", { excludedDirs });
const isExcludedDir = path => path.includes(".bin");
fdir.sync("node_modules", { isExcludedDir });
```

@@ -140,0 +145,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc