Skip to content

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

Open
@AshCoolman

Description

@AshCoolman

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions