Ohm-Management - Projektarbeit B-ME
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.

alignString.js 2.7KB

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