refactor: improved shuffle function (now using Durstenfeld shuffle)
This commit is contained in:
		
							parent
							
								
									436d960603
								
							
						
					
					
						commit
						1ebfe83d20
					
				| @ -43,13 +43,13 @@ path: '/bip39-generator' | ||||
| 
 | ||||
| <script lang="ts"> | ||||
| import * as bip39 from 'bip39' | ||||
| import {shuffle} from '@/utils/string' | ||||
| import {Component, Ref} from 'nuxt-property-decorator' | ||||
| import {CopyableMixin} from '@/mixins/copyable.mixin' | ||||
| import Tool from '@/components/Tool.vue' | ||||
| import type {VForm} from '~/types/VForm' | ||||
| import {shuffleString} from '~/utils/random' | ||||
| 
 | ||||
| const getRandomBuffer = () => Buffer.from(shuffle('0123456789abcdef'.repeat(16)).substring(0, 32), 'hex') | ||||
| const getRandomBuffer = () => Buffer.from(shuffleString('0123456789abcdef'.repeat(16)).substring(0, 32), 'hex') | ||||
| 
 | ||||
| @Component({ | ||||
|   mixins: [CopyableMixin] | ||||
|  | ||||
| @ -38,7 +38,7 @@ path: '/token-generator' | ||||
| import {Component, Watch} from 'nuxt-property-decorator' | ||||
| import Tool from '~/components/Tool.vue' | ||||
| import {CopyableMixin} from '~/mixins/copyable.mixin' | ||||
| import {shuffle} from '~/utils/string' | ||||
| import {shuffleString} from '~/utils/random' | ||||
| 
 | ||||
| const lowercase = 'abcdefghijklmopqrstuvwxyz' | ||||
| const uppercase = 'ABCDEFGHIJKLMOPQRSTUVWXYZ' | ||||
| @ -78,7 +78,7 @@ export default class TokenGenerator extends Tool { | ||||
|       result += specials | ||||
|     } | ||||
| 
 | ||||
|     this.token = shuffle(result.repeat(this.config.length)).substring(0, this.config.length) | ||||
|     this.token = shuffleString(result.repeat(this.config.length)).substring(0, this.config.length) | ||||
|   } | ||||
| } | ||||
| </script> | ||||
|  | ||||
| @ -6,8 +6,25 @@ const randFromArray = (array: any[]) => array[Math.floor(random() * array.length | ||||
| 
 | ||||
| const randIntFromInterval = (min: number, max: number) => Math.floor(random() * (max - min) + min) | ||||
| 
 | ||||
| // Durstenfeld shuffle
 | ||||
| const shuffleArrayMutate = <T extends unknown>(array: T[]): T[] => { | ||||
|   for (let i = array.length - 1; i > 0; i--) { | ||||
|     const j = Math.floor(Math.random() * (i + 1)); | ||||
|     [array[i], array[j]] = [array[j], array[i]]; | ||||
|   } | ||||
| 
 | ||||
|   return array | ||||
| } | ||||
| 
 | ||||
| const shuffleArray = <T extends unknown>(array: T[]): T[] => shuffleArrayMutate([...array]) | ||||
| 
 | ||||
| const shuffleString = (str: string, delimiter: string = ''): string => shuffleArrayMutate(str.split(delimiter)).join(delimiter) | ||||
| 
 | ||||
| export { | ||||
|   randFromArray, | ||||
|   randIntFromInterval, | ||||
|   random | ||||
|   random, | ||||
|   shuffleArray, | ||||
|   shuffleArrayMutate, | ||||
|   shuffleString | ||||
| } | ||||
|  | ||||
| @ -1,5 +1,3 @@ | ||||
| const capitalise = (s: string) => s.charAt(0).toUpperCase() + s.slice(1) | ||||
| 
 | ||||
| const shuffle = (s: string) => s.split('').sort(() => 0.5 - Math.random()).join('') | ||||
| 
 | ||||
| export {capitalise, shuffle} | ||||
| export {capitalise} | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user