Compare commits

..
Author SHA1 Message Date
ShareVB 920ceeaf40 fix(url-parser): handle repeated params
Fix #800 and #862
2024-02-11 10:42:37 +01:00
3 changed files with 33 additions and 13 deletions

No files matched your search

@@ -11,9 +11,6 @@ describe('integer-base-converter', () => {
expect(convertBase({ value: '10100101', fromBase: 2, toBase: 16 })).toEqual('a5');
expect(convertBase({ value: '192654', fromBase: 10, toBase: 8 })).toEqual('570216');
expect(convertBase({ value: 'zz', fromBase: 64, toBase: 10 })).toEqual('2275');
expect(convertBase({ value: '42540766411283223938465490632011909384', fromBase: 10, toBase: 10 })).toEqual('42540766411283223938465490632011909384');
expect(convertBase({ value: '42540766411283223938465490632011909384', fromBase: 10, toBase: 16 })).toEqual('20010db8000085a300000000ac1f8908');
expect(convertBase({ value: '20010db8000085a300000000ac1f8908', fromBase: 16, toBase: 10 })).toEqual('42540766411283223938465490632011909384');
});
});
});
@@ -5,16 +5,16 @@ export function convertBase({ value, fromBase, toBase }: { value: string; fromBa
let decValue = value
.split('')
.reverse()
.reduce((carry: bigint, digit: string, index: number) => {
.reduce((carry: number, digit: string, index: number) => {
if (!fromRange.includes(digit)) {
throw new Error(`Invalid digit "${digit}" for base ${fromBase}.`);
}
return (carry += BigInt(fromRange.indexOf(digit)) * BigInt(fromBase) ** BigInt(index));
}, 0n);
return (carry += fromRange.indexOf(digit) * fromBase ** index);
}, 0);
let newValue = '';
while (decValue > 0) {
newValue = toRange[Number(decValue % BigInt(toBase))] + newValue;
decValue = (decValue - (decValue % BigInt(toBase))) / BigInt(toBase);
newValue = toRange[decValue % toBase] + newValue;
decValue = (decValue - (decValue % toBase)) / toBase;
}
return newValue || '0';
}
+28 -5
View File
@@ -3,9 +3,32 @@ import InputCopyable from '../../components/InputCopyable.vue';
import { isNotThrowing } from '@/utils/boolean';
import { withDefaultOnError } from '@/utils/defaults';
const urlToParse = ref('https://me:pwd@it-tools.tech:3000/url-parser?key1=value&key2=value2#the-hash');
const urlToParse = ref('https://me:pwd@it-tools.tech:3000/url-parser?key=value&keyarr=value1&keyarr=value2&otherarg#the-hash');
const urlParsed = computed(() => withDefaultOnError(() => new URL(urlToParse.value), undefined));
const urlParsedParams = computed(() => {
const params: { key: string; value: string }[] = [];
const usedKeys = new Set();
for (const key of (urlParsed.value?.searchParams.keys() ?? [])) {
// searchParams.keys() reports as many times the key as it appears in the params, so use only first occurrence
if (usedKeys.has(key)) {
continue;
}
usedKeys.add(key);
const values = urlParsed.value?.searchParams.getAll(key) ?? [];
if (values.length > 1) {
// print out multiple values at the place of the first occurrence of param
let index = 0;
values.forEach((value) => {
params.push({ key: `${key}[${index}]`, value: (value ?? '') });
index += 1;
});
continue;
}
params.push({ key, value: (urlParsed.value?.searchParams.get(key) ?? '') });
}
return params;
});
const urlValidationRules = [
{
validator: (value: string) => isNotThrowing(() => new URL(value)),
@@ -49,8 +72,8 @@ const properties: { title: string; key: keyof URL }[] = [
/>
<div
v-for="[k, v] in Object.entries(Object.fromEntries(urlParsed?.searchParams.entries() ?? []))"
:key="k"
v-for="param in urlParsedParams"
:key="param.key"
mb-2
w-full
flex
@@ -59,8 +82,8 @@ const properties: { title: string; key: keyof URL }[] = [
<icon-mdi-arrow-right-bottom />
</div>
<InputCopyable :value="k" readonly />
<InputCopyable :value="v" readonly />
<InputCopyable :value="param.key" readonly />
<InputCopyable :value="param.value" readonly />
</div>
</c-card>
</template>