Offering a simple, lightweight text padding module that allows both left and right padding.
const padText = require('pad-text');
console.log(padText("$5.50", 7));
console.log(padText("$15.50", 7));
console.log(padText("$100.00", 7));
> " $5.50"
> " $15.50"
> "$100.00"
The first two arguments of the padText function are required. The first being the string that may be padded and the second argument being the amount of padding to introduce.
The third argument allows you to substitute which character you'd like to pad with. By default, padding is a space. This comes in handy for various use cases like when you may want to pad with a period (".").
const padText = require('pad-text');
console.log("Page" + padText(5, 5, "."));
console.log("Page" + padText(10, 5, "."));
> "Page....5"
> "Page...10"
The fourth and last argument is a boolean which represents whether the padding should be added to the start or the end of the provided string. It defaults to adding the padding to the start, but passing in a falsey value for this argument will allow you to pad on the end of the string.
const padText = require('pad-text');
console.log(padText("Loading", 15, ".", false));
console.log(padText("Loading 50%", 15, ".", false));
console.log(padText("Loading 100%", 15, ".", false));
> "Loading......"
> "Loading 50%.."
> "Loading 100%."