Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

alignTransform.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _commentParser = require("comment-parser");
  7. /**
  8. * Transform based on https://github.com/syavorsky/comment-parser/blob/master/src/transforms/align.ts
  9. *
  10. * It contains some customizations to align based on the tags, and some custom options.
  11. */
  12. const {
  13. rewireSource
  14. } = _commentParser.util;
  15. const zeroWidth = {
  16. name: 0,
  17. start: 0,
  18. tag: 0,
  19. type: 0
  20. };
  21. const shouldAlign = (tags, index, source) => {
  22. const tag = source[index].tokens.tag.replace('@', '');
  23. const includesTag = tags.includes(tag);
  24. if (includesTag) {
  25. return true;
  26. }
  27. if (tag !== '') {
  28. return false;
  29. }
  30. for (let iterator = index; iterator >= 0; iterator--) {
  31. const previousTag = source[iterator].tokens.tag.replace('@', '');
  32. if (previousTag !== '') {
  33. if (tags.includes(previousTag)) {
  34. return true;
  35. }
  36. return false;
  37. }
  38. }
  39. return true;
  40. };
  41. const getWidth = tags => {
  42. return (width, {
  43. tokens
  44. }, index, source) => {
  45. if (!shouldAlign(tags, index, source)) {
  46. return width;
  47. }
  48. return {
  49. name: Math.max(width.name, tokens.name.length),
  50. start: tokens.delimiter === _commentParser.Markers.start ? tokens.start.length : width.start,
  51. tag: Math.max(width.tag, tokens.tag.length),
  52. type: Math.max(width.type, tokens.type.length)
  53. };
  54. };
  55. };
  56. const space = len => {
  57. return ''.padStart(len, ' ');
  58. };
  59. const alignTransform = ({
  60. customSpacings,
  61. tags,
  62. indent,
  63. preserveMainDescriptionPostDelimiter
  64. }) => {
  65. let intoTags = false;
  66. let width;
  67. const alignTokens = tokens => {
  68. const nothingAfter = {
  69. delim: false,
  70. name: false,
  71. tag: false,
  72. type: false
  73. };
  74. if (tokens.description === '') {
  75. nothingAfter.name = true;
  76. tokens.postName = '';
  77. if (tokens.name === '') {
  78. nothingAfter.type = true;
  79. tokens.postType = '';
  80. if (tokens.type === '') {
  81. nothingAfter.tag = true;
  82. tokens.postTag = '';
  83. /* istanbul ignore next: Never happens because the !intoTags return. But it's here for consistency with the original align transform */
  84. if (tokens.tag === '') {
  85. nothingAfter.delim = true;
  86. }
  87. }
  88. }
  89. } // Todo: Avoid fixing alignment of blocks with multiline wrapping of type
  90. if (tokens.tag === '' && tokens.type) {
  91. return tokens;
  92. }
  93. const spacings = {
  94. postDelimiter: (customSpacings === null || customSpacings === void 0 ? void 0 : customSpacings.postDelimiter) || 1,
  95. postName: (customSpacings === null || customSpacings === void 0 ? void 0 : customSpacings.postName) || 1,
  96. postTag: (customSpacings === null || customSpacings === void 0 ? void 0 : customSpacings.postTag) || 1,
  97. postType: (customSpacings === null || customSpacings === void 0 ? void 0 : customSpacings.postType) || 1
  98. };
  99. tokens.postDelimiter = nothingAfter.delim ? '' : space(spacings.postDelimiter);
  100. if (!nothingAfter.tag) {
  101. tokens.postTag = space(width.tag - tokens.tag.length + spacings.postTag);
  102. }
  103. if (!nothingAfter.type) {
  104. tokens.postType = space(width.type - tokens.type.length + spacings.postType);
  105. }
  106. if (!nothingAfter.name) {
  107. // If post name is empty for all lines (name width 0), don't add post name spacing.
  108. tokens.postName = width.name === 0 ? '' : space(width.name - tokens.name.length + spacings.postName);
  109. }
  110. return tokens;
  111. };
  112. const update = (line, index, source) => {
  113. const tokens = { ...line.tokens
  114. };
  115. if (tokens.tag !== '') {
  116. intoTags = true;
  117. }
  118. const isEmpty = tokens.tag === '' && tokens.name === '' && tokens.type === '' && tokens.description === ''; // dangling '*/'
  119. if (tokens.end === _commentParser.Markers.end && isEmpty) {
  120. tokens.start = indent + ' ';
  121. return { ...line,
  122. tokens
  123. };
  124. }
  125. /* eslint-disable indent */
  126. switch (tokens.delimiter) {
  127. case _commentParser.Markers.start:
  128. tokens.start = indent;
  129. break;
  130. case _commentParser.Markers.delim:
  131. tokens.start = indent + ' ';
  132. break;
  133. default:
  134. tokens.delimiter = ''; // compensate delimiter
  135. tokens.start = indent + ' ';
  136. }
  137. /* eslint-enable */
  138. if (!intoTags) {
  139. if (tokens.description === '') {
  140. tokens.postDelimiter = '';
  141. } else if (!preserveMainDescriptionPostDelimiter) {
  142. tokens.postDelimiter = ' ';
  143. }
  144. return { ...line,
  145. tokens
  146. };
  147. } // Not align.
  148. if (!shouldAlign(tags, index, source)) {
  149. return { ...line,
  150. tokens
  151. };
  152. }
  153. return { ...line,
  154. tokens: alignTokens(tokens)
  155. };
  156. };
  157. return ({
  158. source,
  159. ...fields
  160. }) => {
  161. width = source.reduce(getWidth(tags), { ...zeroWidth
  162. });
  163. return rewireSource({ ...fields,
  164. source: source.map(update)
  165. });
  166. };
  167. };
  168. var _default = alignTransform;
  169. exports.default = _default;
  170. module.exports = exports.default;
  171. //# sourceMappingURL=alignTransform.js.map