Skip to content

Commit

Permalink
add random_char helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanatkn committed Nov 16, 2023
1 parent ee2a4e3 commit 8353169
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-bottles-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@grogarden/util': patch
---

add `random_char` helper
1 change: 1 addition & 0 deletions src/lib/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export const src_json = {
{name: 'random_int', kind: 'function'},
{name: 'random_boolean', kind: 'function'},
{name: 'random_item', kind: 'function'},
{name: 'random_char', kind: 'function'},
{name: 'shuffle', kind: 'function'},
],
},
Expand Down
5 changes: 5 additions & 0 deletions src/lib/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export const random_boolean = (random = Math.random): boolean => random() > 0.5;
export const random_item = <T>(arr: T[], random = Math.random): T =>
arr[random_int(0, arr.length - 1, random)];

export const random_char = (chars = alphanumerics, random = Math.random): string =>
random_item(chars, random);

const alphanumerics = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('');

/**
* Mutates `array` with random ordering.
*/
Expand Down

0 comments on commit 8353169

Please sign in to comment.