Comparing version 1.2.0 to 2.0.0
77
index.js
const fs = require("fs"); | ||
const path = require("path"); | ||
const { sep, resolve } = require("path"); | ||
const readdirOpts = { withFileTypes: true }; | ||
function sync(dir, options) { | ||
function sync(dir, options = {}) { | ||
if (options.resolvePaths) dir = resolve(dir); | ||
const paths = []; | ||
const dirs = [dir]; | ||
let currentDepth = options.maxDepth; | ||
var i = 0; | ||
while (i < dirs.length && !(--currentDepth < 0)) { | ||
const dir = dirs[i]; | ||
if (options.includeDirs) paths.push(dir); | ||
const dirents = fs.readdirSync(dir, readdirOpts); | ||
for (var i = 0; i < dirs.length && !(--options.maxDepth < 0); ++i) { | ||
var currentDir = dirs[i]; | ||
if (options.includeDirs) paths[paths.length] = currentDir; | ||
const dirents = fs.readdirSync(currentDir, readdirOpts); | ||
dirents.forEach(function(dirent) { | ||
recurse(dirent, dir, paths, options, dirs); | ||
recurse(dirent, currentDir, paths, options, dirs); | ||
}); | ||
++i; | ||
} | ||
@@ -23,5 +21,6 @@ return paths; | ||
function async(dir, options) { | ||
return new Promise(function(resolve) { | ||
function async(dir, options = {}) { | ||
return new Promise(function(pResolve) { | ||
const paths = []; | ||
if (options.resolvePaths) dir = resolve(dir); | ||
const dirs = [dir]; | ||
@@ -35,15 +34,14 @@ let cursor = 0; | ||
if (--currentDepth < 0) { | ||
resolve(paths); | ||
pResolve(paths); | ||
break; | ||
} | ||
const dir = dirs[cursor]; | ||
if (options.includeDirs) paths.push(dir); | ||
fs.readdir(dir, readdirOpts, function(err, dirents) { | ||
if (!dirents) return; | ||
dirents.forEach(function(dirent) { | ||
recurse(dirent, dir, paths, options, dirs); | ||
}); | ||
if (options.includeDirs) paths[paths.length] = dir; | ||
fs.readdir(dir, readdirOpts, function(_, dirents) { | ||
for (var j = 0; j < dirents.length; ++j) { | ||
recurse(dirents[j], dir, paths, options, dirs); | ||
} | ||
if (++readCount === total) { | ||
if (dirs.length === cursor) { | ||
resolve(paths); | ||
pResolve(paths); | ||
} else { | ||
@@ -62,38 +60,25 @@ walk(); | ||
// In node < 10, Dirent is not present. Instead we get string paths | ||
/* istanbul ignore next */ | ||
const dirName = dirent.name || dirent; | ||
let fullPath = `${dir}${path.sep}${dirName}`; | ||
let fullPath = `${dir}${sep}${dirName}`; | ||
/* istanbul ignore next */ | ||
const isDirectory = dirent.isDirectory | ||
? dirent.isDirectory() | ||
: fs.lstatSync(fullPath).isDirectory(); | ||
if (isDirectory) { | ||
if (options.isExcludedDir && options.isExcludedDir(dirName)) return; | ||
dirs.push(fullPath); | ||
} else { | ||
if (!options.includeBasePath) fullPath = dirName; | ||
if (!options.searchFn || options.searchFn(fullPath)) paths.push(fullPath); | ||
if (options.isExcludedDir && options.isExcludedDir(fullPath)) return; | ||
dirs[dirs.length] = fullPath; | ||
return; | ||
} | ||
if (options.excludeBasePath) fullPath = dirName; | ||
if (!options.searchFn || options.searchFn(fullPath)) | ||
paths[paths.length] = fullPath; | ||
} | ||
function getOptions(options) { | ||
const defaultOptions = { | ||
includeDirs: false, | ||
includeBasePath: true, | ||
maxDepth: undefined, | ||
searchFn: undefined, | ||
resolvePaths: false, | ||
excludedDirs: undefined | ||
}; | ||
return !options ? defaultOptions : Object.assign(defaultOptions, options); | ||
} | ||
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) | ||
sync, | ||
async | ||
}; |
{ | ||
"name": "fdir", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "The fastest directory crawler for NodeJS. Crawls 10k files in 13ms.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest", | ||
"test:coverage": "jest --coverage", | ||
"test": "jest __tests__/fdir.test.js", | ||
"test:coverage": "jest __tests__/fdir.test.js --coverage", | ||
"benchmark": "node benchmark.js" | ||
@@ -10,0 +10,0 @@ }, |
@@ -22,2 +22,4 @@ <p align="center"> | ||
🔥 **All Node Versions Supported:** `fdir` runs everywhere on all Node versions (within reason). And it is unsurprisingly fastest there too. | ||
## 🚄 Quickstart | ||
@@ -62,2 +64,3 @@ | ||
- 256 GB SSD | ||
- OS: Manjaro Linux | ||
- Directory Size: 7386 files | ||
@@ -105,7 +108,7 @@ | ||
#### `includeBasePath: boolean` | ||
#### `excludeBasePath: boolean` | ||
Whether to include the base path for each file. | ||
Whether to exclude the base path for each file. | ||
`default: true` | ||
`default: false` | ||
@@ -134,6 +137,4 @@ #### `searchFn: Function` | ||
A list of directories to exclude. | ||
Use this to exclude particular directories from being crawled. | ||
> **Note: `fdir` expects an **Object** not an array.** | ||
**Example:** | ||
@@ -156,19 +157,27 @@ | ||
**2. Why create this? What's the point?** | ||
**2. I found X library. I ran its benchmarks. It is faster than `fdir`!** | ||
Um. Well thank you for embarassing me (just joking). Do tell me the name of this library though. I will try to optimize `fdir` and reclaim the first spot :smile: | ||
**3. You are doing X and Y wrong! Do Z and it will improve performance!** | ||
Yes. And I should probably do A, B & C too. The point is, did you run benchmarks with these suggestions? If you did and saw significant improvements, thank you. Now go open a PR :laugh: | ||
**4. Why create this? What's the point?** | ||
I know you don't give a shit. Fine. There's no point behind this. It's "just for fun". No, wait. Actually, I created this, first of all, for me. I needed fast directory access in another app of mine, so `fdir` came into being. | ||
**3. Why are all the other libraries so slow?** | ||
**5. Why are all the other libraries so slow?** | ||
Because they did not spend enough time optimizing it. Most developers give readability and cool code more importance than actual performance and usability. I have seen a library claiming to be the fastest by inverting the benchmarks. Literally. Gave me quite the scare until I went and fixed the benchmark. It was actually one of the slowest. :O | ||
**4. How long did it take you to create this?** | ||
**6. How long did it take you to create this?** | ||
Ummm. Maybe 18 hours? Make it a day. | ||
**5. Are you looking for a job?** | ||
**7. Are you looking for a job?** | ||
Am I? Well, are you offering a job? If yes, I am interested. :D | ||
**6. Why should I give a shit?** | ||
**8. Why should I give a shit?** | ||
@@ -175,0 +184,0 @@ You shouldn't. But here's my email in case you do: **thecodrr[at]protonmail.com**. Don't worry, I don't bite. |
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
188
12496
6
87