feat: BaseConverter
Signed-off-by: Corentin Thomasset <corentin.thomasset74@gmail.com>
This commit is contained in:
		
							parent
							
								
									e5969a534f
								
							
						
					
					
						commit
						0c120a3c5a
					
				| @ -8,6 +8,7 @@ import UrlEncoder from "./routes/tools/UrlEncoder"; | |||||||
| import FileToBase64 from "./routes/tools/FileToBase64"; | import FileToBase64 from "./routes/tools/FileToBase64"; | ||||||
| import TextCypher from "./routes/tools/TextCypher"; | import TextCypher from "./routes/tools/TextCypher"; | ||||||
| import TextStats from "./routes/tools/TextStats"; | import TextStats from "./routes/tools/TextStats"; | ||||||
|  | import BaseConverter from "./routes/tools/BaseConverter"; | ||||||
| 
 | 
 | ||||||
| Vue.use(VueRouter) | Vue.use(VueRouter) | ||||||
| 
 | 
 | ||||||
| @ -21,19 +22,22 @@ const toolsComponents = [ | |||||||
|                 text: 'Token generator', |                 text: 'Token generator', | ||||||
|                 path: '/token-generator', |                 path: '/token-generator', | ||||||
|                 component: TokenGenerator, |                 component: TokenGenerator, | ||||||
|                 keywords: ['md5'] |                 keywords: ['token', 'random', 'string', 'alphanumeric'] | ||||||
|             }, |             }, | ||||||
|             { |             { | ||||||
|                 icon: 'fa-font', |                 icon: 'fa-font', | ||||||
|                 text: 'Hash text', |                 text: 'Hash text', | ||||||
|                 path: '/hash', |                 path: '/hash', | ||||||
|                 component: Hash |                 component: Hash, | ||||||
|  |                 keywords: ['md5', 'sha1', 'sha256', 'sha224', 'sha512', 'sha384', 'sha3', 'ripemd160', 'random'] | ||||||
|  | 
 | ||||||
|             }, |             }, | ||||||
|             { |             { | ||||||
|                 icon: 'fa-lock', |                 icon: 'fa-lock', | ||||||
|                 text: 'Cypher/uncypher text', |                 text: 'Cypher/uncypher text', | ||||||
|                 path: '/cypher', |                 path: '/cypher', | ||||||
|                 component: TextCypher |                 component: TextCypher, | ||||||
|  |                 keywords: ['aes', 'tripledes', 'rabbit', 'rabbitlegacy', 'rc4'] | ||||||
|             }, |             }, | ||||||
|         ], |         ], | ||||||
|     }, |     }, | ||||||
| @ -44,7 +48,15 @@ const toolsComponents = [ | |||||||
|                 icon: 'fa-calendar', |                 icon: 'fa-calendar', | ||||||
|                 text: 'Date/Time converter', |                 text: 'Date/Time converter', | ||||||
|                 path: '/date-converter', |                 path: '/date-converter', | ||||||
|                 component: DateConverter |                 component: DateConverter, | ||||||
|  |                 keywords: ['locale', 'format', 'iso 8601', 'utc', 'timestamp', 'unix', 'year', 'month', 'day', 'hours', 'minutes', 'seconds'] | ||||||
|  |             }, | ||||||
|  |             { | ||||||
|  |                 icon: 'fa-arrows-h', | ||||||
|  |                 text: 'Base converter', | ||||||
|  |                 path: '/base-converter', | ||||||
|  |                 component: BaseConverter, | ||||||
|  |                 keywords: ['binary', 'hexadecimal', 'decimal'] | ||||||
|             }, |             }, | ||||||
|         ], |         ], | ||||||
|     }, |     }, | ||||||
| @ -55,7 +67,8 @@ const toolsComponents = [ | |||||||
|                 icon: 'fa-link', |                 icon: 'fa-link', | ||||||
|                 text: 'URL encode/decode', |                 text: 'URL encode/decode', | ||||||
|                 path: '/url-encoder', |                 path: '/url-encoder', | ||||||
|                 component: UrlEncoder |                 component: UrlEncoder, | ||||||
|  |                 keywords: ['%20'] | ||||||
|             }, |             }, | ||||||
|             { |             { | ||||||
|                 icon: 'fa-file-image-o', |                 icon: 'fa-file-image-o', | ||||||
| @ -72,7 +85,8 @@ const toolsComponents = [ | |||||||
|                 icon: 'fa-file-text', |                 icon: 'fa-file-text', | ||||||
|                 text: 'Text stats', |                 text: 'Text stats', | ||||||
|                 path: '/text-stats', |                 path: '/text-stats', | ||||||
|                 component: TextStats |                 component: TextStats, | ||||||
|  |                 keywords: ['word', 'count', 'size', 'bytes', 'length'] | ||||||
|             }, |             }, | ||||||
|         ], |         ], | ||||||
|     } |     } | ||||||
|  | |||||||
							
								
								
									
										112
									
								
								src/routes/tools/BaseConverter.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								src/routes/tools/BaseConverter.vue
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,112 @@ | |||||||
|  | <template> | ||||||
|  |     <v-card class="single-card"> | ||||||
|  |         <v-card-title>Base converter</v-card-title> | ||||||
|  |         <v-card-text> | ||||||
|  |             <v-row> | ||||||
|  |                 <v-col cols="12" sm="4"> | ||||||
|  |                     <v-text-field | ||||||
|  |                             label="Input base" | ||||||
|  |                             outlined | ||||||
|  |                             type="number" | ||||||
|  |                             v-model="inputBase" | ||||||
|  |                             hide-details="auto" | ||||||
|  |                             :rules="baseRules" | ||||||
|  |                     /> | ||||||
|  |                 </v-col> | ||||||
|  |                 <v-col cols="12" sm="8"> | ||||||
|  |                     <v-text-field | ||||||
|  |                             ref="inputField" | ||||||
|  |                             label="Input number" | ||||||
|  |                             outlined | ||||||
|  |                             v-model="inputNumber" | ||||||
|  |                             hide-details="auto" | ||||||
|  |                     /> | ||||||
|  |                 </v-col> | ||||||
|  |             </v-row> | ||||||
|  |             <br> | ||||||
|  |             <v-divider/> | ||||||
|  |             <br> | ||||||
|  |             <v-row> | ||||||
|  |                 <v-col cols="12" sm="4"> | ||||||
|  |                     <v-text-field | ||||||
|  |                             label="Output base" | ||||||
|  |                             outlined | ||||||
|  |                             type="number" | ||||||
|  |                             v-model="outputBase" | ||||||
|  |                             :rules="baseRules" | ||||||
|  |                     /> | ||||||
|  |                 </v-col> | ||||||
|  |                 <v-col cols="12" sm="8"> | ||||||
|  |                     <v-text-field | ||||||
|  |                             label="Output number" | ||||||
|  |                             outlined | ||||||
|  |                             v-model="outputNumber" | ||||||
|  |                             readonly | ||||||
|  |                             append-icon="fa-clipboard" | ||||||
|  |                             @click:append="copy" | ||||||
|  |                     /> | ||||||
|  |                 </v-col> | ||||||
|  |             </v-row> | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         </v-card-text> | ||||||
|  |     </v-card> | ||||||
|  | </template> | ||||||
|  | 
 | ||||||
