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.

util.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. export function isSpace(source) {
  2. return /^\s+$/.test(source);
  3. }
  4. export function hasCR(source) {
  5. return /\r$/.test(source);
  6. }
  7. export function splitCR(source) {
  8. const matches = source.match(/\r+$/);
  9. return matches == null
  10. ? ['', source]
  11. : [source.slice(-matches[0].length), source.slice(0, -matches[0].length)];
  12. }
  13. export function splitSpace(source) {
  14. const matches = source.match(/^\s+/);
  15. return matches == null
  16. ? ['', source]
  17. : [source.slice(0, matches[0].length), source.slice(matches[0].length)];
  18. }
  19. export function splitLines(source) {
  20. return source.split(/\n/);
  21. }
  22. export function seedBlock(block = {}) {
  23. return Object.assign({ description: '', tags: [], source: [], problems: [] }, block);
  24. }
  25. export function seedSpec(spec = {}) {
  26. return Object.assign({ tag: '', name: '', type: '', optional: false, description: '', problems: [], source: [] }, spec);
  27. }
  28. export function seedTokens(tokens = {}) {
  29. return Object.assign({ start: '', delimiter: '', postDelimiter: '', tag: '', postTag: '', name: '', postName: '', type: '', postType: '', description: '', end: '', lineEnd: '' }, tokens);
  30. }
  31. /**
  32. * Assures Block.tags[].source contains references to the Block.source items,
  33. * using Block.source as a source of truth. This is a counterpart of rewireSpecs
  34. * @param block parsed coments block
  35. */
  36. export function rewireSource(block) {
  37. const source = block.source.reduce((acc, line) => acc.set(line.number, line), new Map());
  38. for (const spec of block.tags) {
  39. spec.source = spec.source.map((line) => source.get(line.number));
  40. }
  41. return block;
  42. }
  43. /**
  44. * Assures Block.source contains references to the Block.tags[].source items,
  45. * using Block.tags[].source as a source of truth. This is a counterpart of rewireSource
  46. * @param block parsed coments block
  47. */
  48. export function rewireSpecs(block) {
  49. const source = block.tags.reduce((acc, spec) => spec.source.reduce((acc, line) => acc.set(line.number, line), acc), new Map());
  50. block.source = block.source.map((line) => source.get(line.number) || line);
  51. return block;
  52. }