Skip to content

Commit

Permalink
refactor: remove qs-stringify dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign committed Dec 1, 2022
1 parent 31cda7f commit 91d8555
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 30 deletions.
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
},
"author": "Andrea Carraro <me@andreacarraro.it>",
"license": "ISC",
"dependencies": {
"qs-stringify": "^1.2.1"
},
"devDependencies": {
"@testing-library/react": "^13.4.0",
"@types/jest": "^29.2.3",
Expand Down
15 changes: 7 additions & 8 deletions src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import { RouterContext } from 'next/dist/shared/lib/router-context';
import type { NextRouter } from 'next/router';
import { renderHook as TLRenderHook } from '@testing-library/react';
import stringify from 'qs-stringify';
import { useContextualRouting, RETURN_HREF_QUERY_PARAM } from '../index';

type PartialRouterMock = Partial<NextRouter>;
Expand Down Expand Up @@ -64,12 +63,12 @@ describe('useContextualRouting', () => {
expect(result.current.returnHref).toBe('/startpage/55?page=2#anchor');
expect(result.current.makeContextualHref({ extraParam: 'foo' })).toBe(
'/startpage/[id]?' +
stringify({
new URLSearchParams({
id: '55',
page: '2',
extraParam: 'foo',
[RETURN_HREF_QUERY_PARAM]: '/startpage/55?page=2#anchor',
})
}).toString()
);
});

Expand All @@ -83,9 +82,9 @@ describe('useContextualRouting', () => {
expect(result.current.returnHref).toBe('/startpage');
expect(result.current.makeContextualHref()).toBe(
'/startpage/[id]?' +
stringify({
new URLSearchParams({
[RETURN_HREF_QUERY_PARAM]: '/startpage',
})
}).toString()
);
});

Expand All @@ -105,7 +104,7 @@ describe('useContextualRouting', () => {
expect(result.current.returnHref).toBe('/startpage/55?page=2#anchor');
expect(result.current.makeContextualHref()).toBe(
'/startpage/[id]?' +
stringify({
new URLSearchParams({
id: '55',
page: '2',
[RETURN_HREF_QUERY_PARAM]: '/startpage/55?page=2#anchor',
Expand All @@ -132,11 +131,11 @@ describe('useContextualRouting', () => {
expect(result.current.returnHref).toBe('/startpage/55?page=3#anchor');
expect(result.current.makeContextualHref()).toBe(
'/startpage/[id]?' +
stringify({
new URLSearchParams({
id: '55',
page: '3',
[RETURN_HREF_QUERY_PARAM]: '/startpage/55?page=3#anchor',
})
}).toString()
);
});
});
Expand Down
11 changes: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useCallback } from 'react';
import { useRouter } from 'next/router';
import stringify from 'qs-stringify';
export const RETURN_HREF_QUERY_PARAM = '_UCR_return_href';

/**
Expand Down Expand Up @@ -36,11 +35,11 @@ export function useContextualRouting(): {
(extraParams?: Record<string, string | number>) =>
router.pathname +
'?' +
stringify(
Object.assign({}, router.query, extraParams, {
[RETURN_HREF_QUERY_PARAM]: returnHref,
})
),
new URLSearchParams({
...router.query,
...extraParams,
[RETURN_HREF_QUERY_PARAM]: returnHref,
}).toString(),
[queryHash, returnHref]
);

Expand Down

0 comments on commit 91d8555

Please sign in to comment.