@@ -35,12 +35,10 @@ const formatQueryString = (str) => {
3535
3636 // iterate through key value pairs, split on = , and save in res object as key value
3737 for ( const pair of query ) {
38- let [ key , value ] = pair . split ( "=" ) ;
38+ let [ key , value = 'true' ] = pair . split ( "=" ) ; // checking value is truthy or undefined else befault 'true' value (3rd case)
3939
4040 // check if key truthy else send empty object (2nd case)
4141 if ( key ) {
42- value = value || "true" ; // checking value is truthy or undefined else befault 'true' value (3rd case)
43-
4442 // if there is value with the same key, have to save values as array item (4th case)
4543 resObj [ key ] = resObj [ key ]
4644 ? typeof resObj [ key ] === "string"
@@ -59,18 +57,18 @@ const formatQueryString = (str) => {
5957 return resObj ;
6058} ;
6159
62- // console.log("1st case: ", formatQueryString("?foo=hello&bar=world"), {
63- // foo: 'hello',
64- // bar: 'world'
65- // });
60+ console . log ( "1st case: " , formatQueryString ( "?foo=hello&bar=world" ) , {
61+ foo : 'hello' ,
62+ bar : 'world'
63+ } ) ;
6664// console.log("2nd case: ", formatQueryString("?"), {}); // if there is no query after ?
6765// console.log("3rd case: ", formatQueryString("?foo=hello&bar=world&baz"), {
6866// foo: "hello",
6967// bar: "world",
7068// baz: "true",
7169// });
7270
73- /* console.log(
71+ console . log (
7472 "4th case: " ,
7573 formatQueryString ( "?foo=hello&bar=world&baz&foo=again&foo=ok&bar=again&baz&dummy&data=earth" ) ,
7674 {
@@ -80,9 +78,9 @@ const formatQueryString = (str) => {
8078 dummy : 'true' ,
8179 data : 'earth'
8280 }
83- ); */
81+ ) ;
8482
8583// test case
86- console . log ( "4th case: " , formatQueryString ( "?foo=hello&foo" ) , {
87- foo : [ "hello" , "true" ] ,
88- } ) ;
84+ // console.log("4th case: ", formatQueryString("?foo=hello&foo"), {
85+ // foo: ["hello", "true"],
86+ // });
0 commit comments