Skip to content

Commit

Permalink
Adding shuffling of goals when generating PDF.
Browse files Browse the repository at this point in the history
  • Loading branch information
UniqueClone committed Jan 12, 2024
1 parent d90be4e commit 723dcc8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/pdf-generator/Generator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { text, image, barcodes } from "@pdfme/schemas";
import { generate } from "@pdfme/generator";
import { formatName, validateGoals } from "./GeneratorUtils";
import { formatName, shuffleGoals, validateGoals } from "./GeneratorUtils";

export const Generator = (name: string, goals: string[]) => {
name = formatName(name);
validateGoals(goals);
shuffleGoals(goals);

(async () => {
const template = {
Expand Down
11 changes: 11 additions & 0 deletions src/pdf-generator/GeneratorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,15 @@ export const formatName = (name: string): string => {
} else {
return name + "'s";
}
}

/**
* Shuffles array in place using the Fisher-Yates shuffle algorithm.
* @param goals An array containing the items.
*/
export const shuffleGoals = (goals: string[]): void => {
for (let i = goals.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[goals[i], goals[j]] = [goals[j], goals[i]];
}
}

0 comments on commit 723dcc8

Please sign in to comment.