What is pad?
The 'pad' npm package is a simple utility for padding strings to a specified length with a given character. It is useful for formatting text output, ensuring consistent string lengths, and aligning text in console applications or logs.
What are pad's main functionalities?
Left Padding
Pads the string 'Hello' with spaces on the left to make its total length 10 characters.
const pad = require('pad');
console.log(pad(10, 'Hello')); // ' Hello'
Right Padding
Pads the string 'Hello' with spaces on the right to make its total length 10 characters.
const pad = require('pad');
console.log(pad.right(10, 'Hello')); // 'Hello '
Custom Character Padding
Pads the string 'Hello' with dots on the left to make its total length 10 characters.
const pad = require('pad');
console.log(pad(10, 'Hello', '.')); // '.....Hello'
Other packages similar to pad
left-pad
The 'left-pad' package is a popular utility for left-padding strings with a specified character. It is similar to 'pad' but focuses solely on left-padding functionality.
string-pad
The 'string-pad' package provides both left and right padding functionalities, similar to 'pad'. It allows padding with custom characters and offers more flexibility in padding options.
pad-left
The 'pad-left' package is another utility for left-padding strings. It is similar to 'left-pad' and 'pad' but focuses exclusively on left-padding.
Node.js pad
Node Pad is a simple and elegant function to pad strings in both left and right directions.
Usage
pad('pad', 5)
pad(5, 'pad')
pad('pad', 5, '+')
pad(5, 'pad', '+')
Options
Options are provided as a third argument and are all optional. A string argument
it is interpreted as the "char" option. Accepted options include:
char
(string)
The character used to fill the gap.colors
(boolean)
Ajust to hidden terminal color characters, you may also use
require 'pad/lib/colors'
to avoid passing this option.strip
(boolean)
Remove characters from text if length smaller than text length, default to
"false".fixed_width
(boolean)
An optimization option to disable the usage of the wcwdith package to handle
the discovery of characters using more than one column for display.
one column to displaywcwidth_options
(object)
Options passed to the wcwidth package used to calculate the display width of
characters using more than one column.
Left padding: pad(length, text, [options])
Left padding occurs when the first argument is a number and the second
argument is a string.
var pad = require('pad');
pad(5, 'pad', '-').should.eql('-pad');
Right padding: pad(text, length, [options])
Right padding occurs when the first argument is a string and the second
argument is a number.
var pad = require('pad');
pad('pad', 6).should.eql('pad ');
Installing
Starting with version 1.1.0, Node pad rely on Node.js 4.0.0 or more recent. Stick to version 1.0.x if using an older version of Node.js.
Via npm:
npm install pad
Via git (or downloaded tarball), copy or link the project from a discoverable Node directory:
git clone http://github.com/wdavidw/node-pad.git
Testing
Clone the repo, install the development dependencies and run the suite:
git clone http://github.com/wdavidw/node-pad.git .
npm install
make test