Skip to content

Commit

Permalink
feat: wildcard support
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeIbberson committed Jan 31, 2020
1 parent bd6ec14 commit 0c10c55
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/__tests__/location.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Adapter from 'enzyme-adapter-react-16';
import { withLocation, LocationProvider } from '..';

let Hoc;
const Mock = () => <div />
const Mock = () => <div />;

const hookDecorators = expect.objectContaining({
getFrom: expect.any(Function),
Expand All @@ -27,9 +27,11 @@ describe('withLocation', () => {
});

it('should return a high-order function', () => {
expect(mount(<Hoc />).find(Mock).props()).toMatchObject(
hookDecorators,
);
expect(
mount(<Hoc />)
.find(Mock)
.props(),
).toMatchObject(hookDecorators);
});
});

Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/parameters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ describe('Parameters', () => {
month: 'April',
},
},
testAll: '*',
testNone: '!*',
});

expect(remove).toHaveBeenCalledWith('friends');
Expand All @@ -80,6 +82,8 @@ describe('Parameters', () => {
'April',
);

expect(set).toHaveBeenCalledWith('testAll', '');
expect(set).toHaveBeenCalledWith('!testNone', '');
expect(set).toHaveBeenCalledWith('isFriendly', false);
expect(set).toHaveBeenCalledWith('name', name);

Expand Down
11 changes: 8 additions & 3 deletions src/parameters.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { navigate } from '@reach/router';
import flat from 'flat';
import { isEmpty, serializeArray } from './utils';
import {
isEmpty,
serializeArray,
asEmpty,
asInverted,
} from './utils';

export default class extends URLSearchParams {
populate(a = {}) {
Expand All @@ -18,7 +23,7 @@ export default class extends URLSearchParams {
serializeAndAssign(key, v) {
const arr = serializeArray(v);
if (arr.length) {
this.set(key, serializeArray(arr));
this.set(asInverted(key, arr), asEmpty(arr));
} else {
this.delete(key);
}
Expand All @@ -40,7 +45,7 @@ export default class extends URLSearchParams {
},
);
} else {
this.set(key, v);
this.set(asInverted(key, v), asEmpty(v));
}
});
}
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ export const isEmpty = (v) =>

export const serializeArray = (v) =>
Array.isArray(v) ? v.join(',') : v;

export const asEmpty = (v) =>
v === '*' || v === '!*' ? '' : v;

export const asInverted = (key, v) =>
v === '!*' ? `!${key}` : key;

0 comments on commit 0c10c55

Please sign in to comment.