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.

index.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict'
  2. const spaces = /\s+/g
  3. const leadingProduct = /^(BUS|TRAM)\s+/g
  4. const symbolOnly = /^[a-z]{1,3}$/i
  5. const numberOnly = /^[\d]+$/i
  6. const twoNumbers = /^([\d]+)[^\w]([\d]+)$/i
  7. const symbolAndNumber = /^(([a-z]{1,3})[^\w]?([\d]+)|([\d]+)[^\w]?([a-z]{1,3}))/i
  8. const modes = {
  9. 'ICE': 'train',
  10. 'IC': 'train',
  11. 'EC': 'train',
  12. 'EN': 'train',
  13. 'LOC': 'train',
  14. 'RE': 'train',
  15. 'RB': 'train',
  16. 'OE': 'train',
  17. 'U': 'train',
  18. 'S': 'train',
  19. 'F': 'watercraft',
  20. 'E': 'bus',
  21. 'H': 'bus',
  22. 'N': 'bus',
  23. 'X': 'bus'
  24. }
  25. const products = {
  26. 'ICE': 'express',
  27. 'IC': 'express',
  28. 'EC': 'express',
  29. 'EN': 'express',
  30. 'LOC': 'express',
  31. 'RE': 'regional',
  32. 'RB': 'regional',
  33. 'OE': 'regional',
  34. 'U': 'subway',
  35. 'S': 'suburban',
  36. 'F': 'ferry',
  37. 'E': 'bus',
  38. 'H': 'bus',
  39. 'N': 'bus',
  40. 'X': 'bus'
  41. }
  42. const parse = (name) => {
  43. name = name.toUpperCase().replace(leadingProduct, '').replace(spaces, '')
  44. let r = {_: name,
  45. mode: null, product: null, symbol: null, nr: null,
  46. metro: false, express: false, night: false
  47. }
  48. // weird buses in Berlin
  49. if (name === 'TXL') return {_: name,
  50. mode: 'bus', product: 'bus', symbol: 'TXL', nr: null,
  51. metro: true, express: true, night: true
  52. }
  53. if (name === 'SXF2') return {_: name,
  54. mode: 'bus', product: 'bus', symbol: 'SXF', nr: 2,
  55. metro: false, express: true, night: false
  56. }
  57. if (name === 'SEV') return {_: name,
  58. mode: 'bus', product: 'bus', symbol: null, nr: null,
  59. metro: true, express: false, night: false
  60. }
  61. if (name === 'IRE') return {_: name,
  62. mode: 'train', product: 'regional', symbol: 'IRE', nr: null,
  63. metro: false, express: true, night: false
  64. }
  65. if (symbolOnly.test(name)) {
  66. r.symbol = name
  67. r.mode = r.product = 'bus'
  68. } else if (numberOnly.test(name)) { // bus & tram lines
  69. r.nr = parseInt(name)
  70. } else if (twoNumbers.test(name)) { // weird buses in Brandenburg
  71. const matches = twoNumbers.exec(name)
  72. r.mode = r.product = 'bus'
  73. r.nr = parseInt(matches[1])
  74. } else {
  75. const matches = symbolAndNumber.exec(name)
  76. if (matches && matches[2] && matches[3]) {
  77. r.symbol = matches[2]
  78. r.nr = parseInt(matches[3])
  79. } else if (matches && matches[4] && matches[5]) { // night bus somewhere else
  80. r.nr = parseInt(matches[4])
  81. r.symbol = matches[5]
  82. }
  83. }
  84. if (Number.isNaN(r.nr)) r.nr = null
  85. // handle bus & tram lines with symbol
  86. if (r.symbol === 'M') r.metro = true
  87. else if (r.symbol === 'X') r.express = true
  88. else if (r.symbol === 'N') r.night = true
  89. // handle weird metro bus & metro tram naming
  90. if (!r.product) {
  91. if (r.symbol === 'M') {
  92. r.product = (r.nr <= 17 && r.nr !== 11) ? 'tram' : 'bus'
  93. r.mode = (r.nr <= 17 && r.nr !== 11) ? 'train' : 'bus'
  94. } else if (r.symbol) {
  95. r.mode = modes[r.symbol]
  96. r.product = products[r.symbol]
  97. } else {
  98. r.product = r.nr < 100 ? 'tram' : 'bus'
  99. r.mode = r.nr < 100 ? 'train' : 'bus'
  100. }
  101. }
  102. if (r.symbol === 'RE') r.express = true // regional express trains
  103. else if (r.symbol === 'EN') r.night = true // national night trains
  104. return r
  105. }
  106. module.exports = parse