TUtils
TUtils 提供了大量 Node.js 开发必备的工具组件,比如命令行、网络库、文件操作等。
特性
- 【强大】30+ 个组件(可单独引用),顶 140+ 个 NPM 依赖;
- 【轻量】所有源码不超过 300K,无外部依赖;
- 【性能】几乎所有组件在性能上稳超 NPM 社区组件;
- 【稳定】大量的单元测试(覆盖率 90%+)和生产实践,测试用例和社区同步;
- 【文档】完美全中文文档,VSCode/WebStorm/VS 等可智能提示。
为什么要重做轮子
- NPM 组件为了照顾所有用户的需求,往往提供很多冗余的功能,增加了复杂度、降级了性能,而 TUtils 会帮用户决定,只保留一种性价比最高的方案;
- NPM 组件风格不一,命名无规律,每次用都要翻文档。TUtils 所有组件遵循同一规范,可以举一反三,过目不忘;
- NPM 上有大量功能相似的组件被同时依赖,写个 Hello World 都要依赖几十个包;
- NPM 上有很多历史遗留代码(比如为了支持低版本 Node),TUtils 只考虑 Node 最新稳定版(LTS),以确保既轻量又高效;
- 很多 NPM 组件对中日韩文支持不好。
NPM 社区是开放的,也产生了很多优秀的组件;TUtils 则相对封闭,以保障组件的质量和体验。这类似于开放的安卓和封闭的苹果之间的区别。
用法
使用 NPM 安装:
npm install tutils --save
注意:TUtils 基于 ES2018 编写,只支持 Node v10.12+,部分组件可能不兼容低版本。
可以一行代码引用所有组件:
const tutils = require("tutils")
也可以单独引入某个组件,比如:
const fs = require("tutils/fileSystemSync")
API 文档
点击查看生成的 API 文档
组件列表
目录
- 文件操作
- 网络
- 进程
- 命令行
- 编码、解析
- 生成代码
- 异步
- 其它
文件操作
fileSystem
const { FileSystem } = require("tutils/fileSystem")
const fs = new FileSystem()
await fs.writeFile("foo.txt", "Hello world")
npm 组件名 |
TUtils 对应的组件函数 |
graceful-fs |
FileSystem |
fs-extra |
FileSystem |
graceful-fs-extra |
FileSystem |
mkdirp |
FileSystem.createDir |
make-dir |
FileSystem.createDir |
cp-file |
FileSystem.copFile |
cpy |
FileSystem.copFile |
ncp |
FileSystem.copyDir |
copy-files-tree |
FileSystem.copyDir |
clean-dir |
FileSystem.cleanDir |
delete |
FileSystem.deleteDir |
del |
FileSystem.deleteDir |
rimraf |
FileSystem.deleteDir , FileSystem.deleteFile |
node-glob |
FileSystem.glob |
fast-glob |
FileSystem.glob |
globby |
FileSystem.glob |
glob-all |
FileSystem.glob |
walker |
FileSystem.walk |
walkdir |
FileSystem.walk |
move-file |
FileSystem.moveFile |
path-exists |
FileSystem.existsFile , FileSystem.existsDir |
fileSystemSync
const fs = require("tutils/fileSystemSync")
fs.writeFile("foo.txt", "Hello world")
memoryFileSystem
const { MemoryFileSystem } = require("tutils/memoryFileSystem")
const fs = new MemoryFileSystem()
npm 组件名 |
TUtils 对应的组件函数 |
memory-fs |
MemoryFileSystem |
mem-fs |
MemoryFileSystem |
memfs |
MemoryFileSystem |
fileSystemWatcher
const { FileSystemWatcher } = require("tutils/fileSystemWatcher")
const watcher = new FileSystemWatcher()
watcher.on("change", path => { console.log("Changed", path) })
watcher.on("delete", path => { console.log("Deleted", path) })
watcher.on("create", path => { console.log("Created", path) })
watcher.add(process.cwd(), () => { console.log("Start Watching...") })
npm 组件名 |
TUtils 对应的组件函数 |
chokidar |
FileSystemWatcher |
gaze |
FileSystemWatcher |
sane |
FileSystemWatcher |
watchpack |
FileSystemWatcher |
matcher
const matcher = require("tutils/matcher")
matcher.match("/path.js", "/*.js")
npm 组件名 |
TUtils 对应的组件函数 |
glob |
Matcher |
node-glob |
Matcher |
matcher |
Matcher , match |
minimatch |
match |
micromatch |
match |
anymatch |
match |
glob-base |
Matcher.base |
glob-parent |
Matcher.base |
is-glob |
isGlob |
path
const path = require("tutils/path")
npm 组件名 |
TUtils 对应的组件函数 |
rename-extension |
setExt |
changefilesname |
setName |
is-relative |
isAbsolutePath |
is-absolute |
isAbsolutePath |
normalize-path |
normalizePath |
path-is-inside |
containsPath |
contains-path |
containsPath |
网络
request
const request = require("tutils/request")
const html = await request.request("https://www.baidu.com")
console.log(html)
npm 组件名 |
TUtils 对应的组件函数 |
request |
request |
got |
request |
axios |
request |
wreck |
request |
cookiejar |
CookieJar |
touch-cookiejar |
CookieJar |
httpServer
const httpServer = require("tutils/httpServer")
const server = new httpServer.HTTPServer({}, (req, res) => {
res.end(req.href)
})
server.listen(8080)
npm 组件名 |
TUtils 对应的组件函数 |
express |
HTTPServer |
koa |
HTTPServer |
http-server |
HTTPServer |
body-parser |
HTTPRequest.body |
cookie-parser |
HTTPRequest.cookies |
cookie-sessions |
HTTPServer.sessions |
multipart |
HTTPRequest.files |
webSocket
const webSocket = require("tutils/webSocket")
const server = new webSocket.WebSocketServer("ws://localhost:8080")
server.on("connection", ws => {
ws.send("hello")
})
const client = new webSocket.WebSocket("ws://localhost:8080")
client.on("message", data => {
console.log(data)
client.send("hello")
})
npm 组件名 |
TUtils 对应的组件函数 |
ws |
WebSocket |
websocket-driver |
WebSocket |
url
const url = require("tutils/url")
npm 组件名 |
TUtils 对应的组件函数 |
resolve-pathname |
resolveURL |
relative-url |
relativeURL |
normalize-url |
normalizeURL |
is-relative-url |
isAbsoluteURL |
get-urls |
replaceURL |
linkify-it |
replaceURL |
进程
process
const process = require("tutils/process")
npm 组件名 |
TUtils 对应的组件函数 |
cross-spawn |
exec |
execa |
exec |
open |
open |
signal-exit |
onExit , offExit |
vm
const vm = require("tutils/vm")
vm.runInVM(`var x = 1`)
workerPool
const { WorkerPool } = require("tutils/workerPool")
const pool = new WorkerPool("./worker.js")
await pool.exec("sum", 1, 2)
npm 组件名 |
TUtils 对应的组件函数 |
worker-threads-pool |
WorkerPool |
命令行
ansi
const ansi = require("tutils/ansi")
npm 组件名 |
TUtils 对应的组件函数 |
ansi-color |
color |
ansi-colors |
color |
chalk |
bold , color , backgroundColor |
kleur |
bold , color , backgroundColor |
ansi-style-codes |
ANSIColor |
ansi-regex |
ansiCodeRegExp |
strip-ansi |
removeANSICodes |
strip-color |
removeANSICodes |
ansi-stripper |
removeANSICodes |
cli-truncate |
truncateString |
ansi-color-table |
formatTable |
chunk-text |
wrapString |
wrap-ansi |
wrapString |
cli-columns |
formatList |
console-log-tree |
formatTree |
ansi-color-table |
formatTable |
columnify |
formatTable |
formatter-codeframe |
formatCodeFrame |
ansicolor |
ansiToHTML |
ansi-to-html |
ansiToHTML |
ansi-html |
ansiToHTML |
stream-ansi2html |
ansiToHTML |
string-width |
getStringWidth |
monospace-char-width |
getCharWidth |
commandLine
const commandLine = require("tutils/commandLine")
npm 组件名 |
TUtils 对应的组件函数 |
show-terminal-cursor |
showCursor |
hide-terminal-cursor |
hideCursor |
cli-cursor |
showCursor , hideCursor |
restore-cursor |
showCursor |
meow |
parseCommandLineArguments , formatCommandLineOptions |
yargs |
parseCommandLineArguments , formatCommandLineOptions |
clear-cli |
clear |
node-console-input |
input |
logger
const { Logger } = require("tutils/logger")
const logger = new Logger()
logger.error("Hello world")
npm 组件名 |
TUtils 对应的组件函数 |
logger |
Logger |
fancy-log |
Logger |
编码、解析
base64
const base64 = require("tutils/base64")
npm 组件名 |
TUtils 对应的组件函数 |
js-base64 |
encodeBase64 , decodeBase64 |
data-urlse |
encodeDataURI , decodeDataURI |
crypto
const crypto = require("tutils/crypto")
npm 组件名 |
TUtils 对应的组件函数 |
md5 |
md5 |
sha1 |
sha1 |
html
const html = require("tutils/html")
npm 组件名 |
TUtils 对应的组件函数 |
ent |
encodeHTML , decodeHTML |
entities |
encodeHTML , decodeHTML |
he |
encodeHTML , decodeHTML |
html-entities |
encodeHTML |
decode-html |
decodeHTML |
html-decoder |
decodeHTML |
html-entity-decoder |
decodeHTML |
html-encoder-decoder |
encodeHTML , decodeHTML |
html-parser |
parseHTML |
js
const js = require("tutils/js")
npm 组件名 |
TUtils 对应的组件函数 |
js-string-escape |
encodeJS |
json
const json = require("tutils/json")
npm 组件名 |
TUtils 对应的组件函数 |
strip-json-comments |
normalizeJSON |
load-json-file |
readJSON |
write-json-file |
writeJSON |
css
const css = require("tutils/css")
npm 组件名 |
TUtils 对应的组件函数 |
cssesc |
encodeCSS |
esm
const { transformESModuleToCommonJS } = require("tutils/esm")
npm 组件名 |
TUtils 对应的组件函数 |
esm |
transformESModuleToCommonJS |
生成代码
lineColumn
const lineColumn = require("tutils/lineColumn")
npm 组件名 |
TUtils 对应的组件函数 |
find-line-column |
indexToLineColumn , lineColumnToIndex |
lines-and-columns |
LineMap |
sourceMap
const sourceMap = require("tutils/sourceMap")
const map = new sourceMap.SourceMapBuilder({ })
map.addMapping(1, 2)
const output = map.toJSON()
npm 组件名 |
TUtils 对应的组件函数 |
source-map |
SourceMapBuilder |
convert-source-map |
SourceMapBuilder |
merge-source-map |
SourceMapBuilder.applySourceMap |
textWriter
const { TextWriter, SourceMapTextWriter } = require("tutils/textWriter")
npm 组件名 |
TUtils 对应的组件函数 |
string-builder |
TextWriter |
source-list-map |
SourceMapTextWriter |
fast-sourcemap-concat |
SourceMapTextWriter |
textDocument
const { TextDocument, replace, insert } = require("tutils/textDocument")
const data = replace({
content: "var a = 1",
path: "source.js"
}, /\bvar\b/g, "let")
console.log(data.content)
console.log(data.sourceMap)
npm 组件名 |
TUtils 对应的组件函数 |
magic-string |
TextDocument |
异步
asyncQueue
const { AsyncQueue } = require("tutils/asyncQueue")
const asyncQueue = new AsyncQueue()
await asyncQueue.then(async () => { })
await asyncQueue.then(async () => { })
await asyncQueue.then(async () => { })
npm 组件名 |
TUtils 对应的组件函数 |
asyncqueue |
AsyncQueue |
node-asyncqueue |
AsyncQueue |
deferred
const { Deferred } = require("tutils/deferred")
const deferred = new Deferred()
deferred.reject()
setTimeout(() => {
deferred.resolve()
}, 2000)
await deferred
npm 组件名 |
TUtils 对应的组件函数 |
deferred |
Deferred |
eventEmitter
const { EventEmitter } = require("tutils/eventEmitter")
const events = new EventEmitter()
events.on("error", data => console.log(data))
await events.emit("error", "hello")
npm 组件名 |
TUtils 对应的组件函数 |
events |
EventEmitter |
tappable |
EventEmitter |
其它
misc
const misc = require("tutils/misc")
npm 组件名 |
TUtils 对应的组件函数 |
strip-bom |
stripBOM |
sorted-array-type |
insertSorted |
throttle-debounce |
throttle |
escape-string-regexp |
escapeRegExp |
format-date |
formatDate |
dateformat |
formatDate |
Moment.js |
formatDate |
node-dateformate |
formatDate |
pretty-hrtime |
formatHRTime |
pretty-time |
formatHRTime |
pretty-bytes |
formatSize |
pretty-size |
formatSize |