Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | "use strict"; |
| 2 | |
| 3 | Object.defineProperty(exports, "__esModule", { |
| 4 | value: true |
| 5 | }); |
| 6 | exports.default = void 0; |
| 7 | |
Paul Lewis | 75090cf | 2019-10-25 14:13:11 +0100 | [diff] [blame] | 8 | var _isNumber2 = _interopRequireDefault(require("lodash/isNumber")); |
| 9 | |
| 10 | var _isString2 = _interopRequireDefault(require("lodash/isString")); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 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 | |
Tim van der Lippe | b97da6b | 2021-02-12 14:32:53 +0000 | [diff] [blame] | 47 | if (width % 2 === 0) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 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) => { |
Paul Lewis | 75090cf | 2019-10-25 14:13:11 +0100 | [diff] [blame] | 66 | if (!(0, _isString2.default)(subject)) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 67 | throw new TypeError('Subject parameter value must be a string.'); |
| 68 | } |
| 69 | |
Paul Lewis | 75090cf | 2019-10-25 14:13:11 +0100 | [diff] [blame] | 70 | if (!(0, _isNumber2.default)(containerWidth)) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 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 | |
Paul Lewis | 75090cf | 2019-10-25 14:13:11 +0100 | [diff] [blame] | 81 | if (!(0, _isString2.default)(alignment)) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 82 | throw new TypeError('Alignment parameter value must be a string.'); |
| 83 | } |
| 84 | |
Paul Lewis | 75090cf | 2019-10-25 14:13:11 +0100 | [diff] [blame] | 85 | if (!alignments.includes(alignment)) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 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 |