mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-23 08:16:16 -04:00
fix(sec): switched to cryptographically secure random
This commit is contained in:
parent
03a7e8c42d
commit
74e5758735
2 changed files with 11 additions and 4 deletions
|
@ -1,8 +1,13 @@
|
||||||
const randFromArray = (array: any[]) => array[Math.floor(Math.random() * array.length)]
|
import {randomBytes} from 'crypto'
|
||||||
|
|
||||||
const randIntFromInterval = (min: number, max: number) => Math.floor(Math.random() * (max - min) + min)
|
const random = () => randomBytes(4).readUInt32LE(0) / 0x100000000
|
||||||
|
|
||||||
|
const randFromArray = (array: any[]) => array[Math.floor(random() * array.length)]
|
||||||
|
|
||||||
|
const randIntFromInterval = (min: number, max: number) => Math.floor(random() * (max - min) + min)
|
||||||
|
|
||||||
export {
|
export {
|
||||||
randFromArray,
|
randFromArray,
|
||||||
randIntFromInterval
|
randIntFromInterval,
|
||||||
|
random
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
|
import {random} from './random'
|
||||||
|
|
||||||
const capitalise = (s: string) => s.charAt(0).toUpperCase() + s.slice(1)
|
const capitalise = (s: string) => s.charAt(0).toUpperCase() + s.slice(1)
|
||||||
|
|
||||||
const shuffle = (s: string) => s.split('').sort(() => 0.5 - Math.random()).join('')
|
const shuffle = (s: string) => s.split('').sort(() => 0.5 - random()).join('')
|
||||||
|
|
||||||
export {capitalise, shuffle}
|
export {capitalise, shuffle}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue