.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / node_modules / table / dist / alignString.js
1 'use strict';
2
3 Object.defineProperty(exports, "__esModule", {
4   value: true
5 });
6
7 var _lodash = require('lodash');
8
9 var _lodash2 = _interopRequireDefault(_lodash);
10
11 var _stringWidth = require('string-width');
12
13 var _stringWidth2 = _interopRequireDefault(_stringWidth);
14
15 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
17 const alignments = ['left', 'right', 'center'];
18
19 /**
20  * @param {string} subject
21  * @param {number} width
22  * @returns {string}
23  */
24 const alignLeft = (subject, width) => {
25   return subject + ' '.repeat(width);
26 };
27
28 /**
29  * @param {string} subject
30  * @param {number} width
31  * @returns {string}
32  */
33 const alignRight = (subject, width) => {
34   return ' '.repeat(width) + subject;
35 };
36
37 /**
38  * @param {string} subject
39  * @param {number} width
40  * @returns {string}
41  */
42 const alignCenter = (subject, width) => {
43   let halfWidth;
44
45   halfWidth = width / 2;
46
47   if (halfWidth % 2 === 0) {
48     return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth);
49   } else {
50     halfWidth = Math.floor(halfWidth);
51
52     return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth + 1);
53   }
54 };
55
56 /**
57  * Pads a string to the left and/or right to position the subject
58  * text in a desired alignment within a container.
59  *
60  * @param {string} subject
61  * @param {number} containerWidth
62  * @param {string} alignment One of the valid options (left, right, center).
63  * @returns {string}
64  */
65
66 exports.default = (subject, containerWidth, alignment) => {
67   if (!_lodash2.default.isString(subject)) {
68     throw new TypeError('Subject parameter value must be a string.');
69   }
70
71   if (!_lodash2.default.isNumber(containerWidth)) {
72     throw new TypeError('Container width parameter value must be a number.');
73   }
74
75   const subjectWidth = (0, _stringWidth2.default)(subject);
76
77   if (subjectWidth > containerWidth) {
78     // console.log('subjectWidth', subjectWidth, 'containerWidth', containerWidth, 'subject', subject);
79
80     throw new Error('Subject parameter value width cannot be greater than the container width.');
81   }
82
83   if (!_lodash2.default.isString(alignment)) {
84     throw new TypeError('Alignment parameter value must be a string.');
85   }
86
87   if (alignments.indexOf(alignment) === -1) {
88     throw new Error('Alignment parameter value must be a known alignment parameter value (left, right, center).');
89   }
90
91   if (subjectWidth === 0) {
92     return ' '.repeat(containerWidth);
93   }
94
95   const availableWidth = containerWidth - subjectWidth;
96
97   if (alignment === 'left') {
98     return alignLeft(subject, availableWidth);
99   }
100
101   if (alignment === 'right') {
102     return alignRight(subject, availableWidth);
103   }
104
105   return alignCenter(subject, availableWidth);
106 };