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.flow 2.3KB

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