1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | var repeat = require('repeat-string');
|
11 | var align = require('align-text');
|
12 |
|
13 | module.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 | };
|