Skip to content

Commit

Permalink
fix: iterator fn
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeIbberson committed Feb 7, 2020
1 parent 62852fb commit 6213ba6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/__tests__/useLocation.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,15 @@ describe('useLocation', () => {
describe('"getAll"', () => {
it('should reduce forEach method results', () => {
jest
.spyOn(Parameters.prototype, 'forEach')
.mockImplementation((v) => {
v('foo', 'bar');
.spyOn(Parameters.prototype, 'entries')
.mockImplementation(() => {
const iterable = {
*[Symbol.iterator]() {
yield ['foo', 'bar'];
},
};

return iterable;
});

const output = useLocation().getAll();
Expand Down
16 changes: 10 additions & 6 deletions src/useLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ const isString = (v) => typeof v === 'string' && v.length;

export default (search, navigate) => {
const params = new Parameters(search);
const redirect = () => navigate(params.redirectStr());

return {
params,
redirect,

getFrom(key) {
if (isObject(key)) return params.populate(key);
Expand All @@ -21,9 +23,11 @@ export default (search, navigate) => {
const output = {};

try {
params.forEach((value, key) => {
output[value] = key;
});
// eslint-disable-next-line
for (const pair of params.entries()) {
// eslint-disable-next-line
output[pair[0]] = pair[1];
}
} catch (e) {
// noop
}
Expand All @@ -33,15 +37,15 @@ export default (search, navigate) => {

pushTo(values) {
params.merge(values);
navigate(params.redirectStr());
redirect();
window.scrollTo(0, 0);
},

clearByName(next) {
return (e) => {
e.stopPropagation();
params.delete(e.currentTarget.name);
navigate(params.redirectStr());
redirect();
next();
};
},
Expand All @@ -57,7 +61,7 @@ export default (search, navigate) => {
}

params.delete('page');
navigate(params.redirectStr());
redirect();
next();
};
},
Expand Down

0 comments on commit 6213ba6

Please sign in to comment.