3 Object.defineProperty(exports, "__esModule", {
7 var _lodash = require('lodash');
9 var _lodash2 = _interopRequireDefault(_lodash);
11 var _stringWidth = require('string-width');
13 var _stringWidth2 = _interopRequireDefault(_stringWidth);
15 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17 const alignments = ['left', 'right', 'center'];
20 * @param {string} subject
21 * @param {number} width
24 const alignLeft = (subject, width) => {
25 return subject + ' '.repeat(width);
29 * @param {string} subject
30 * @param {number} width
33 const alignRight = (subject, width) => {
34 return ' '.repeat(width) + subject;
38 * @param {string} subject
39 * @param {number} width
42 const alignCenter = (subject, width) => {
45 halfWidth = width / 2;
47 if (halfWidth % 2 === 0) {
48 return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth);
50 halfWidth = Math.floor(halfWidth);
52 return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth + 1);
57 * Pads a string to the left and/or right to position the subject
58 * text in a desired alignment within a container.
60 * @param {string} subject
61 * @param {number} containerWidth
62 * @param {string} alignment One of the valid options (left, right, center).
66 exports.default = (subject, containerWidth, alignment) => {
67 if (!_lodash2.default.isString(subject)) {
68 throw new TypeError('Subject parameter value must be a string.');
71 if (!_lodash2.default.isNumber(containerWidth)) {
72 throw new TypeError('Container width parameter value must be a number.');
75 const subjectWidth = (0, _stringWidth2.default)(subject);
77 if (subjectWidth > containerWidth) {
78 // console.log('subjectWidth', subjectWidth, 'containerWidth', containerWidth, 'subject', subject);
80 throw new Error('Subject parameter value width cannot be greater than the container width.');
83 if (!_lodash2.default.isString(alignment)) {
84 throw new TypeError('Alignment parameter value must be a string.');
87 if (alignments.indexOf(alignment) === -1) {
88 throw new Error('Alignment parameter value must be a known alignment parameter value (left, right, center).');
91 if (subjectWidth === 0) {
92 return ' '.repeat(containerWidth);
95 const availableWidth = containerWidth - subjectWidth;
97 if (alignment === 'left') {
98 return alignLeft(subject, availableWidth);
101 if (alignment === 'right') {
102 return alignRight(subject, availableWidth);
105 return alignCenter(subject, availableWidth);