forked from lukemorales/next-safe-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose
createNavigationConfig
function
- Loading branch information
1 parent
ce7d81a
commit ab8e2ab
Showing
12 changed files
with
1,485 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { convertObjectToURLSearchParams } from './convert-object-to-url-search-params'; | ||
|
||
describe('convertObjectToURLSearchParams', () => { | ||
it('transforms an object into URLSearchParams', () => { | ||
{ | ||
// empty object | ||
const result = convertObjectToURLSearchParams({}); | ||
|
||
expect(result.toString()).toBe(''); | ||
} | ||
|
||
const result = convertObjectToURLSearchParams({ | ||
query: 'john doe', | ||
filters: ['active', 'is_admin'], | ||
}); | ||
|
||
expect(result.toString()).toBe( | ||
'query=john+doe&filters=active&filters=is_admin', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export function convertObjectToURLSearchParams( | ||
object: Record<string, string | string[]>, | ||
): URLSearchParams { | ||
const urlSearchParams = new URLSearchParams(); | ||
|
||
for (const [key, value] of Object.entries(object)) { | ||
if (Array.isArray(value)) { | ||
for (const entry of value) { | ||
urlSearchParams.append(key, `${entry}`); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
urlSearchParams.append(key, `${value}`); | ||
} | ||
|
||
return urlSearchParams; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { ReadonlyURLSearchParams } from 'next/navigation'; | ||
import { convertURLSearchParamsToObject } from './convert-url-search-params-to-object'; | ||
|
||
describe('convertURLSearchParamsToObject', () => { | ||
it('returns an empty object if no URLSearchParams are passed', () => { | ||
const result = convertURLSearchParamsToObject(null); | ||
|
||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('transforms URLSearchParams into an object', () => { | ||
{ | ||
// empty URLSearchParams | ||
const result = convertURLSearchParamsToObject( | ||
new URLSearchParams('') as ReadonlyURLSearchParams, | ||
); | ||
|
||
expect(result).toEqual({}); | ||
} | ||
|
||
const result = convertURLSearchParamsToObject( | ||
new URLSearchParams( | ||
'query=john+doe&filters=active&filters=is_admin', | ||
) as ReadonlyURLSearchParams, | ||
); | ||
|
||
expect(result).toEqual({ | ||
query: 'john doe', | ||
filters: ['active', 'is_admin'], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ReadonlyURLSearchParams } from 'next/navigation'; | ||
|
||
export function convertURLSearchParamsToObject( | ||
params: ReadonlyURLSearchParams | null, | ||
): Record<string, string | string[]> { | ||
if (!params) { | ||
return {}; | ||
} | ||
|
||
return [...params.entries()].reduce<Record<string, string | string[]>>( | ||
(acc, [key, value]) => { | ||
const values = params.getAll(key); | ||
|
||
acc[key] = values.length > 1 ? values : value; | ||
return acc; | ||
}, | ||
{}, | ||
); | ||
} |
Oops, something went wrong.