Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

util.ts 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class KeyGenerator {
  2. idx = 0;
  3. chars: string[] = [];
  4. constructor() {
  5. const chars = Array(26)
  6. .fill(1)
  7. .map((x, idx) => String.fromCharCode(97 + idx)); // 26 char
  8. this.chars = chars;
  9. }
  10. generateKey() {
  11. const key = this.chars[this.idx];
  12. this.idx++;
  13. return key;
  14. }
  15. }
  16. // Classify nodes based on edge relationships
  17. export class Converter {
  18. keyGenerator;
  19. dict: Record<string, string> = {}; // key is node id, value is combo
  20. constructor() {
  21. this.keyGenerator = new KeyGenerator();
  22. }
  23. buildDict(edges: { source: string; target: string }[]) {
  24. edges.forEach((x) => {
  25. if (this.dict[x.source] && !this.dict[x.target]) {
  26. this.dict[x.target] = this.dict[x.source];
  27. } else if (!this.dict[x.source] && this.dict[x.target]) {
  28. this.dict[x.source] = this.dict[x.target];
  29. } else if (!this.dict[x.source] && !this.dict[x.target]) {
  30. this.dict[x.source] = this.dict[x.target] =
  31. this.keyGenerator.generateKey();
  32. }
  33. });
  34. return this.dict;
  35. }
  36. buildNodesAndCombos(nodes: any[], edges: any[]) {
  37. this.buildDict(edges);
  38. const nextNodes = nodes.map((x) => ({ ...x, combo: this.dict[x.id] }));
  39. const combos = Object.values(this.dict).reduce<any[]>((pre, cur) => {
  40. if (pre.every((x) => x.id !== cur)) {
  41. pre.push({
  42. id: cur,
  43. data: {
  44. label: `Combo ${cur}`,
  45. },
  46. });
  47. }
  48. return pre;
  49. }, []);
  50. return { nodes: nextNodes, combos };
  51. }
  52. }