-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: David Liu <david-khala@hotmail.com>
- Loading branch information
1 parent
e507a95
commit 14e824c
Showing
3 changed files
with
19 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,25 @@ | ||
// total: 293KB | ||
import crypto from 'crypto'; | ||
|
||
export function randomKeyOf(obj) { | ||
const keys = Object.keys(obj); | ||
const keyIndex = Math.floor(Math.random() * keys.length); | ||
const keyIndex = crypto.randomInt(keys.length); | ||
return keys[keyIndex]; | ||
} | ||
|
||
export const uuid = crypto.randomUUID; | ||
export const randomHex = (length) => crypto.randomBytes(length / 2).toString('hex'); | ||
|
||
/** | ||
* | ||
* @param {number} length integer | ||
* @return {string} alphaNumeric | ||
*/ | ||
export const randomString = (length) => [...Array(length)].map(() => (~~(Math.random() * 36)).toString(36)).join(''); | ||
export const randomString = (length) => [...Array(length)].map(() => (~~(crypto.randomInt(36))).toString(36)).join(''); | ||
|
||
export function randomChars(length, charSet) { | ||
if (!charSet) { | ||
charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
} | ||
let result = ''; | ||
for (let i = 0; i < length; i++) { | ||
result += charSet.charAt(Math.floor(Math.random() * charSet.length)); | ||
} | ||
return result; | ||
return [...Array(length)].map(() => charSet.charAt(crypto.randomInt(charSet.length))).join(''); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters