Skip to content

Commit

Permalink
refactor: make Array.choose an extension
Browse files Browse the repository at this point in the history
  • Loading branch information
madkarmaa committed Dec 21, 2024
1 parent 41bc06b commit d6f972d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ declare global {
// interface PageState {}
// interface Platform {}
}

interface Array<T> {
choose(): T | undefined;
}
}

export {};
3 changes: 3 additions & 0 deletions src/lib/choose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Array.prototype.choose = function <T>(this: T[]): T | undefined {
return this[Math.floor(Math.random() * this.length)];
};
2 changes: 0 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ export const secToMs = (sec: number): number => sec * 1000;

export const minToMs = (min: number): number => secToMs(min * 60);

export const choose = <T>(items: T[]): T => items[Math.floor(Math.random() * items.length)];

export const base64 = {
encode: (input: string): string => btoa(input),
decode: (input: string): string => atob(input)
Expand Down
1 change: 1 addition & 0 deletions src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import '../app.css';
import '../lib/choose';
import '../lib/deepfreeze';

export const prerender = true;

0 comments on commit d6f972d

Please sign in to comment.