Skip to content

Commit d6d7b68

Browse files
committed
d edge case tests and update parseQueryString
1 parent 92d720c commit d6d7b68

2 files changed

Lines changed: 53 additions & 34 deletions

File tree

‎Sprint-2/implement/querystring.js‎

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,36 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3-
if (queryString.length === 0) {
4-
return queryParams;
3+
if (typeof queryString !== "string") {
4+
throw new Error("Invalid input");
55
}
6-
7-
const keyValuePairs = queryString.split("&");
8-
9-
for (const pair of keyValuePairs) {
10-
if (pair === "") {
11-
continue;
12-
}
13-
const pairWithoutAdd = pair.replace(/\+/g, " ");
14-
const indexOfEqual = pairWithoutAdd.indexOf("=");
15-
16-
let keyVal;
17-
let val;
18-
19-
if (indexOfEqual === -1) {
20-
keyVal = pairWithoutAdd;
21-
val = "";
6+
if (queryString.length === 0) return queryParams;
7+
8+
const parseQueryStringArray = queryString.replace(/\+/g, " ").split("&");
9+
let key;
10+
let value;
11+
for (const pair of parseQueryStringArray) {
12+
if (pair === "") continue;
13+
let equalIndex = pair.indexOf("=");
14+
if (equalIndex === -1) {
15+
key = pair;
16+
value = "";
2217
} else {
23-
keyVal = pairWithoutAdd.slice(0, indexOfEqual);
24-
val = pairWithoutAdd.slice(indexOfEqual + 1);
18+
key = decodeURIComponent(pair.slice(0, equalIndex));
19+
value = decodeURIComponent(pair.slice(equalIndex + 1));
2520
}
26-
keyVal = decodeURIComponent(keyVal);
27-
val = decodeURIComponent(val);
28-
29-
if (Object.hasOwn(queryParams, keyVal)) {
30-
if (!Array.isArray(queryParams[keyVal])) {
31-
queryParams[keyVal] = [queryParams[keyVal]];
21+
if (Object.hasOwn(queryParams, key)) {
22+
if (Array.isArray(queryParams[key])) {
23+
queryParams[key].push(value);
24+
} else {
25+
queryParams[key] = [queryParams[key]];
26+
queryParams[key].push(value);
3227
}
33-
queryParams[keyVal].push(val);
3428
} else {
35-
queryParams[keyVal] = val;
29+
queryParams[key] = value;
3630
}
3731
}
3832

3933
return queryParams;
4034
}
4135

4236
module.exports = parseQueryString;
43-
44-
/*
45-
let a = { color: "red" };
46-
a.color = [a.color];
47-
console.log(a);*/

‎Sprint-2/implement/querystring.test.js‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Below are some test cases the implementation doesn't handle well.
44
// Fix the implementation for these tests, and try to think of as many other edge cases as possible - write tests and fix those too.
55

6-
const parseQueryString = require("./querystring.js")
6+
const parseQueryString = require("./querystring.js");
77

88
test("should parse values containing '='", () => {
99
expect(parseQueryString("equation=a=b-2")).toEqual({
@@ -25,6 +25,36 @@ test("should accept empty string as key or as value", () => {
2525
expect(parseQueryString("=")).toEqual({ "": "" });
2626
});
2727

28+
test("should return empty object for empty input", () => {
29+
expect(parseQueryString("")).toEqual({});
30+
});
31+
32+
test("throws an error for invalid input", () => {
33+
expect(() => parseQueryString(null)).toThrow("Invalid input");
34+
expect(() => parseQueryString({})).toThrow("Invalid input");
35+
expect(() => parseQueryString([])).toThrow("Invalid input");
36+
expect(() => parseQueryString(undefined)).toThrow("Invalid input");
37+
expect(() => parseQueryString(123)).toThrow("Invalid input");
38+
expect(() => parseQueryString(true)).toThrow("Invalid input");
39+
});
40+
41+
test("should work on duplicate empty keys", () => {
42+
expect(parseQueryString("=one&=two")).toEqual({
43+
"": ["one", "two"],
44+
});
45+
});
46+
test("should work on duplicate empty values", () => {
47+
expect(parseQueryString("key=&key=")).toEqual({
48+
key: ["", ""],
49+
});
50+
});
51+
52+
test("should work on & inside values", () => {
53+
expect(parseQueryString("text=Tom%26Jerry")).toEqual({
54+
text: "Tom&Jerry",
55+
});
56+
});
57+
2858
test("should decode percent-encoded characters", () => {
2959
expect(parseQueryString("%24half=1%2F2")).toEqual({
3060
$half: "1/2",

0 commit comments

Comments
 (0)