Skip to content

Commit cc33329

Browse files
committed
fix(url-parser): handle repeated params
Fix CorentinTh#800 and CorentinTh#862
1 parent efe62da commit cc33329

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/tools/url-parser/url-parser.vue

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,32 @@ import InputCopyable from '../../components/InputCopyable.vue';
33
import { isNotThrowing } from '@/utils/boolean';
44
import { withDefaultOnError } from '@/utils/defaults';
55
6-
const urlToParse = ref('https://me:[email protected]:3000/url-parser?key1=value&key2=value2#the-hash');
6+
const urlToParse = ref('https://me:[email protected]:3000/url-parser?key=value&keyarr=value1&keyarr=value2&otherarg#the-hash');
77
88
const urlParsed = computed(() => withDefaultOnError(() => new URL(urlToParse.value), undefined));
9+
const urlParsedParams = computed(() => {
10+
const params: { key: string; value: string }[] = [];
11+
const usedKeys = new Set();
12+
for (const key of (urlParsed.value?.searchParams.keys() ?? [])) {
13+
// searchParams.keys() reports as many times the key as it appears in the params, so use only first occurrence
14+
if (usedKeys.has(key)) {
15+
continue;
16+
}
17+
usedKeys.add(key);
18+
const values = urlParsed.value?.searchParams.getAll(key) ?? [];
19+
if (values.length > 1) {
20+
// print out multiple values at the place of the first occurrence of param
21+
let index = 0;
22+
values.forEach((value) => {
23+
params.push({ key: `${key}[${index}]`, value: (value ?? '') });
24+
index += 1;
25+
});
26+
continue;
27+
}
28+
params.push({ key, value: (urlParsed.value?.searchParams.get(key) ?? '') });
29+
}
30+
return params;
31+
});
932
const urlValidationRules = [
1033
{
1134
validator: (value: string) => isNotThrowing(() => new URL(value)),
@@ -49,8 +72,8 @@ const properties: { title: string; key: keyof URL }[] = [
4972
/>
5073

5174
<div
52-
v-for="[k, v] in Object.entries(Object.fromEntries(urlParsed?.searchParams.entries() ?? []))"
53-
:key="k"
75+
v-for="param in urlParsedParams"
76+
:key="param.key"
5477
mb-2
5578
w-full
5679
flex
@@ -59,8 +82,8 @@ const properties: { title: string; key: keyof URL }[] = [
5982
<icon-mdi-arrow-right-bottom />
6083
</div>
6184

62-
<InputCopyable :value="k" readonly />
63-
<InputCopyable :value="v" readonly />
85+
<InputCopyable :value="param.key" readonly />
86+
<InputCopyable :value="param.value" readonly />
6487
</div>
6588
</c-card>
6689
</template>

0 commit comments

Comments
 (0)