All files / utils buildUrl.js

100% Statements 12/12
100% Branches 9/9
100% Functions 3/3
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                    218x   218x 495x     218x       218x 218x 218x   218x   218x 103x     218x    
 
// File copied from https://github.com/folio-org/stripes-smart-components/blob/master/lib/SearchAndSort/buildUrl.js
import queryString from 'query-string';
import {
  unset,
  isEmpty,
  forOwn,
} from 'lodash';
 
function removeEmpty(obj) {
  const cleanObj = {};
 
  forOwn(obj, (value, key) => {
    if (value) cleanObj[key] = value;
  });
 
  return cleanObj;
}
 
export default function buildUrl(location, values, basePath) {
  const locationQuery = location?.query ?? queryString.parse(location.search);
  let url = values._path || basePath || location.pathname;
  const params = removeEmpty(Object.assign(locationQuery, values));
 
  unset(params, '_path');
 
  if (!isEmpty(params)) {
    url += `?${queryString.stringify(params)}`;
  }
 
  return url;
}