Lodash _.pad: Center Strings with Custom Characters
The _.pad method in the Lodash JavaScript library
centers a string within a specified target length by adding padding
characters to both its left and right sides. By default, it pads the
string with empty spaces, but developers can specify custom characters
or multi-character strings to achieve formatted output. This article
explains the syntax of _.pad, breaks down the internal
logic used to distribute custom characters evenly, and provides
practical code examples.
Syntax and Parameters
The syntax for _.pad is:
_.pad([string=''], [length=0], [chars=' '])string: The target string to center.length: The desired total length of the resulting string. If this value is less than or equal to the original string's length, Lodash returns the original string without modification.chars: The character or sequence of characters used for padding. Defaults to a standard whitespace (' ').
How _.pad Calculates
Padding
To center the string, _.pad performs the following
steps:
- Calculates Total Padding: It subtracts the original
string length from the target
length. IftotalPaddingis zero or negative, it immediately returns the original string. - Distributes Padding: The padding is split between
the left and right sides. The left side receives
Math.floor(totalPadding / 2)characters, while the right side receivesMath.ceil(totalPadding / 2)characters. If the required padding count is an odd number, the extra character goes to the right side. - Repeats and Truncates Custom Characters: Lodash
fills each side by repeating the
charsstring until it matches the required length for that side. If the custom padding sequence exceeds the required length, it is truncated to fit exactly.
Practical Examples
1. Padding with a Single Custom Character
Using a single character such as a hyphen (-) or an
asterisk (*) evenly flanks the string until the desired
length is reached:
const _ = require('lodash');
// Target length: 10, String length: 4 -> Total padding: 6 (3 left, 3 right)
console.log(_.pad('code', 10, '-'));
// Output: "---code---"2. Handling Odd Padding Lengths
When the total padding needed is odd, the right side receives the remaining character:
// Target length: 9, String length: 4 -> Total padding: 5 (2 left, 3 right)
console.log(_.pad('code', 9, '*'));
// Output: "**code***"3. Using Multi-Character Padding Strings
When provided with a multi-character string as the chars
argument, Lodash repeats the sequence sequentially and truncates it
where necessary:
// Target length: 12, String length: 4 -> Total padding: 8 (4 left, 4 right)
// Left side needs 4 chars: "ab" repeated -> "abab"
// Right side needs 4 chars: "ab" repeated -> "abab"
console.log(_.pad('test', 12, 'ab'));
// Output: "abtestabab"
// Target length: 11, String length: 3 -> Total padding: 8 (4 left, 4 right)
// "123" repeated to 4 chars -> "1231"
console.log(_.pad('xyz', 11, '123'));
// Output: "1231xyz1231"Summary
The _.pad method simplifies text-alignment tasks in
JavaScript by automating the math required to center text. By supplying
the optional third argument, you gain control over visual dividers,
terminal formatting, and padded identifiers without having to manually
calculate substrings or handle string repetitions.