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.

description.js 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Markers } from '../../primitives.js';
  2. /**
  3. * Makes no changes to `spec.lines[].tokens` but joins them into `spec.description`
  4. * following given spacing srtategy
  5. * @param {Spacing} spacing tells how to handle the whitespace
  6. */
  7. export default function descriptionTokenizer(spacing = 'compact') {
  8. const join = getJoiner(spacing);
  9. return (spec) => {
  10. spec.description = join(spec.source);
  11. return spec;
  12. };
  13. }
  14. export function getJoiner(spacing) {
  15. if (spacing === 'compact')
  16. return compactJoiner;
  17. if (spacing === 'preserve')
  18. return preserveJoiner;
  19. return spacing;
  20. }
  21. function compactJoiner(lines) {
  22. return lines
  23. .map(({ tokens: { description } }) => description.trim())
  24. .filter((description) => description !== '')
  25. .join(' ');
  26. }
  27. const lineNo = (num, { tokens }, i) => tokens.type === '' ? num : i;
  28. const getDescription = ({ tokens }) => (tokens.delimiter === '' ? tokens.start : tokens.postDelimiter.slice(1)) +
  29. tokens.description;
  30. function preserveJoiner(lines) {
  31. if (lines.length === 0)
  32. return '';
  33. // skip the opening line with no description
  34. if (lines[0].tokens.description === '' &&
  35. lines[0].tokens.delimiter === Markers.start)
  36. lines = lines.slice(1);
  37. // skip the closing line with no description
  38. const lastLine = lines[lines.length - 1];
  39. if (lastLine !== undefined &&
  40. lastLine.tokens.description === '' &&
  41. lastLine.tokens.end.endsWith(Markers.end))
  42. lines = lines.slice(0, -1);
  43. // description starts at the last line of type definition
  44. lines = lines.slice(lines.reduce(lineNo, 0));
  45. return lines.map(getDescription).join('\n');
  46. }