|  | <script> | ||||||
|  |     import {copyToClipboard} from "../../utils/helpers"; | ||||||
|  | 
 | ||||||
|  |     const convertBase = (value, fromBase, toBase) => { | ||||||
|  |         const range = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'.split(''); | ||||||
|  |         const fromRange = range.slice(0, fromBase); | ||||||
|  |         const toRange = range.slice(0, toBase); | ||||||
|  | 
 | ||||||
|  |         let decValue = value.split('').reverse().reduce((carry, digit, index) => { | ||||||
|  |             if (fromRange.indexOf(digit) === -1) throw new Error('Invalid digit `' + digit + '` for base ' + fromBase + '.'); | ||||||
|  |             return carry += fromRange.indexOf(digit) * (Math.pow(fromBase, index)); | ||||||
|  |         }, 0); | ||||||
|  | 
 | ||||||
|  |         let newValue = ''; | ||||||
|  |         while (decValue > 0) { | ||||||
|  |             newValue = toRange[decValue % toBase] + newValue; | ||||||
|  |             decValue = (decValue - (decValue % toBase)) / toBase; | ||||||
|  |         } | ||||||
|  |         return newValue || '0'; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     export default { | ||||||
|  |         name: "BaseConverter", | ||||||
|  |         data() { | ||||||
|  |             return { | ||||||
|  |                 inputError:'', | ||||||
|  |                 inputNumber: '42', | ||||||
|  |                 inputBase: 10, | ||||||
|  |                 outputBase: 16, | ||||||
|  |                 baseRules: [ | ||||||
|  |                     v => !!v || 'Required', | ||||||
|  |                     v => v > 1 || 'Base should be > 1', | ||||||
|  |                     v => v <= 64 || 'Base should be <= 64', | ||||||
|  |                 ], | ||||||
|  |             } | ||||||
|  |         }, | ||||||
|  |         methods: { | ||||||
|  |             copy() { | ||||||
|  |                 copyToClipboard(this.outputNumber); | ||||||
|  |                 this.$toast.success('Copied to clipboard.') | ||||||
|  |             } | ||||||
|  |         }, | ||||||
|  |         computed: { | ||||||
|  |             outputNumber() { | ||||||
|  |                 try{ | ||||||
|  |                     return convertBase(this.inputNumber, this.inputBase, this.outputBase) | ||||||
|  |                 }catch (e) { | ||||||
|  |                     return e.message; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | </script> | ||||||
|  | 
 | ||||||
|  | <style scoped> | ||||||
|  | 
 | ||||||
|  | </style> | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user