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.

rangeContextNodeParser.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // @ts-nocheck
  2. 'use strict';
  3. const rangeOperators = ['>=', '<=', '>', '<', '='];
  4. const styleSearch = require('style-search');
  5. function getRangeContextOperators(node) {
  6. const operators = [];
  7. styleSearch({ source: node.value, target: rangeOperators }, (match) => {
  8. const before = node[match.startIndex - 1];
  9. if (before === '>' || before === '<') {
  10. return;
  11. }
  12. operators.push(match.target);
  13. });
  14. // Sorting helps when using the operators to split
  15. // E.g. for "(10em < width <= 50em)" this returns ["<=", "<"]
  16. return operators.sort((a, b) => b.length - a.length);
  17. }
  18. function getRangeContextName(parsedNode) {
  19. // When the node is like "(10em < width < 50em)"
  20. // The parsedNode is ["10em", "width", "50em"] - the name is always in the second position
  21. if (parsedNode.length === 3) {
  22. return parsedNode[1];
  23. }
  24. // When the node is like "(width > 10em)" or "(10em < width)"
  25. // Regex is needed because the name can either be in the first or second position
  26. return parsedNode.find((value) => value.match(/^(?!--)\D+/) || value.match(/^(--).+/));
  27. }
  28. module.exports = function (node) {
  29. const nodeValue = node.value;
  30. const operators = getRangeContextOperators(node);
  31. // Remove spaces and parentheses and split by the operators
  32. const parsedMedia = nodeValue.replace(/[()\s]/g, '').split(new RegExp(operators.join('|')));
  33. const name = getRangeContextName(parsedMedia);
  34. const nameObj = {
  35. value: name,
  36. sourceIndex: node.sourceIndex + nodeValue.indexOf(name),
  37. };
  38. const values = parsedMedia
  39. .filter((parsedValue) => parsedValue !== name)
  40. .map((value) => {
  41. return {
  42. value,
  43. sourceIndex: node.sourceIndex + nodeValue.indexOf(value),
  44. };
  45. });
  46. return {
  47. name: nameObj,
  48. values,
  49. };
  50. };