mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 14:01:42 +03:00
Merge branch 'fix/integer-base-converter' into chore/all-my-stuffs
# Conflicts: # src/tools/integer-base-converter/integer-base-converter.model.ts
This commit is contained in:
commit
2ce4d1ba50
2 files changed
+52
-2
No files matched your search
@@ -2,6 +2,48 @@ export function hasNumberPrefix(value: string) {
|
|||||||
return (value ?? '').trim().match(/^(0[xob].|&[hob].)/i);
|
return (value ?? '').trim().match(/^(0[xob].|&[hob].)/i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function convertBase(
|
||||||
|
{
|
||||||
|
value, fromBase, toBase,
|
||||||
|
ignorePunctuationsRegexChars = ' \u00A0_\.,-',
|
||||||
|
handlePrefixSuffix = true,
|
||||||
|
ignoreCase = true,
|
||||||
|
}: {
|
||||||
|
value: string
|
||||||
|
fromBase: number
|
||||||
|
toBase: number
|
||||||
|
ignorePunctuationsRegexChars?: string
|
||||||
|
handlePrefixSuffix?: boolean
|
||||||
|
ignoreCase?: boolean
|
||||||
|
}) {
|
||||||
|
let cleanedValue = (value ?? '0').trim();
|
||||||
|
if (ignorePunctuationsRegexChars) {
|
||||||
|
cleanedValue = cleanedValue.replace(new RegExp(`[${ignorePunctuationsRegexChars}]`, 'g'), '');
|
||||||
|
}
|
||||||
|
let finalFromBase = fromBase;
|
||||||
|
if (handlePrefixSuffix) {
|
||||||
|
for (const regBase of [
|
||||||
|
{ base: 2, regex: /^(&b|0b)?([01]+)([IULZn]*)$/i },
|
||||||
|
{ base: 8, regex: /^(&o|0o)?([0-7]+)([IULZn]*)$/i },
|
||||||
|
{ base: 16, regex: /^(&h|0x)?([a-f0-9]+)([IULZn]*)$/i },
|
||||||
|
]) {
|
||||||
|
const match = cleanedValue.match(regBase.regex);
|
||||||
|
if (match) {
|
||||||
|
if (match[1]) {
|
||||||
|
finalFromBase = regBase.base;
|
||||||
|
}
|
||||||
|
cleanedValue = match[2];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ignoreCase && finalFromBase <= 36) {
|
||||||
|
cleanedValue = cleanedValue.toLowerCase();
|
||||||
|
}
|
||||||
|
export function hasNumberPrefix(value: string) {
|
||||||
|
return (value ?? '').trim().match(/^(0[xob].|&[hob].)/i);
|
||||||
|
}
|
||||||
|
|
||||||
export function convertBase(
|
export function convertBase(
|
||||||
{
|
{
|
||||||
value, fromBase, toBase,
|
value, fromBase, toBase,
|
||||||
@@ -42,13 +84,17 @@ export function convertBase(
|
|||||||
}
|
}
|
||||||
const range = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'.split('');
|
const range = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'.split('');
|
||||||
const fromRange = range.slice(0, finalFromBase);
|
const fromRange = range.slice(0, finalFromBase);
|
||||||
|
const fromRange = range.slice(0, finalFromBase);
|
||||||
const toRange = range.slice(0, toBase);
|
const toRange = range.slice(0, toBase);
|
||||||
|
let decValue = cleanedValue
|
||||||
let decValue = cleanedValue
|
let decValue = cleanedValue
|
||||||
.split('')
|
.split('')
|
||||||
.reverse()
|
.reverse()
|
||||||
|
.reduce((carry: bigint, digit: string, index: number) => {
|
||||||
.reduce((carry: bigint, digit: string, index: number) => {
|
.reduce((carry: bigint, digit: string, index: number) => {
|
||||||
if (!fromRange.includes(digit)) {
|
if (!fromRange.includes(digit)) {
|
||||||
throw new Error(`Invalid digit "${digit}" for base ${finalFromBase}.`);
|
throw new Error(`Invalid digit "${digit}" for base ${finalFromBase}.`);
|
||||||
|
throw new Error(`Invalid digit "${digit}" for base ${finalFromBase}.`);
|
||||||
}
|
}
|
||||||
return (carry += BigInt(fromRange.indexOf(digit)) * BigInt(fromBase) ** BigInt(index));
|
return (carry += BigInt(fromRange.indexOf(digit)) * BigInt(fromBase) ** BigInt(index));
|
||||||
}, 0n);
|
}, 0n);
|
||||||
@@ -56,6 +102,8 @@ export function convertBase(
|
|||||||
while (decValue > 0) {
|
while (decValue > 0) {
|
||||||
newValue = toRange[Number(decValue % BigInt(toBase))] + newValue;
|
newValue = toRange[Number(decValue % BigInt(toBase))] + newValue;
|
||||||
decValue = (decValue - (decValue % BigInt(toBase))) / BigInt(toBase);
|
decValue = (decValue - (decValue % BigInt(toBase))) / BigInt(toBase);
|
||||||
|
newValue = toRange[Number(decValue % BigInt(toBase))] + newValue;
|
||||||
|
decValue = (decValue - (decValue % BigInt(toBase))) / BigInt(toBase);
|
||||||
}
|
}
|
||||||
return newValue || '0';
|
return newValue || '0';
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import InputCopyable from '../../components/InputCopyable.vue';
|
import InputCopyable from '../../components/InputCopyable.vue';
|
||||||
import { convertBase } from './integer-base-converter.model';
|
import { convertBase, hasNumberPrefix } from './integer-base-converter.model';
|
||||||
import { getErrorMessageIfThrows } from '@/utils/error';
|
import { getErrorMessageIfThrows } from '@/utils/error';
|
||||||
|
|
||||||
const inputProps = {
|
const inputProps = {
|
||||||
@@ -15,6 +15,8 @@ const input = ref('42');
|
|||||||
const inputBase = ref(10);
|
const inputBase = ref(10);
|
||||||
const outputBase = ref(42);
|
const outputBase = ref(42);
|
||||||
|
|
||||||
|
const hasInputNumberPrefix = computed(() => hasNumberPrefix(input.value));
|
||||||
|
|
||||||
function errorlessConvert(...args: Parameters<typeof convertBase>) {
|
function errorlessConvert(...args: Parameters<typeof convertBase>) {
|
||||||
try {
|
try {
|
||||||
return convertBase(...args);
|
return convertBase(...args);
|
||||||
@@ -36,7 +38,7 @@ const error = computed(() =>
|
|||||||
<c-card>
|
<c-card>
|
||||||
<c-input-text v-model:value="input" label="Input number" placeholder="Put your number here (ex: 42)" label-position="left" label-width="110px" mb-2 label-align="right" />
|
<c-input-text v-model:value="input" label="Input number" placeholder="Put your number here (ex: 42)" label-position="left" label-width="110px" mb-2 label-align="right" />
|
||||||
|
|
||||||
<n-form-item label="Input base" label-placement="left" label-width="110" :show-feedback="false">
|
<n-form-item v-if="!hasInputNumberPrefix" label="Input base" label-placement="left" label-width="110" :show-feedback="false">
|
||||||
<n-input-number v-model:value="inputBase" max="64" min="2" placeholder="Put your input base here (ex: 10)" w-full />
|
<n-input-number v-model:value="inputBase" max="64" min="2" placeholder="Put your input base here (ex: 10)" w-full />
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user