@ 文件夹/ 已读目录

递相读取一目录,快活快活.
请考虑跟随这个项目的作者, 乔恩·斯林克特,并考虑主演该项目以显示你的:心:和支持.
安装
$ npm install --save @folder/readdir
为什么使用@ 文件夹/ readdir 而不使用其他的 lib ?
- 这是 快点燃。 。 。 .
- 很简单, 直截了当的 API 和直觉 选项 用于高级使用案例.
- 可选地返回文件对象的数组( extends node.js 本地) s. 迪伦特) (中文(简体) ). 默认返回路径字符串 .
- 无依赖关系
使用量
const readdir = require('@folder/readdir');
const options = {};
// async usage
console.log(await readdir('somedir', options));
console.log(await readdir(['two', 'dirs'], options));
// sync usage
console.log(readdir.sync('somedir', options));
console.log(readdir.sync(['two' 'dirs'], options));
段落
同步和同步函数都采用相同的参数:
readdir(dir, options);
dir(字符串+阵列) - 要读取的一个或多个目录options- 见可用 选项
选项
绝对
当真实时, 绝对路径会被返回 。 否则,返回路径是相对 选项 如果定义,或给定的目录.
类型数字 : boolean
默认数字 : undefined
示例
console.log(await readdir('some/dir', { absolute: true }));
基础
创建相对路径的基础目录 .
类型数字 : string
默认: 默认通过目录作为第一个参数.
示例
const files = await readdir('some/dir', { base: 'dir' });
console.log(files);
碱性名称
当真实时, 只返回每个文件的基名 .
类型数字 : boolean
默认数字 : undefined
示例
console.log(await readdir('some/dir', { basename: true }));
深度
递归读取目录的最大文件夹深度 .
类型数字 : number
默认数字 : undefined
示例
const files = await readdir('some/dir', { depth: 2 });
console.log(files);
点
Dotfile默认包含在结果中. 过 false 忽略所有点文件。 使用 [每 [ , [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [花 [.
类型数字 : boolean
默认数字 : true
示例
const files = await readdir('.');
console.log(files);
//=> ['.DS_Store', '.git', 'LICENSE', 'README.md', 'package.json']
const files = await readdir('.', { dot: false });
console.log(files);
//=> ['LICENSE', 'README.md', 'package.json']
过滤器
类型数字 : function|string|array|regexp
默认数字 : undefined
示例
// only return file paths with "foo" somewhere in the path
console.log(await readdir('some/dir', { filter: /foo/ }));
// only return file paths without "foo" somewhere in the path
console.log(await readdir('some/dir', { filter: file => !/foo/.test(file.path) }));
跟着
跟随符号链接.
类型数字 : boolean
默认数字 : undefined
示例
console.log(await readdir('some/dir', { follow: true }));
是游戏
类型数字 : function|string|regex|array<function|string|regex>
默认数字 : undefined
示例
// only return file paths with "/.git/" somewhere in the path
console.log(await readdir('some/dir', { isMatch: /\/\.git\// }));
// only return file paths that are not inside "node_modules"
console.log(await readdir('some/dir', { isMatch: file => !file.relative.includes('node_modules') }));
// get all files that are not named .DS_Store
console.log(await readdir('some/dir', { isMatch: file => file.name !== '.DS_Store' }));
// use globs
const picomatch = require('picomatch');
const isMatch = picomatch('*/*.js');
console.log(await readdir('some/dir', { isMatch: file => isMatch(file.relative) }));
无目录
何时 true 目录被排除在结果之外.
类型数字 : boolean
默认数字 : undefined
对象
返回时 s. 迪伦特 对象而不是路径.
类型数字 : boolean
默认数字 : undefined
console.log(await readdir('some/dir', { objects: true }));
编辑器
在所有目录上调用函数 .
类型数字 : function
默认数字 : undefined
示例
const onDirectory = file => {
if (file.name === 'node_modules') {
file.recurse = false;
}
};
console.log(await readdir('some/dir', { onDirectory }));
每个
在所有目录和文件上调用函数 .
类型数字 : function
默认数字 : undefined
示例
const onEach = file => {
if (file.name === 'node_modules') {
file.recurse = false;
}
if (file.isFile() && file.name[0] === '.') {
file.keep = true;
}
};
console.log(await readdir('some/dir', { onEach }));
文件夹
在所有文件上调用函数 .
类型数字 : function
默认数字 : undefined
示例
const onFile = file => {
if (file.isFile() && file.name[0] === '.') {
file.keep = true;
}
};
console.log(await readdir('some/dir', { onFile }));
同步链接
在所有符号链接上调用函数 .
类型数字 : function
默认数字 : undefined
示例
const onSymbolicLink = file => {
// do stuff
};
console.log(await readdir('some/dir', { onSymbolicLink }));
真实路径
当真实时, 文件的真路径会在结果中返回 。 这可以与其他选项结合使用,例如 basename 或者说 relative。 。 。 .
类型数字 : boolean
默认数字 : undefined
示例
console.log(await readdir('some/dir', { realpath: true }));
递归
类型数字 : function
类型数字 : string
类型数字 : boolean
默认数字 : undefined
示例
const files = await readdir('some/dir', { recursive: true });
console.log(files);
符号链接
返回符号链接的第一个目录级别。 使用 选项 来递归跟随同音节.
类型数字 : boolean
默认数字 : undefined
示例
console.log(await readdir('some/dir', { symlinks: true }));
独一无二
只返回唯一文件路径 。 只需要当 选项 实值 true。 。 。 .
类型数字 : boolean
默认数字 : undefined
示例
console.log(await readdir('some/dir', { unique: true }));
提示( T)
使用 文件夹 在从文件系统读取的文件上操作,然后将其推入结果阵列.
这使得您可以谨慎地控制返回的文件 .
(注意,即使指定文件应作为 路径 而不是对象,所有在选项上传递的函数都会作为对象接收文件,这样您可以任意操纵返回的路径)
const readdir = require('@folder/readdir');
const isMatch = file => true;
module.exports = async (dir, options) => {
const opts = { absolute: true, recursive: true, objects: true, ...options };
const files = [];
const onFile = file => {
if (isMatch(file)) {
files.push(file);
}
};
await readdir(dir, { ...opts, onFile });
return files;
};
文件与目录
这个 onFile 选项不接收 dir 对象,只接收 Dirents (文件) 。 如果您同时需要文件和目录,您可以做以下工作:
const readdir = require('@folder/readdir');
const isMatch = file => true;
module.exports = async (dir, options) => {
const opts = { recursive: true, objects: true, ...options };
const files = [];
const onDirectory = file => {
if (file.name === 'node_modules') {
file.recurse = false;
}
};
const onFile = file => {
if (isMatch(file)) {
files.push(file);
}
};
await readdir(dir, { ...opts, onFile, onDirectory });
return files;
};
你也可以用这个 每个 (在确定文件是否会根据其他标准和选项返回之前,它会给出每个文件。) 这允许您以颗粒方式覆盖默认行为),或 打开 (它给你一个文件,它将在结果阵列中返回).
在这里,我们只显示 onEach因为它是相同的 onPush 在使用方面.
const readdir = require('@folder/readdir');
const ignore = ['node_modules', '.git'];
const isIgnored = file => ignore.includes(file.nane);
module.exports = async (dir, options) => {
const opts = { recursive: true, objects: true, ...options };
const files = [];
const onEach = file => {
if (file.isDirectory()) {
file.recurse = !isIgnored(file);
} else {
files.push(file);
}
};
await readdir(dir, { ...opts, onFile, onEach });
return files;
};
基准
(注意只有基准与 fdir 由于该图书馆声称最快,因此列入此处
要运行基准你自己, 你需要CD进入 bench 文件夹并运行 $ npm i运行 recursive-large 最后的基准,在你运行它们之前 bench/fixtures 和做( 做) $ npm i。 。 。 .
光谱
- CPU: 英特尔 i9-9980HK 2.4GHz
- 核心:16(8个物理)
- 内存: 64GB
- 磁盘:苹果APLE SSD AP2048N 1864GB NVMe (PCIe x4)
- OS: macOS macOS 大苏尔 (达文)
- 内核: 20.3.0 x64
- 节点:v15.14.0
- V8: 8.6.395.17-节点28
# single directory (~5-10 files)
@folder/readdir x 24,938 ops/sec (124,693 runs sampled)
fdir x 24,771 ops/sec (123,858 runs sampled)
# recursive ~220 files
@folder/readdir x 1,915 ops/sec (9,576 runs sampled)
fdir x 1,850 ops/sec (9,253 runs sampled)
# recursive ~2,700 files
@folder/readdir x 155 ops/sec (780 runs sampled)
fdir x 145 ops/sec (730 runs sampled)
# recursive ~57,200 files (just gatsby!)
@folder/readdir x 11 ops/sec (57 runs sampled)
fdir x 10 ops/sec (54 runs sampled)
关于
捐款
拉出请求和星星总是受欢迎的. 对于错误和特性请求, 请创建一个问题。 。 。 .
运行测试
运行和审查单元测试是熟悉图书馆及其API的好办法。 您可以使用以下命令安装依赖性并运行测试:
$ npm install && npm test
大楼文件
(此工程的 readme.md 由 动词请不要直接编辑读取器。 读取器的任何改动必须在 动词 md 读取我的模板。 )
要生成读取, 请运行以下命令 :
$ npm install -g verbose/verb#dev verb-generate-readme && verb
作者
乔恩·斯林克特
许可证
版权 2021, 乔恩·斯林克特。 。 。 . 释放 MIT 许可证。 。 。 .
此文件由 动词基因读取器,v0.8.0,于2021年4月19日查阅.