serialize-query-params
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -422,2 +422,14 @@ "use strict"; | ||
var JSON_SAFE_CHARS = `{}[],":`.split("").map((d) => [d, encodeURIComponent(d)]); | ||
function getHrefFromLocation(location, search) { | ||
let href = search; | ||
if (location.href) { | ||
try { | ||
const url = new URL(location.href); | ||
href = `${url.origin}${url.pathname}${search}`; | ||
} catch (e) { | ||
href = ""; | ||
} | ||
} | ||
return href; | ||
} | ||
function transformSearchStringJsonSafe(searchString) { | ||
@@ -433,13 +445,6 @@ let str = searchString; | ||
const search = encodedSearchString.length ? `?${encodedSearchString}` : ""; | ||
let href; | ||
if (location.href) { | ||
const url = new URL(location.href); | ||
href = `${url.origin}${url.pathname}${search}`; | ||
} else { | ||
href = search; | ||
} | ||
const newLocation = { | ||
...location, | ||
key: `${Date.now()}`, | ||
href, | ||
href: getHrefFromLocation(location, search), | ||
search, | ||
@@ -446,0 +451,0 @@ query: encodedQuery |
{ | ||
"name": "serialize-query-params", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "A library for simplifying encoding and decoding URL query parameters.", | ||
@@ -47,3 +47,3 @@ "main": "./index.cjs.js", | ||
}, | ||
"gitHead": "fc548a28a7b94fb6eb4a1cff45f57977b65dc0c4" | ||
"gitHead": "cb5010bb5709a3ae3a982ffaeb4ca42d52536475" | ||
} |
import { objectToSearchString } from "./objectToSearchString"; | ||
import { searchStringToObject } from "."; | ||
const JSON_SAFE_CHARS = `{}[],":`.split("").map((d) => [d, encodeURIComponent(d)]); | ||
function getHrefFromLocation(location, search) { | ||
let href = search; | ||
if (location.href) { | ||
try { | ||
const url = new URL(location.href); | ||
href = `${url.origin}${url.pathname}${search}`; | ||
} catch (e) { | ||
href = ""; | ||
} | ||
} | ||
return href; | ||
} | ||
function transformSearchStringJsonSafe(searchString) { | ||
@@ -14,13 +26,6 @@ let str = searchString; | ||
const search = encodedSearchString.length ? `?${encodedSearchString}` : ""; | ||
let href; | ||
if (location.href) { | ||
const url = new URL(location.href); | ||
href = `${url.origin}${url.pathname}${search}`; | ||
} else { | ||
href = search; | ||
} | ||
const newLocation = { | ||
...location, | ||
key: `${Date.now()}`, | ||
href, | ||
href: getHrefFromLocation(location, search), | ||
search, | ||
@@ -27,0 +32,0 @@ query: encodedQuery |
{ | ||
"name": "serialize-query-params", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "A library for simplifying encoding and decoding URL query parameters.", | ||
@@ -47,3 +47,3 @@ "main": "./dist/index.cjs.js", | ||
}, | ||
"gitHead": "fc548a28a7b94fb6eb4a1cff45f57977b65dc0c4" | ||
"gitHead": "cb5010bb5709a3ae3a982ffaeb4ca42d52536475" | ||
} |
import { stringify } from 'query-string'; | ||
import { EncodedQuery } from '..'; | ||
export function makeMockLocation(query: EncodedQuery): Location { | ||
export function makeMockLocation(query: EncodedQuery, includeHref: boolean = true): Location { | ||
const queryStr = stringify(query); | ||
@@ -12,5 +12,5 @@ const search = queryStr.length ? `?${queryStr}` : ''; | ||
search, | ||
href: 'http://localhost:3000/' + search, | ||
href: includeHref ? 'http://localhost:3000/' + search : undefined, | ||
key: `mock-${Date.now()}`, | ||
} as Location & { key: string }; | ||
} |
@@ -80,2 +80,22 @@ import { describe, it } from 'vitest'; | ||
}); | ||
it('call it twice, should not throw exception', () => { | ||
const location = makeMockLocation({ | ||
foo: 'one two', | ||
bar: '[({1,2:3:4,5})]', | ||
}, false); | ||
const newLocationAsReturned = updateLocation( | ||
{ foo: 'o t h', bar: '[({1,2:3:6,5})]' }, | ||
location, | ||
(params) => stringify(params, { encode: false }) | ||
); | ||
const newLocation2 = updateLocation( | ||
{ foo: 'o t h', bar: '[({1,2:6:4,5})]' }, | ||
newLocationAsReturned, | ||
(params) => { | ||
const str = stringify(params, { encode: false }); | ||
return transformSearchStringJsonSafe(str).replace(/ /g, '%20'); | ||
} | ||
); | ||
expect(newLocation2.search).toBe('?bar=[({1,2:6:4,5})]&foo=o%20t%20h'); | ||
}); | ||
}); | ||
@@ -82,0 +102,0 @@ |
@@ -13,2 +13,20 @@ import { EncodedQuery } from './types'; | ||
function getHrefFromLocation(location: Location, search: string): string { | ||
// https://developer.mozilla.org/en-US/docs/Web/API/URL/URL | ||
let href: string = search; | ||
if (location.href) { | ||
// TODO - implement base option if location.href is relative | ||
// see https://developer.mozilla.org/en-US/docs/Web/API/URL/URL#syntax | ||
try { | ||
const url = new URL(location.href); | ||
href = `${url.origin}${url.pathname}${search}`; | ||
} catch (e) { | ||
href = ''; | ||
} | ||
} | ||
return href; | ||
} | ||
export function transformSearchStringJsonSafe(searchString: string): string { | ||
@@ -34,9 +52,2 @@ let str = searchString; | ||
const search = encodedSearchString.length ? `?${encodedSearchString}` : ''; | ||
let href: string; | ||
if (location.href) { | ||
const url = new URL(location.href); | ||
href = `${url.origin}${url.pathname}${search}`; | ||
} else { | ||
href = search; | ||
} | ||
@@ -49,3 +60,3 @@ const newLocation: Location & { | ||
key: `${Date.now()}`, // needed for some routers (e.g. react-router) | ||
href, | ||
href: getHrefFromLocation(location, search), | ||
search, | ||
@@ -52,0 +63,0 @@ query: encodedQuery, // needed for some routers (e.g. found) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
212833
3610