Skip to content

Commit

Permalink
feat: ✨ adds support for equal sign in keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando G. Vilar committed Apr 28, 2020
1 parent 68507f3 commit 0959875
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ export function encode(query: Query): string {
for (const k in query) {
const values = query[k];
if (values) {
if (values === true) parts.push(k);
if (values === true) parts.push(k.replace(/=/g, "%3D"));
else if (values.length) {
const safeKey = k.replace(/=/g, "%3D");
let qsValue = "";
for (let i = 0; i < values.length; i++)
qsValue += (i ? "&" : "") + k + "=" + values[i].replace(/\&/g, "%26");
qsValue = `${qsValue}${i ? "&" : ""}${safeKey}=${values[i].replace(
/&/g,
"%26",
)}`;
parts.push(qsValue);
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ describe("exported `decode`", () => {
limit: ["20"],
},
],
[
"?a%3Da=a&%3D=a=a&a%3D==&a%3D=",
{ "a=a": ["a"], "=": ["a=a"], "a=": ["=", ""] },
],
[
"?string=question&boolean&string=&string=42",
{ boolean: true, string: ["question", "", "42"] },
Expand Down Expand Up @@ -111,6 +115,10 @@ describe("exported `encode`", () => {
limit: ["20"],
},
],
[
"?%3D=a=a&a%3D==&a%3D=&a%3Da=a",
{ "a=a": ["a"], "=": ["a=a"], "a=": ["=", ""] },
],
[
"?boolean&string=question&string=&string=42",
{ boolean: true, string: ["question", "", "42"] },
Expand Down

0 comments on commit 0959875

Please sign in to comment.