11<script setup lang="ts">
22import { useI18n } from ' vue-i18n' ;
33import InputCopyable from ' ../../components/InputCopyable.vue' ;
4- import { convertBase , hasNumberPrefix } from ' ./integer-base-converter.model' ;
4+ import { convertBase , hasNumberPrefix , formatWithSpaces } from ' ./integer-base-converter.model' ;
55import { getErrorMessageIfThrows } from ' @/utils/error' ;
66import { useQueryParam } from ' @/composable/queryParams' ;
77
@@ -21,9 +21,39 @@ const outputBase = useQueryParam({ tool: 'int-base-conv', name: 'outbase', defau
2121
2222const hasInputNumberPrefix = computed (() => hasNumberPrefix (input .value ));
2323
24- function errorlessConvert(... args : Parameters <typeof convertBase >) {
24+ const useSpaceSeparation = ref (false );
25+ const groupSizes = ref ({
26+ binary: 8 ,
27+ octal: 3 ,
28+ decimal: 3 ,
29+ hex: 4 ,
30+ base64: 4 ,
31+ custom: 4 ,
32+ });
33+
34+ function getGroupSizeForBase(base : number ): number {
35+ if (! useSpaceSeparation .value ) {
36+ return 0 ;
37+ }
38+ switch (base ) {
39+ case 2 : return groupSizes .value .binary ;
40+ case 8 : return groupSizes .value .octal ;
41+ case 10 : return groupSizes .value .decimal ;
42+ case 16 : return groupSizes .value .hex ;
43+ case 64 : return groupSizes .value .base64 ;
44+ default :
45+ return base === outputBase .value ? groupSizes .value .custom : 4 ;
46+ }
47+ }
48+
49+ function formattedConvert(args : Parameters <typeof convertBase >[0 ]) {
2550 try {
26- return convertBase (... args );
51+ const converted = convertBase (args );
52+ const size = getGroupSizeForBase (args .toBase );
53+ if (useSpaceSeparation .value && size > 0 ) {
54+ return formatWithSpaces (converted , size );
55+ }
56+ return converted ;
2757 }
2858 catch (err ) {
2959 return ' ' ;
@@ -46,6 +76,33 @@ const error = computed(() =>
4676 <n-input-number-i18n v-model:value =" inputBase" max =" 64" min =" 2" :placeholder =" t('tools.integer-base-converter.texts.placeholder-put-your-input-base-here-ex-10')" w-full />
4777 </n-form-item >
4878
79+ <n-form-item :label =" t('tools.integer-base-converter.texts.label-space-separation')" label-placement =" left" label-width =" 110" :show-feedback =" false" style =" margin-top : 10px ;" >
80+ <n-switch v-model:value =" useSpaceSeparation" />
81+ </n-form-item >
82+
83+ <div v-if =" useSpaceSeparation" style =" margin-top : 10px ; margin-bottom : 20px ; padding-left : 20px ; border-left : 2px solid var (--n-border-color )" >
84+ <div style =" display : grid ; grid-template-columns : repeat (auto-fill , minmax (140px , 1fr )); gap : 10px ;" >
85+ <n-form-item :label =" t('tools.integer-base-converter.texts.label-binary-size')" label-placement =" top" :show-feedback =" false" >
86+ <n-input-number v-model:value =" groupSizes.binary" :min =" 1" :max =" 64" size =" small" />
87+ </n-form-item >
88+ <n-form-item :label =" t('tools.integer-base-converter.texts.label-octal-size')" label-placement =" top" :show-feedback =" false" >
89+ <n-input-number v-model:value =" groupSizes.octal" :min =" 1" :max =" 64" size =" small" />
90+ </n-form-item >
91+ <n-form-item :label =" t('tools.integer-base-converter.texts.label-decimal-size')" label-placement =" top" :show-feedback =" false" >
92+ <n-input-number v-model:value =" groupSizes.decimal" :min =" 1" :max =" 64" size =" small" />
93+ </n-form-item >
94+ <n-form-item :label =" t('tools.integer-base-converter.texts.label-hexadecimal-size')" label-placement =" top" :show-feedback =" false" >
95+ <n-input-number v-model:value =" groupSizes.hex" :min =" 1" :max =" 64" size =" small" />
96+ </n-form-item >
97+ <n-form-item :label =" t('tools.integer-base-converter.texts.label-base64-size')" label-placement =" top" :show-feedback =" false" >
98+ <n-input-number v-model:value =" groupSizes.base64" :min =" 1" :max =" 64" size =" small" />
99+ </n-form-item >
100+ <n-form-item :label =" t('tools.integer-base-converter.texts.label-custom-size')" label-placement =" top" :show-feedback =" false" >
101+ <n-input-number v-model:value =" groupSizes.custom" :min =" 1" :max =" 64" size =" small" />
102+ </n-form-item >
103+ </div >
104+ </div >
105+
49106 <n-alert v-if =" error" style =" margin-top : 25px " type =" error" >
50107 {{ error }}
51108 </n-alert >
@@ -54,35 +111,35 @@ const error = computed(() =>
54111 <InputCopyable
55112 :label =" t (' tools.integer-base-converter.texts.label-binary-2' )"
56113 v-bind =" inputProps "
57- :value =" errorlessConvert ({ value: input , fromBase: inputBase , toBase: 2 })"
114+ :value =" formattedConvert ({ value: input , fromBase: inputBase , toBase: 2 })"
58115 :placeholder =" t (' tools.integer-base-converter.texts.placeholder-binary-version-will-be-here' )"
59116 />
60117
61118 <InputCopyable
62119 :label =" t (' tools.integer-base-converter.texts.label-octal-8' )"
63120 v-bind =" inputProps "
64- :value =" errorlessConvert ({ value: input , fromBase: inputBase , toBase: 8 })"
121+ :value =" formattedConvert ({ value: input , fromBase: inputBase , toBase: 8 })"
65122 :placeholder =" t (' tools.integer-base-converter.texts.placeholder-octal-version-will-be-here' )"
66123 />
67124
68125 <InputCopyable
69126 :label =" t (' tools.integer-base-converter.texts.label-decimal-10' )"
70127 v-bind =" inputProps "
71- :value =" errorlessConvert ({ value: input , fromBase: inputBase , toBase: 10 })"
128+ :value =" formattedConvert ({ value: input , fromBase: inputBase , toBase: 10 })"
72129 :placeholder =" t (' tools.integer-base-converter.texts.placeholder-decimal-version-will-be-here' )"
73130 />
74131
75132 <InputCopyable
76133 :label =" t (' tools.integer-base-converter.texts.label-hexadecimal-16' )"
77134 v-bind =" inputProps "
78- :value =" errorlessConvert ({ value: input , fromBase: inputBase , toBase: 16 })"
135+ :value =" formattedConvert ({ value: input , fromBase: inputBase , toBase: 16 })"
79136 :placeholder =" t (' tools.integer-base-converter.texts.placeholder-hexadecimal-version-will-be-here' )"
80137 />
81138
82139 <InputCopyable
83140 :label =" t (' tools.integer-base-converter.texts.label-base64-64' )"
84141 v-bind =" inputProps "
85- :value =" errorlessConvert ({ value: input , fromBase: inputBase , toBase: 64 })"
142+ :value =" formattedConvert ({ value: input , fromBase: inputBase , toBase: 64 })"
86143 :placeholder =" t (' tools.integer-base-converter.texts.placeholder-base64-version-will-be-here' )"
87144 />
88145
@@ -95,7 +152,7 @@ const error = computed(() =>
95152 <InputCopyable
96153 flex-1
97154 v-bind =" inputProps "
98- :value =" errorlessConvert ({ value: input , fromBase: inputBase , toBase: outputBase })"
155+ :value =" formattedConvert ({ value: input , fromBase: inputBase , toBase: outputBase })"
99156 :placeholder =" ` Base ${outputBase } will be here... ` "
100157 />
101158 </div >
0 commit comments