Skip to content

Commit 3543e12

Browse files
committed
fix: make package consistent with native
1 parent 1867c12 commit 3543e12

3 files changed

Lines changed: 15 additions & 17 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ npm i --save fast-querystring
1212

1313
- Parsed object does not have prototype methods
1414
- Uses `&` separator as default
15-
- Removes fragment from string
16-
- `foo=bar#hello=world` parses into `{foo: 'bar'}`
1715
- Supports only UTF-8
1816
- Supports only input of type `string`
1917
- Key & value with length 0 is omitted by default

lib/parse.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,25 @@ function parse(input) {
1717
let separatorIndex = 0;
1818
let equalityIndex = 0;
1919
let shouldEncode = false;
20+
let hasPlus = false;
2021

2122
// Have a boundary of input.length + 1 to access last pair inside the loop.
2223
for (let i = 0; i < input.length + 1; i++) {
2324
let c = input.charCodeAt(i);
2425

25-
// Handle '&', '#' and end of line to pass the current values to result
26+
// Handle '&' and end of line to pass the current values to result
2627
if (c === 38 || c === 35 || isNaN(c)) {
2728
// Disallow empty key values.
2829
if (equalityIndex - separatorIndex > 0 && i - equalityIndex + 1 > 0) {
2930
key = input.slice(separatorIndex, equalityIndex);
3031
value = input.slice(equalityIndex + 1, i);
3132

33+
// Optimization: Replace '+' with space
34+
if (hasPlus) {
35+
key = key.replace(/\+/g, " ");
36+
value = value.replace(/\+/g, " ");
37+
}
38+
3239
// Optimization: Do not decode if it's not necessary.
3340
if (shouldEncode) {
3441
key = decodeURIComponent(key);
@@ -48,11 +55,6 @@ function parse(input) {
4855
}
4956
}
5057

51-
// Terminate loop when parser encounters '#'
52-
if (c === 35) {
53-
break;
54-
}
55-
5658
// Reset reading key value pairs
5759
separatorIndex = i + 1;
5860
equalityIndex = i + 1;
@@ -64,12 +66,7 @@ function parse(input) {
6466
}
6567
// Check '+', and replace it with empty space.
6668
else if (c === 43) {
67-
input[i] = " ";
68-
}
69-
// Check '?' in the beginning, and omit if exists
70-
else if (c === 63 && i === 0) {
71-
separatorIndex = 1;
72-
equalityIndex = 1;
69+
hasPlus = true;
7370
} else {
7471
// Check '%' character for encoding
7572
if (c === 37) {

test/basics.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ test("handles & on first/last character", () => {
3636
});
3737

3838
test("handles ? on first character", () => {
39-
assert.deepEqual(qs.parse("?hello=world"), { hello: "world" });
39+
// This aligns with `node:querystring` functionality
40+
assert.deepEqual(qs.parse("?hello=world"), { "?hello": "world" });
4041
});
4142

42-
test("removes fragment from string", () => {
43-
assert.deepEqual(qs.parse("foo=bar#hello"), { foo: "bar" });
43+
test("handles + character", () => {
44+
assert.deepEqual(qs.parse("author=Yagiz+Nizipli"), {
45+
author: "Yagiz Nizipli",
46+
});
4447
});

0 commit comments

Comments
 (0)