index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const genresText = require('./genresText.js');
  2. const genres = [];
  3. const nonfb2Genres = [];//костылики
  4. let nonfb2 = false;//костылики
  5. const sec2index = {};
  6. const lines = genresText.split('\n').map(l => l.trim());
  7. let index = 0;
  8. let other;//прочее в конец
  9. const names = new Set();
  10. for (const line of lines) {
  11. if (line.indexOf('#nonfb2') == 0)
  12. nonfb2 = true;
  13. if (!line || line[0] == '#')
  14. continue;
  15. const p = line.indexOf(' ');
  16. const num = line.substring(0, p).trim().split('.');
  17. if (num.length < 2)
  18. continue;
  19. const section = `${num[0]}.${num[1]}`;
  20. if (section == '0.0')
  21. continue;
  22. let name = line.substring(p + 1).trim();
  23. if (!nonfb2) {
  24. if (num.length < 3) {//раздел
  25. if (section == '0.20') {//прочее
  26. other = {name, value: []};
  27. } else {
  28. if (sec2index[section] === undefined) {
  29. if (!genres[index])
  30. genres[index] = {name, value: []};
  31. sec2index[section] = index;
  32. index++;
  33. }
  34. }
  35. } else {//подраздел
  36. const n = name.split(';').map(l => l.trim());
  37. names.add(n[0]);
  38. if (section == '0.20') {//прочее
  39. other.value.push({name: n[1], value: n[0]});
  40. } else {
  41. const i = sec2index[section];
  42. if (i !== undefined) {
  43. genres[i].value.push({name: n[1], value: n[0]});
  44. }
  45. }
  46. }
  47. } else {
  48. const n = name.split(';').map(l => l.trim());
  49. if (!names.has(n[0]))
  50. nonfb2Genres.push({name: n[1], value: n[0]});
  51. names.add(n[0]);
  52. }
  53. }
  54. if (other) {
  55. if (nonfb2Genres.length) {
  56. other.value = other.value.concat(nonfb2Genres);
  57. }
  58. genres.push(other);
  59. }
  60. //console.log(JSON.stringify(genres));
  61. module.exports = genres;