Skip to content

Commit 63973cd

Browse files
committed
Refactor parseQueryString function to handle empty inputs, decode parameters, and support duplicate keys; add corresponding tests for edge cases
1 parent 655bb31 commit 63973cd

2 files changed

Lines changed: 76 additions & 9 deletions

File tree

‎Sprint-2/implement/querystring.js‎

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,65 @@
11
function parseQueryString(queryString) {
2-
const queryParams = {};
3-
if (queryString.length === 0) {
4-
return queryParams;
2+
const result = {};
3+
4+
// Return empty object for empty or non-string inputs
5+
if (typeof queryString !== "string" || !queryString.trim()) {
6+
return result;
57
}
6-
const keyValuePairs = queryString.split("&");
78

8-
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
9+
// Optional: strip leading '?' if passed a full URL search string (e.g., "?a=1")
10+
const sanitizedInput = queryString.startsWith("?")
11+
? queryString.slice(1)
12+
: queryString;
13+
14+
// Split pairs by '&'
15+
const pairs = sanitizedInput.split("&");
16+
17+
for (const pair of pairs) {
18+
// Ignore empty pairs (e.g. from consecutive '&&' or trailing '&')
19+
if (!pair) continue;
20+
21+
// Find the FIRST '=' to separate key and value correctly
22+
const equalIndex = pair.indexOf("=");
23+
24+
let rawKey = "";
25+
let rawValue = "";
26+
27+
if (equalIndex === -1) {
28+
// Key with no '=' (e.g., "key" -> key="key", value="")
29+
rawKey = pair;
30+
rawValue = "";
31+
} else {
32+
// Split strictly on the first '=' so values containing '=' stay intact
33+
rawKey = pair.slice(0, equalIndex);
34+
rawValue = pair.slice(equalIndex + 1);
35+
}
36+
37+
// Helper to decode query string encoding (+ to space, percent-encoding)
38+
const decodeParam = (str) => {
39+
try {
40+
return decodeURIComponent(str.replace(/\+/g, " "));
41+
} catch {
42+
// Fallback if URI decoding fails on malformed input
43+
return str.replace(/\+/g, " ");
44+
}
45+
};
46+
47+
const key = decodeParam(rawKey);
48+
const value = decodeParam(rawValue);
49+
50+
// Handle single key vs duplicate keys
51+
if (Object.prototype.hasOwnProperty.call(result, key)) {
52+
if (Array.isArray(result[key])) {
53+
result[key].push(value);
54+
} else {
55+
result[key] = [result[key], value];
56+
}
57+
} else {
58+
result[key] = value;
59+
}
1160
}
1261

13-
return queryParams;
62+
return result;
1463
}
1564

1665
module.exports = parseQueryString;

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

Lines changed: 19 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({
@@ -46,3 +46,21 @@ test("should store values of a key in an array when the key has 2 or more values
4646
foo: "bar",
4747
});
4848
});
49+
test("should strip leading '?' if present", () => {
50+
expect(parseQueryString("?foo=bar&baz=qux")).toEqual({
51+
foo: "bar",
52+
baz: "qux",
53+
});
54+
});
55+
56+
test("should handle empty or invalid inputs gracefully", () => {
57+
expect(parseQueryString("")).toEqual({});
58+
expect(parseQueryString(null)).toEqual({});
59+
expect(parseQueryString(undefined)).toEqual({});
60+
});
61+
62+
test("should gracefully handle malformed percent encoding", () => {
63+
expect(parseQueryString("key=%E0%A4")).toEqual({
64+
key: "%E0%A4",
65+
});
66+
});

0 commit comments

Comments
 (0)