Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
indent-string
Advanced tools
The indent-string npm package is a utility that allows users to add indentation to the beginning of each line in a string. This can be particularly useful for formatting multi-line strings or code snippets in a more readable manner. The package provides a simple interface to specify the number of spaces or the string to use for indentation, making it a versatile tool for text manipulation in Node.js applications.
Indenting with spaces
This feature allows you to indent each line of a given string with a specified number of spaces. In the code sample, the string 'Unicorn\nRainbow' is indented by 4 spaces.
const indentString = require('indent-string');
console.log(indentString('Unicorn\nRainbow', 4));
// Output:
// Unicorn
// Rainbow
Indenting with custom string
This feature enables indenting each line of a string with a custom string instead of spaces. In the example, the string 'Unicorn\nRainbow' is indented using the '♥' character, repeated 4 times as specified.
const indentString = require('indent-string');
console.log(indentString('Unicorn\nRainbow', 4, {indent: '♥'}));
// Output:
// ♥♥♥♥Unicorn
// ♥♥♥♥Rainbow
Similar to indent-string, left-pad adds characters to the left of a string, effectively indenting it. However, left-pad is more focused on padding a single line to a specific length, rather than indenting all lines in a multi-line string.
While not directly similar, cli-boxes is a package that deals with text formatting in the terminal, offering a variety of box styles for CLI applications. It can be used in conjunction with indent-string for more complex text formatting tasks.
Indent each line in a string
$ npm install --save indent-string
var indentString = require('indent-string');
indentString('Unicorns\nRainbows', '♥', 4);
//=> ♥♥♥♥Unicorns
//=> ♥♥♥♥Rainbows
Required
Type: string
The string you want to indent.
Required
Type: string
The string to use for the indent.
Type: number
Default: 1
How many times you want indent
repeated.
$ npm install --global indent-string
$ indent-string --help
Usage
$ indent-string <string> [--indent <string>] [--count <number>]
$ cat file.txt | indent-string > indented-file.txt
Example
$ indent-string "$(printf 'Unicorns\nRainbows\n')" --indent ♥ --count 4
♥♥♥♥Unicorns
♥♥♥♥Rainbows
MIT © Sindre Sorhus
FAQs
Indent each line in a string
The npm package indent-string receives a total of 32,242,712 weekly downloads. As such, indent-string popularity was classified as popular.
We found that indent-string demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.