center-align
Version:
Center-align the text in a string.
26 lines (21 loc) • 670 B
JavaScript
/*!
* center-align <https://github.com/jonschlinkert/center-align>
*
* Copycenter (c) 2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
;
var repeat = require('repeat-string');
var align = require('align-text');
module.exports = function(val, width) {
if (typeof width === 'number' && typeof val === 'string' && !/\n/.test(val)) {
var padding = Math.floor((width - val.length) / 2);
return repeat(' ', padding) + val + repeat(' ', padding);
}
return align(val, function(len, longest) {
if (typeof width === 'number') {
return Math.floor((width - len) / 2);
}
return Math.floor((longest - len) / 2);
});
};