UNPKG

670 BJavaScriptView Raw
1/*!
2 * center-align <https://github.com/jonschlinkert/center-align>
3 *
4 * Copycenter (c) 2015, Jon Schlinkert.
5 * Licensed under the MIT License.
6 */
7
8'use strict';
9
10var repeat = require('repeat-string');
11var align = require('align-text');
12
13module.exports = function(val, width) {
14 if (typeof width === 'number' && typeof val === 'string' && !/\n/.test(val)) {
15 var padding = Math.floor((width - val.length) / 2);
16 return repeat(' ', padding) + val + repeat(' ', padding);
17 }
18
19 return align(val, function(len, longest) {
20 if (typeof width === 'number') {
21 return Math.floor((width - len) / 2);
22 }
23 return Math.floor((longest - len) / 2);
24 });
25};