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