cli-util
Version:
Utility functions for the cli toolkit
16 lines (14 loc) • 425 B
JavaScript
var repeat = require('./repeat');
/**
* Pad a string with leading or trailing whitespace.
*
* Default is to add trailing whitespace.
*
* @param str The string to pad.
* @param width The target character width.
* @param leading Pad before the string.
*/
module.exports = function pad(str, width, leading) {
var len = Math.max(0, width - str.length);
return leading ? repeat(len) + str : str + repeat(len);
}