From 919e13412722f6fb05e4d26254651495e41fa8e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthieu=20Coll=C3=A9?= Date: Sun, 11 Aug 2024 08:37:14 -0400 Subject: [PATCH] fix: update $ replace with global regexp (#54) --- src/index.ts | 3 ++- test/path.spec.ts | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index a88e4b6..d18aa4d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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. * diff --git a/test/path.spec.ts b/test/path.spec.ts index 4675438..812e3b5 100644 --- a/test/path.spec.ts +++ b/test/path.spec.ts @@ -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' }, @@ -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;