Skip to content

Commit

Permalink
added plugin concept, moved non-core methods to common plugin, moved …
Browse files Browse the repository at this point in the history
…types and util functions around
  • Loading branch information
jsoverson committed May 17, 2020
1 parent 168f005 commit 32405a2
Show file tree
Hide file tree
Showing 16 changed files with 399 additions and 369 deletions.
27 changes: 20 additions & 7 deletions src/id-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ const nouns = require('./nouns');
const adjectives = require('./adjectives');
const seedrandom = require('seedrandom');

export class IdGenerator {
export interface IdGenerator extends Iterator<string> {
next(): IteratorResult<string>;
}

export class MemorableIdGenerator implements IdGenerator {
rng: seedrandom.prng;

constructor(seed = 0) {
this.rng = seedrandom(0);
this.rng = seedrandom(seed);

}

randomNoun() {
Expand All @@ -31,7 +35,10 @@ export class IdGenerator {
next() {
const noun = this.randomNoun();

return `${this.randomAdjective()}${noun[0].toUpperCase()}${noun.slice(1)}`;
return {
done: false,
value: `${this.randomAdjective()}${noun[0].toUpperCase()}${noun.slice(1)}`
}
}

*[Symbol.iterator]() {
Expand All @@ -41,7 +48,7 @@ export class IdGenerator {
}
}

export class BasicIdGenerator {
export class BasicIdGenerator implements IdGenerator {
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
reservedWords: Set<string> = new Set(jsKeywords);
current: number[];
Expand All @@ -56,10 +63,16 @@ export class BasicIdGenerator {
this._increment();
const nextId = this.current.reduce((acc, code) => acc + this.alphabet[code], "");
if (!this.reservedWords.has(nextId)) {
return nextId;
return {
done: false,
value: nextId
};
} else {
this._increment();
return this.current.reduce((acc, code) => acc + this.alphabet[code], "");
return {
done: false,
value: this.current.reduce((acc, code) => acc + this.alphabet[code], "")
};
}
}

Expand Down
Loading

0 comments on commit 32405a2

Please sign in to comment.