Skip to content

Commit

Permalink
fix: update randomId to return a cryptographically random UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
pleb committed Jun 20, 2022
1 parent bddc9a6 commit 41b4837
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/data-utilties.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { randomUUID } from 'crypto'

/**
* Generates a random number between 0 or [Number.MIN_SAFE_INTEGER](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER) when `allowNegative` is set
* and [Number.MAX_SAFE_INTEGER](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER).
Expand Down Expand Up @@ -44,14 +46,14 @@ export function randomString(min: number, max: number): string {
}

/**
* Generates a random ID with a length of `length`
* Generates a cryptographically random UUID in the form of 00000000-0000-0000-0000-000000000000
*
* ```typescript
* const id = randomId(15)
* const id = randomId()
* ```
*/
export function randomId(length: number): string {
return randomString(length, length)
export function randomId(): string {
return randomUUID()
}

export const randomDateRangeMin = new Date(1980, 1, 1)
Expand Down
2 changes: 1 addition & 1 deletion test/data-utilities.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Data utilities', () => {
})
describe('randomId', () => {
it('generates a random ID of the requested length', () => {
expect(randomId(10).length).toBe(10)
expect(randomId()).toBeTruthy()
})
})
describe('randomDate', () => {
Expand Down

0 comments on commit 41b4837

Please sign in to comment.