blob: 2dec379bf5fcd7381f0b80d820aa4b535eb2e7e3 [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
Paul Lewis75090cf2019-10-25 14:13:11 +01008var _isNumber2 = _interopRequireDefault(require("lodash/isNumber"));
9
10var _isString2 = _interopRequireDefault(require("lodash/isString"));
Yang Guo4fd355c2019-09-19 10:59:03 +020011
12var _stringWidth = _interopRequireDefault(require("string-width"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16const alignments = ['left', 'right', 'center'];
17/**
18 * @param {string} subject
19 * @param {number} width
20 * @returns {string}
21 */
22
23const alignLeft = (subject, width) => {
24 return subject + ' '.repeat(width);
25};
26/**
27 * @param {string} subject
28 * @param {number} width
29 * @returns {string}
30 */
31
32
33const alignRight = (subject, width) => {
34 return ' '.repeat(width) + subject;
35};
36/**
37 * @param {string} subject
38 * @param {number} width
39 * @returns {string}
40 */
41
42
43const alignCenter = (subject, width) => {
44 let halfWidth;
45 halfWidth = width / 2;
46
Tim van der Lippeb97da6b2021-02-12 14:32:53 +000047 if (width % 2 === 0) {
Yang Guo4fd355c2019-09-19 10:59:03 +020048 return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth);
49 } else {
50 halfWidth = Math.floor(halfWidth);
51 return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth + 1);
52 }
53};
54/**
55 * Pads a string to the left and/or right to position the subject
56 * text in a desired alignment within a container.
57 *
58 * @param {string} subject
59 * @param {number} containerWidth
60 * @param {string} alignment One of the valid options (left, right, center).
61 * @returns {string}
62 */
63
64
65const alignString = (subject, containerWidth, alignment) => {
Paul Lewis75090cf2019-10-25 14:13:11 +010066 if (!(0, _isString2.default)(subject)) {
Yang Guo4fd355c2019-09-19 10:59:03 +020067 throw new TypeError('Subject parameter value must be a string.');
68 }
69
Paul Lewis75090cf2019-10-25 14:13:11 +010070 if (!(0, _isNumber2.default)(containerWidth)) {
Yang Guo4fd355c2019-09-19 10:59:03 +020071 throw new TypeError('Container width parameter value must be a number.');
72 }
73
74 const subjectWidth = (0, _stringWidth.default)(subject);
75
76 if (subjectWidth > containerWidth) {
77 // console.log('subjectWidth', subjectWidth, 'containerWidth', containerWidth, 'subject', subject);
78 throw new Error('Subject parameter value width cannot be greater than the container width.');
79 }
80
Paul Lewis75090cf2019-10-25 14:13:11 +010081 if (!(0, _isString2.default)(alignment)) {
Yang Guo4fd355c2019-09-19 10:59:03 +020082 throw new TypeError('Alignment parameter value must be a string.');
83 }
84
Paul Lewis75090cf2019-10-25 14:13:11 +010085 if (!alignments.includes(alignment)) {
Yang Guo4fd355c2019-09-19 10:59:03 +020086 throw new Error('Alignment parameter value must be a known alignment parameter value (left, right, center).');
87 }
88
89 if (subjectWidth === 0) {
90 return ' '.repeat(containerWidth);
91 }
92
93 const availableWidth = containerWidth - subjectWidth;
94
95 if (alignment === 'left') {
96 return alignLeft(subject, availableWidth);
97 }
98
99 if (alignment === 'right') {
100 return alignRight(subject, availableWidth);
101 }
102
103 return alignCenter(subject, availableWidth);
104};
105
106var _default = alignString;
107exports.default = _default;
108//# sourceMappingURL=alignString.js.map