Skip to content

Commit

Permalink
fix: update $ replace with global regexp (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
raiseandfall authored Aug 11, 2024
1 parent eda8d02 commit 919e134
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ export function join(path1: string, path2: string): string {
}

let encode = encodeURIComponent;
let encodeKey = k => encode(k).replace('%24', '$');
const dollarSignRegex: RegExp = /%24/g;
let encodeKey = k => encode(k).replace(dollarSignRegex, '$');
/**
* Recursively builds part of query string for parameter.
*
Expand Down
18 changes: 17 additions & 1 deletion test/path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ describe('query strings', () => {
{ input: { a: '&' }, output: 'a=%26' },
{ input: { '&': 'a' }, output: '%26=a' },
{ input: { a: true }, output: 'a=true' },
{ input: { '$test': true }, output: '$test=true' },

{ input: { obj: { a: 5, b: "str", c: false } }, output: 'obj%5Ba%5D=5&obj%5Bb%5D=str&obj%5Bc%5D=false' },
{ input: { obj: { a: 5, b: "str", c: false } }, traditional: true, output: 'obj=%5Bobject%20Object%5D' },
Expand All @@ -286,6 +285,23 @@ describe('query strings', () => {
});
});

describe('$ encoding', () => {
it('encodes $ in keys', () => {
const path1 = { $test: true };
expect(buildQueryString(path1)).toBe('$test=true');
});

it('encodes multiple $ in keys', () => {
const path1 = { $test$: true };
expect(buildQueryString(path1)).toBe('$test$=true');
});

it('encodes key with only $', () => {
const path1 = { $$$$$$: true };
expect(buildQueryString(path1)).toBe('$$$$$$=true');
});
});

interface IParseTestCase {
input: string;
output: any;
Expand Down

0 comments on commit 919e134

Please sign in to comment.