pirxpilot / liftie

:ski: Clean, simple, easy to read, fast ski resort lift status.
https://liftie.info
BSD 3-Clause "New" or "Revised" License
67 stars 31 forks source link

Help with resort? #61

Closed FezVrasta closed 11 months ago

FezVrasta commented 11 months ago

Hi, could I get some help converting this into a resort matcher?

$$('[class*="impianto-status"]').map(x => ({
  name: x.parentNode.childNodes[0].innerText,
  status: (() => { switch (x.getAttribute("class").match(/impianto-status-([FPO])/)[1]) {
          case 'F': return 'closed';
          case 'P': return 'hold';
          case 'O': return 'open';
        }})()
}));

I have this but it's not working

module.exports = {
  selector: '[class*="impianto-status"]', // selector for lift information
  parse: {
    filter: (node) => node.children, // if present skips nodes for which filter is falsy
    name: '../0', // example of a simple path descriptor - index, ',', '..', '+', '-' are supported. Here 0/1 just means "use the second child of the first child."
    status: {
      // example of a compound descriptor child. attribute, regex, fn - can be specified
      child: '.',
      attribute: 'class',
      regex: /impianto-status-([FPO])/,
      fn: (s) => {
        switch (s[1]) {
          case 'F': return 'closed';
          case 'P': return 'hold';
          case 'O': return 'open';
        }
      }
    },
  },
};
pirxpilot commented 11 months ago

The only thing that I noticed is s[1] usage in the case statement.

fn already receives the content of the capture group: in this case 'F', 'P', or 'O' - so it should be switch(s) not switch(s[1]).