feat: prevent non-integer bases

Signed-off-by: Corentin Thomasset <corentin.thomasset74@gmail.com>
This commit is contained in:
Corentin Thomasset 2020-06-07 19:06:07 +02:00 committed by Corentin THOMASSET
parent 0bf13326c8
commit 45112d1b33
2 changed files with 23 additions and 7 deletions

View file

@ -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,11 +104,15 @@
}, },
computed: { computed: {
outputNumber() { outputNumber() {
if(this.isMounted && this.$refs.inputBase.validate() && this.$refs.outputBase.validate()){
try{ try{
return convertBase(this.inputNumber, this.inputBase, this.outputBase) return convertBase(this.inputNumber, this.inputBase, this.outputBase)
}catch (e) { }catch (e) {
return e.message; return e.message;
} }
}else {
return ''
}
} }
} }
} }

View file

@ -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
} }