What is dprint-node?
dprint-node is a Node.js wrapper for the dprint code formatting tool. It allows developers to format code using dprint's powerful and extensible formatting capabilities directly within a Node.js environment.
What are dprint-node's main functionalities?
Format Code
This feature allows you to format code using dprint. In this example, TypeScript code is formatted to follow dprint's formatting rules.
const dprint = require('dprint-node');
const code = 'function helloWorld() {console.log("Hello, world!");}';
const formattedCode = dprint.format('typescript', code);
console.log(formattedCode);
Configure Formatter
This feature allows you to configure the formatter with custom settings. In this example, the TypeScript formatter is configured to use a line width of 80 characters and spaces instead of tabs.
const dprint = require('dprint-node');
const config = {
'typescript': {
'lineWidth': 80,
'useTabs': false
}
};
const code = 'function helloWorld() {console.log("Hello, world!");}';
const formattedCode = dprint.format('typescript', code, config);
console.log(formattedCode);
Format Multiple Files
This feature allows you to format multiple files. In this example, it reads TypeScript files, formats them, and writes the formatted code back to the files.
const dprint = require('dprint-node');
const fs = require('fs');
const files = ['file1.ts', 'file2.ts'];
files.forEach(file => {
const code = fs.readFileSync(file, 'utf8');
const formattedCode = dprint.format('typescript', code);
fs.writeFileSync(file, formattedCode);
});
Other packages similar to dprint-node
prettier
Prettier is an opinionated code formatter that supports many languages and integrates with most editors. It automatically formats code to ensure a consistent style across your project. Compared to dprint-node, Prettier is more widely adopted and has a larger community, but dprint-node offers more extensibility and customization options.
eslint
ESLint is a tool for identifying and fixing problems in JavaScript code. It can be configured to enforce a consistent coding style and can also automatically fix some issues. While ESLint is primarily a linter, it also has formatting capabilities through plugins. Compared to dprint-node, ESLint is more focused on code quality and linting, whereas dprint-node is focused purely on formatting.
clang-format
Clang-Format is a tool to format C, C++, Java, JavaScript, Objective-C, Protobuf, and other languages. It is part of the LLVM project and is highly configurable. Compared to dprint-node, Clang-Format is more specialized for C-family languages and is less flexible in terms of extensibility and customization.
dprint-node
A node API for the dprint TypeScript and JavaScript code formatter. It's written in Rust for blazing fast speed.
Usage
Pass a file path and the code to format to dprint.format
.
const dprint = require('dprint-node');
dprint.format(filePath, code);
You can also optionally pass some configuration options as an object to the third parameter. All of the options listed here are supported.
dprint.format(filePath, code, {
lineWidth: 100
});
Benchmark
$ node bench.js
prettier 215.07 opts/sec (mean: 4.65ms, stddev: 7.656ms, 50 samples)
dprint 4,655.99 opts/sec (mean: 0.215ms, stddev: 0.077ms, 50 samples)