feat(tools): added text stats
This commit is contained in:
		
							parent
							
								
									67bc09ccfd
								
							
						
					
					
						commit
						7e80f27551
					
				
							
								
								
									
										89
									
								
								pages/tools/text/text-stats.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								pages/tools/text/text-stats.vue
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,89 @@ | ||||
| <template> | ||||
|   <ToolWrapper :config="config()"> | ||||
|     <v-textarea | ||||
|       v-model="text" | ||||
|       outlined | ||||
|       label="Input text" | ||||
|       hide-details | ||||
|       auto-grow | ||||
| 
 | ||||
|     /> | ||||
| 
 | ||||
|     <table> | ||||
|       <tr> | ||||
|         <td><strong>Character count:</strong></td> | ||||
|         <td>{{ textLength }}</td> | ||||
|       </tr> | ||||
|       <tr> | ||||
|         <td><strong>Word count:</strong></td> | ||||
|         <td>{{ textWordCount }}</td> | ||||
|       </tr> | ||||
|       <tr> | ||||
|         <td><strong>Line count:</strong></td> | ||||
|         <td>{{ textLineCount }}</td> | ||||
|       </tr> | ||||
|       <tr> | ||||
|         <td><strong>Byte size:</strong></td> | ||||
|         <td>{{ textSize }}</td> | ||||
|       </tr> | ||||
|     </table> | ||||
|   </ToolWrapper> | ||||
| </template> | ||||
| 
 | ||||
| <script lang="ts"> | ||||
| import {Component} from 'nuxt-property-decorator' | ||||
| import {CopyableMixin} from '~/mixins/copyable.mixin' | ||||
| import Tool from '~/components/Tool.vue' | ||||
| import type {ToolConfig} from '~/types/ToolConfig' | ||||
| import {formatBytes} from '~/utils/convert'; | ||||
| 
 | ||||
| @Component({ | ||||
|   mixins: [CopyableMixin] | ||||
| }) | ||||
| export default class TextStats extends Tool { | ||||
|   config(): ToolConfig { | ||||
|     return { | ||||
|       title: 'Text stats', | ||||
|       description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus distinctio dolor dolorum eaque eligendi, facilis impedit laboriosam odit placeat.', | ||||
|       icon: 'mdi-text', | ||||
|       keywords: ['length', 'character', 'count'] | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.' | ||||
| 
 | ||||
|   get textLength() { | ||||
|     return this.text.length | ||||
|   } | ||||
| 
 | ||||
|   get textWordCount() { | ||||
|     return this.text.split(/\s+/).length | ||||
|   } | ||||
| 
 | ||||
|   get textLineCount() { | ||||
|     return this.text.split(/\r\n|\r|\n/).length | ||||
|   } | ||||
| 
 | ||||
|   get textSize() { | ||||
|     return formatBytes(Uint8Array.from(this.text).buffer.byteLength, 3) | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| </script> | ||||
| 
 | ||||
| <style scoped lang="less"> | ||||
| table { | ||||
|   width: 100%; | ||||
| 
 | ||||
|   tr { | ||||
|     td { | ||||
|       width: 50%; | ||||
|       padding: 5px; | ||||
| 
 | ||||
|       &:first-child { | ||||
|         text-align: right; | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| } | ||||
| </style> | ||||
| @ -1,7 +1,20 @@ | ||||
| const base64ToString = (str: string) => Buffer.from(str, 'base64').toString('utf-8') | ||||
| const stringToBase64 = (str: string) => Buffer.from(str, 'utf-8').toString('base64') | ||||
| 
 | ||||
| const formatBytes = (bytes: number, decimals = 2) => { | ||||
|   if (bytes === 0) { | ||||
|     return '0 Bytes' | ||||
|   } | ||||
| 
 | ||||
|   const k = 1024 | ||||
|   const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] | ||||
|   const i = Math.floor(Math.log(bytes) / Math.log(k)) | ||||
| 
 | ||||
|   return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i] | ||||
| } | ||||
| 
 | ||||
| export { | ||||
|   stringToBase64, | ||||
|   base64ToString | ||||
|   base64ToString, | ||||
|   formatBytes | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user