Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please sanity test my query modification functions [Suggestion?] #76

Open
AshCoolman opened this issue Apr 26, 2017 · 0 comments
Open

Comments

@AshCoolman
Copy link

Hi,

I threw together some quick & dirty helper functions for modifying the "query string".

I'm not very familiar with this project, and I was wondering:

Question: Is the following approach is (broadly) aligned with the intended usage of this project?

import qs from 'query-string';

/**
 * Warning: I don't know if anything relies on query-string format,
 * but I will keep for convention
 */

export const removeQuery = (prevLocation = {}, history, ...queryKeys) => {
  const location = Object.assign({}, prevLocation);
  const searchObj = qs.parse(location.search);
  queryKeys.forEach(q => delete searchObj[q]);
  location.search = qs.stringify(searchObj);
  history.push(location);
};

export const addQuery = (prevLocation = {}, history, queryObj) => {
  const location = Object.assign({}, prevLocation);
  const searchObj = Object.assign(
    qs.parse(location.search),
    queryObj
  );
  location.search = qs.stringify(searchObj);
  history.push(location);
};

export const getQuery = (prevLocation = {}, queryKey) => {
  const searchObj = qs.parse(prevLocation.search);
  return searchObj[queryKey];
};

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant