You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.7 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _isNumber2 = _interopRequireDefault(require("lodash/isNumber"));
  7. var _isString2 = _interopRequireDefault(require("lodash/isString"));
  8. var _stringWidth = _interopRequireDefault(require("string-width"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. const alignments = ['left', 'right', 'center'];
  11. /**
  12. * @param {string} subject
  13. * @param {number} width
  14. * @returns {string}
  15. */
  16. const alignLeft = (subject, width) => {
  17. return subject + ' '.repeat(width);
  18. };
  19. /**
  20. * @param {string} subject
  21. * @param {number} width
  22. * @returns {string}
  23. */
  24. const alignRight = (subject, width) => {
  25. return ' '.repeat(width) + subject;
  26. };
  27. /**
  28. * @param {string} subject
  29. * @param {number} width
  30. * @returns {string}
  31. */
  32. const alignCenter = (subject, width) => {
  33. let halfWidth;
  34. halfWidth = width / 2;
  35. if (halfWidth % 2 === 0) {
  36. return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth);
  37. } else {
  38. halfWidth = Math.floor(halfWidth);
  39. return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth + 1);
  40. }
  41. };
  42. /**
  43. * Pads a string to the left and/or right to position the subject
  44. * text in a desired alignment within a container.
  45. *
  46. * @param {string} subject
  47. * @param {number} containerWidth
  48. * @param {string} alignment One of the valid options (left, right, center).
  49. * @returns {string}
  50. */
  51. const alignString = (subject, containerWidth, alignment) => {
  52. if (!(0, _isString2.default)(subject)) {
  53. throw new TypeError('Subject parameter value must be a string.');
  54. }
  55. if (!(0, _isNumber2.default)(containerWidth)) {
  56. throw new TypeError('Container width parameter value must be a number.');
  57. }
  58. const subjectWidth = (0, _stringWidth.default)(subject);
  59. if (subjectWidth > containerWidth) {
  60. // console.log('subjectWidth', subjectWidth, 'containerWidth', containerWidth, 'subject', subject);
  61. throw new Error('Subject parameter value width cannot be greater than the container width.');
  62. }
  63. if (!(0, _isString2.default)(alignment)) {
  64. throw new TypeError('Alignment parameter value must be a string.');
  65. }
  66. if (!alignments.includes(alignment)) {
  67. throw new Error('Alignment parameter value must be a known alignment parameter value (left, right, center).');
  68. }
  69. if (subjectWidth === 0) {
  70. return ' '.repeat(containerWidth);
  71. }
  72. const availableWidth = containerWidth - subjectWidth;
  73. if (alignment === 'left') {
  74. return alignLeft(subject, availableWidth);
  75. }
  76. if (alignment === 'right') {
  77. return alignRight(subject, availableWidth);
  78. }
  79. return alignCenter(subject, availableWidth);
  80. };
  81. var _default = alignString;
  82. exports.default = _default;
  83. //# sourceMappingURL=alignString.js.map