|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
-
- var _lodash = _interopRequireDefault(require("lodash"));
-
- var _stringWidth = _interopRequireDefault(require("string-width"));
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- const alignments = ['left', 'right', 'center'];
- /**
- * @param {string} subject
- * @param {number} width
- * @returns {string}
- */
-
- const alignLeft = (subject, width) => {
- return subject + ' '.repeat(width);
- };
- /**
- * @param {string} subject
- * @param {number} width
- * @returns {string}
- */
-
-
- const alignRight = (subject, width) => {
- return ' '.repeat(width) + subject;
- };
- /**
- * @param {string} subject
- * @param {number} width
- * @returns {string}
- */
-
-
- const alignCenter = (subject, width) => {
- let halfWidth;
- halfWidth = width / 2;
-
- if (halfWidth % 2 === 0) {
- return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth);
- } else {
- halfWidth = Math.floor(halfWidth);
- return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth + 1);
- }
- };
- /**
- * Pads a string to the left and/or right to position the subject
- * text in a desired alignment within a container.
- *
- * @param {string} subject
- * @param {number} containerWidth
- * @param {string} alignment One of the valid options (left, right, center).
- * @returns {string}
- */
-
-
- const alignString = (subject, containerWidth, alignment) => {
- if (!_lodash.default.isString(subject)) {
- throw new TypeError('Subject parameter value must be a string.');
- }
-
- if (!_lodash.default.isNumber(containerWidth)) {
- throw new TypeError('Container width parameter value must be a number.');
- }
-
- const subjectWidth = (0, _stringWidth.default)(subject);
-
- if (subjectWidth > containerWidth) {
- // console.log('subjectWidth', subjectWidth, 'containerWidth', containerWidth, 'subject', subject);
- throw new Error('Subject parameter value width cannot be greater than the container width.');
- }
-
- if (!_lodash.default.isString(alignment)) {
- throw new TypeError('Alignment parameter value must be a string.');
- }
-
- if (alignments.indexOf(alignment) === -1) {
- throw new Error('Alignment parameter value must be a known alignment parameter value (left, right, center).');
- }
-
- if (subjectWidth === 0) {
- return ' '.repeat(containerWidth);
- }
-
- const availableWidth = containerWidth - subjectWidth;
-
- if (alignment === 'left') {
- return alignLeft(subject, availableWidth);
- }
-
- if (alignment === 'right') {
- return alignRight(subject, availableWidth);
- }
-
- return alignCenter(subject, availableWidth);
- };
-
- var _default = alignString;
- exports.default = _default;
- //# sourceMappingURL=alignString.js.map
|