Home Reference Source Repository

src/app/component-registry/label-generator-component.js

export class LabelGenerator {
  static get LOWER_CODE() {
    return 97; // ASCII 'a'
  }

  static get UPPER_CODE() {
    return 122; // ASCII 'z'
  }

  constructor() {
    this.current_code = [lower_code];
    this.used_labels = new Set();
  }

  addUsed(label) {
    this.used_labels.add(label);
  }

  generate() {
    const label = current_code.map(String.fromCharCode);
    const pos = current_code.length - 1;
    if (current_code[pos] >= LabelGenerator.UPPER_CODE) {
      current_code[pos] = LabelGenerator.LOWER_CODE;
      current_code.push(LabelGenerator.LOWER_CODE);
    } else {
      current_code[pos] += 1;
    }
    const strLabel = label.join("").toUpperCase();
    if (this.used_labels.has(strLabel)) {
      return this.generate();
    }
    this.used_labels.add(strLabel);
    return {
      draw: () => strLabel
    };
  }
}