minimal adjustments
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / table / dist / alignString.js
1 "use strict";
2
3 Object.defineProperty(exports, "__esModule", {
4   value: true
5 });
6 exports.default = void 0;
7
8 var _isNumberObject = _interopRequireDefault(require("is-number-object"));
9
10 var _isString = _interopRequireDefault(require("is-string"));
11
12 var _stringWidth = _interopRequireDefault(require("string-width"));
13
14 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16 const alignments = ['left', 'right', 'center'];
17 /**
18  * @param {string} subject
19  * @param {number} width
20  * @returns {string}
21  */
22
23 const alignLeft = (subject, width) => {
24   return subject + ' '.repeat(width);
25 };
26 /**
27  * @param {string} subject
28  * @param {number} width
29  * @returns {string}
30  */
31
32
33 const alignRight = (subject, width) => {
34   return ' '.repeat(width) + subject;
35 };
36 /**
37  * @param {string} subject
38  * @param {number} width
39  * @returns {string}
40  */
41
42
43 const alignCenter = (subject, width) => {
44   let halfWidth;
45   halfWidth = width / 2;
46
47   if (width % 2 === 0) {
48     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
65 const alignString = (subject, containerWidth, alignment) => {
66   if (!(0, _isString.default)(subject)) {
67     throw new TypeError('Subject parameter value must be a string.');
68   }
69
70   if (!(0, _isNumberObject.default)(containerWidth)) {
71     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
81   if (!(0, _isString.default)(alignment)) {
82     throw new TypeError('Alignment parameter value must be a string.');
83   }
84
85   if (!alignments.includes(alignment)) {
86     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
106 var _default = alignString;
107 exports.default = _default;
108 //# sourceMappingURL=alignString.js.map