3 Object.defineProperty(exports, "__esModule", {
6 exports.default = void 0;
8 var _isNumber2 = _interopRequireDefault(require("lodash/isNumber"));
10 var _isString2 = _interopRequireDefault(require("lodash/isString"));
12 var _stringWidth = _interopRequireDefault(require("string-width"));
14 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16 const alignments = ['left', 'right', 'center'];
18 * @param {string} subject
19 * @param {number} width
23 const alignLeft = (subject, width) => {
24 return subject + ' '.repeat(width);
27 * @param {string} subject
28 * @param {number} width
33 const alignRight = (subject, width) => {
34 return ' '.repeat(width) + subject;
37 * @param {string} subject
38 * @param {number} width
43 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);
51 return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth + 1);
55 * Pads a string to the left and/or right to position the subject
56 * text in a desired alignment within a container.
58 * @param {string} subject
59 * @param {number} containerWidth
60 * @param {string} alignment One of the valid options (left, right, center).
65 const alignString = (subject, containerWidth, alignment) => {
66 if (!(0, _isString2.default)(subject)) {
67 throw new TypeError('Subject parameter value must be a string.');
70 if (!(0, _isNumber2.default)(containerWidth)) {
71 throw new TypeError('Container width parameter value must be a number.');
74 const subjectWidth = (0, _stringWidth.default)(subject);
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.');
81 if (!(0, _isString2.default)(alignment)) {
82 throw new TypeError('Alignment parameter value must be a string.');
85 if (!alignments.includes(alignment)) {
86 throw new Error('Alignment parameter value must be a known alignment parameter value (left, right, center).');
89 if (subjectWidth === 0) {
90 return ' '.repeat(containerWidth);
93 const availableWidth = containerWidth - subjectWidth;
95 if (alignment === 'left') {
96 return alignLeft(subject, availableWidth);
99 if (alignment === 'right') {
100 return alignRight(subject, availableWidth);
103 return alignCenter(subject, availableWidth);
106 var _default = alignString;
107 exports.default = _default;
108 //# sourceMappingURL=alignString.js.map