
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
fixed-width-string
Advanced tools
Fit a string to the fixed width (length) with aware of ANSI escape codes.
Features:
$ npm install fixed-width-string
var fixedWidthString = require('fixed-width-string');
console.log(fixedWidthString('test', 10));
// "test "
console.log(fixedWidthString('very long string', 10));
// "very long…"
console.log(fixedWidthString('very long string', { align: 'right' }));
// "…ng string"
var chalk = require('chalk');
console.log(fixedWidthString(chalk.green('hello') + ' ' + chalk.red('world'), 8));
// "\u001b[32mhello\u001b[39m \u001b[31mw…\u001b[39m"
//
// but in terminal you'll see colored
// "hello w…"
fixedWidthString(str, width[, options])
String to padding with.
Default: ' '
(one space)
console.log(fixedWidthString('test', 10));
// "test "
console.log(fixedWidthString('test', 10, { padding: '.' }));
// "test......"
Side to align.
Values: 'left'
or 'right'
Default: 'left'
console.log(fixedWidthString('test', 10));
// "test "
console.log(fixedWidthString('test', 10, { align: 'right' }));
// " test"
String to be placed when string is truncating.
Default: '…'
Note: To omit ellipsis, pass an empty string or falsy value for the option.
console.log(fixedWidthString('hello world', 8));
// "hello w…"
console.log(fixedWidthString('hello world', 8, { ellipsis: '~' }));
// "hello w~"
console.log(fixedWidthString('hello world', 8, { ellipsis: '...', align: 'right' }));
// "...world"
console.log(fixedWidthString('hello world', 8, { ellipsis: false, align: 'right' }));
// "lo world"
Side should be truncated.
Values: 'left'
or 'right'
Default: 'right'
when align: 'left'
or 'left'
when align: 'right'
console.log(fixedWidthString('hello world', 8));
// "hello w…"
console.log(fixedWidthString('hello world', 8, { truncate: 'left' }));
// "…o world"
console.log(fixedWidthString('hello world', 8, { align: 'right' }));
// "hello w…"
console.log(fixedWidthString('hello world', 8, { align: 'right', truncate: 'left' }));
// "…o world"
MIT
2.0.0 (2020-05-15)
FAQs
Terminal ANSI-aware string fit to fixed width
The npm package fixed-width-string receives a total of 2,772 weekly downloads. As such, fixed-width-string popularity was classified as popular.
We found that fixed-width-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
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.