methods.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const fs = require('fs')
  2. const csvParse = require('csv-parse/lib/sync')
  3. const Usability = {
  4. UNKNOWN: 0,
  5. USER: 1,
  6. BOT: 2,
  7. BOTH: 4,
  8. }
  9. class MethodInfo {
  10. constructor(name, usability, errors, friendly) {
  11. this.name = name
  12. this.errors = errors
  13. this.friendly = friendly
  14. if (usability.toUpperCase() in Usability) {
  15. this.usability = Usability[usability.toUpperCase()]
  16. } else {
  17. throw new Error(`Usability must be either user, bot, both or unknown, not ${usability}`)
  18. }
  19. }
  20. }
  21. /**
  22. * Parses the input CSV file with columns (method, usability, errors)
  23. * and yields `MethodInfo` instances as a result.
  24. */
  25. const parseMethods = function* (csvFile, friendlyCsvFile, errorsDict) {
  26. const rawToFriendly = {}
  27. const f1 = csvParse(fs.readFileSync(friendlyCsvFile, { encoding: 'utf-8' }))
  28. for (const [ns, friendly, rawList] of f1.slice(1)) {
  29. for (const raw of rawList.split(' ')) {
  30. rawToFriendly[raw] = [ns, friendly]
  31. }
  32. }
  33. const f2 = csvParse(fs.readFileSync(csvFile, { encoding: 'utf-8' })).slice(1)
  34. for (let line = 0; line < f2.length; line++) {
  35. let [method, usability, errors] = f2[line]
  36. errors = errors
  37. .split(' ')
  38. .filter(Boolean)
  39. .map((x) => {
  40. if (x && !(x in errorsDict)) {
  41. throw new Error(`Method ${method} references unknown errors ${errors}`)
  42. }
  43. return errorsDict[x]
  44. })
  45. const friendly = rawToFriendly[method]
  46. delete rawToFriendly[method]
  47. yield new MethodInfo(method, usability, errors, friendly)
  48. }
  49. }
  50. module.exports = {
  51. Usability,
  52. MethodInfo,
  53. parseMethods,
  54. }