feat: prevent non-integer bases
Signed-off-by: Corentin Thomasset <corentin.thomasset74@gmail.com>
This commit is contained in:
		
							parent
							
								
									0bf13326c8
								
							
						
					
					
						commit
						45112d1b33
					
				| @ -9,6 +9,7 @@ | ||||
|                             outlined | ||||
|                             type="number" | ||||
|                             v-model="inputBase" | ||||
|                             ref="inputBase" | ||||
|                             hide-details="auto" | ||||
|                             :rules="baseRules" | ||||
|                     /> | ||||
| @ -33,6 +34,7 @@ | ||||
|                             outlined | ||||
|                             type="number" | ||||
|                             v-model="outputBase" | ||||
|                             ref="outputBase" | ||||
|                             :rules="baseRules" | ||||
|                     /> | ||||
|                 </v-col> | ||||
| @ -54,7 +56,7 @@ | ||||
| </template> | ||||
| 
 | ||||
| <script> | ||||
|     import {copyToClipboard} from "../../utils/helpers"; | ||||
|     import {copyToClipboard, isInt} from "../../utils/helpers"; | ||||
| 
 | ||||
|     const convertBase = (value, fromBase, toBase) => { | ||||
|         const range = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'.split(''); | ||||
| @ -83,12 +85,17 @@ | ||||
|                 inputBase: 10, | ||||
|                 outputBase: 16, | ||||
|                 baseRules: [ | ||||
|                     v => isInt(v) || 'Base should be an integer', | ||||
|                     v => !!v || 'Required', | ||||
|                     v => v > 1 || 'Base should be > 1', | ||||
|                     v => v <= 64 || 'Base should be <= 64', | ||||
|                     v => v <= 64 || 'Base should be <= 64' | ||||
|                 ], | ||||
|                 isMounted: false | ||||
|             } | ||||
|         }, | ||||
|         mounted() { | ||||
|             this.isMounted = true; | ||||
|         }, | ||||
|         methods: { | ||||
|             copy() { | ||||
|                 copyToClipboard(this.outputNumber); | ||||
| @ -97,11 +104,15 @@ | ||||
|         }, | ||||
|         computed: { | ||||
|             outputNumber() { | ||||
|                 if(this.isMounted && this.$refs.inputBase.validate() && this.$refs.outputBase.validate()){ | ||||
|                     try{ | ||||
|                         return convertBase(this.inputNumber, this.inputBase, this.outputBase) | ||||
|                     }catch (e) { | ||||
|                         return e.message; | ||||
|                     } | ||||
|                 }else { | ||||
|                     return '' | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @ -24,8 +24,13 @@ const formatBytes = (bytes, decimals = 2) => { | ||||
|     return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; | ||||
| } | ||||
| 
 | ||||
| const isInt = (value) => { | ||||
|     return !isNaN(value) && ((x) => (x | 0) === x)(parseFloat(value)) | ||||
| } | ||||
| 
 | ||||
| export { | ||||
|     copyToClipboard, | ||||
|     fileIsImage, | ||||
|     formatBytes | ||||
|     formatBytes, | ||||
|     isInt | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user