alignString.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _lodash = require('lodash');
  6. var _lodash2 = _interopRequireDefault(_lodash);
  7. var _stringWidth = require('string-width');
  8. var _stringWidth2 = _interopRequireDefault(_stringWidth);
  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 + _lodash2.default.repeat(' ', width);
  18. };
  19. /**
  20. * @param {string} subject
  21. * @param {number} width
  22. * @returns {string}
  23. */
  24. const alignRight = (subject, width) => {
  25. return _lodash2.default.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 _lodash2.default.repeat(' ', halfWidth) + subject + _lodash2.default.repeat(' ', halfWidth);
  37. } else {
  38. halfWidth = _lodash2.default.floor(halfWidth);
  39. return _lodash2.default.repeat(' ', halfWidth) + subject + _lodash2.default.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. exports.default = (subject, containerWidth, alignment) => {
  52. if (!_lodash2.default.isString(subject)) {
  53. throw new TypeError('Subject parameter value must be a string.');
  54. }
  55. if (!_lodash2.default.isNumber(containerWidth)) {
  56. throw new TypeError('Container width parameter value must be a number.');
  57. }
  58. const subjectWidth = (0, _stringWidth2.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 (!_lodash2.default.isString(alignment)) {
  64. throw new TypeError('Alignment parameter value must be a string.');
  65. }
  66. if (alignments.indexOf(alignment) === -1) {
  67. throw new Error('Alignment parameter value must be a known alignment parameter value (left, right, center).');
  68. }
  69. if (subjectWidth === 0) {
  70. return _lodash2.default.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. };