label-generator-component.js 848 B

12345678910111213141516171819202122232425262728293031323334353637
  1. export class LabelGenerator {
  2. static get LOWER_CODE() {
  3. return 97; // ASCII 'a'
  4. }
  5. static get UPPER_CODE() {
  6. return 122; // ASCII 'z'
  7. }
  8. constructor() {
  9. this.current_code = [lower_code];
  10. this.used_labels = new Set();
  11. }
  12. addUsed(label) {
  13. this.used_labels.add(label);
  14. }
  15. generate() {
  16. const label = current_code.map(String.fromCharCode);
  17. const pos = current_code.length - 1;
  18. if (current_code[pos] >= LabelGenerator.UPPER_CODE) {
  19. current_code[pos] = LabelGenerator.LOWER_CODE;
  20. current_code.push(LabelGenerator.LOWER_CODE);
  21. } else {
  22. current_code[pos] += 1;
  23. }
  24. const strLabel = label.join("").toUpperCase();
  25. if (this.used_labels.has(strLabel)) {
  26. return this.generate();
  27. }
  28. this.used_labels.add(strLabel);
  29. return {
  30. draw: () => strLabel
  31. };
  32. }
  33. }