Browse Source

Implement new label generator that cycles from the letters a-z, increasing one letter at each cycle

Lucas de Souza 4 years ago
parent
commit
ca38a23e79
2 changed files with 46 additions and 0 deletions
  1. 9 0
      .eslintrc.json
  2. 37 0
      src/app/component-registry/label-generator-component.js

+ 9 - 0
.eslintrc.json

@@ -0,0 +1,9 @@
+{
+  "env": {
+    "browser": true,
+    "es6": true
+  },
+  "rules": {
+    "space-before-function-paren": ["error", "always"]
+  }
+}

+ 37 - 0
src/app/component-registry/label-generator-component.js

@@ -0,0 +1,37 @@
+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
+    };
+  }
+}