Skip to content

Commit

Permalink
feat: add generator function
Browse files Browse the repository at this point in the history
  • Loading branch information
Bikossor committed Feb 4, 2024
1 parent a526fdd commit 83365b7
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions packages/anonymus/src/anonymus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,28 +228,18 @@ export const version = [2, 0, 0].join(".");
/**
* Generates one or more unique names
*/
export const create = (amount: number = 1) => {
export function* createAnonymusGenerator() {
let indexColor = 0;
let indexAnimal = 0;

if (!Number.isInteger(amount)) {
throw new Error("Amount has to be an integer!");
}
// Shuffle the colors and animals
colors.sort(() => Math.random() - 0.5);
animals.sort(() => Math.random() - 0.5);

if (amount > sizeMax) {
throw new Error("Amount cannot exceed 'anonymus.maxSize'!");
}
while (true) {
yield `${colors.at(indexColor)} ${animals.at(indexAnimal)}`;

var result: Array<string> = [],
tmp: string;

for (var i = 0; i < amount; i++) {
tmp = randomColor() + " " + randomAnimal();

while (result.includes(tmp)) {
tmp = randomColor() + " " + randomAnimal();
}

result.push(tmp);
}

return result;
};
indexColor++;
indexAnimal++;
}
}

0 comments on commit 83365b7

Please sign in to comment.