jmurzy/react-router-native

Please sanity test my query modification functions [Suggestion?]

AshCoolman opened this issue · 0 comments

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