@@ -3,9 +3,32 @@ import InputCopyable from '../../components/InputCopyable.vue';
3
3
import { isNotThrowing } from ' @/utils/boolean' ;
4
4
import { withDefaultOnError } from ' @/utils/defaults' ;
5
5
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' );
7
7
8
8
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
+ });
9
32
const urlValidationRules = [
10
33
{
11
34
validator : (value : string ) => isNotThrowing (() => new URL (value )),
@@ -49,8 +72,8 @@ const properties: { title: string; key: keyof URL }[] = [
49
72
/>
50
73
51
74
<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 "
54
77
mb-2
55
78
w-full
56
79
flex
@@ -59,8 +82,8 @@ const properties: { title: string; key: keyof URL }[] = [
59
82
<icon-mdi-arrow-right-bottom />
60
83
</div >
61
84
62
- <InputCopyable :value =" k " readonly />
63
- <InputCopyable :value =" v " readonly />
85
+ <InputCopyable :value =" param.key " readonly />
86
+ <InputCopyable :value =" param.value " readonly />
64
87
</div >
65
88
</c-card >
66
89
</template >
0 commit comments