All files / platform/core/src/utils urlUtil.js

30.55% Statements 11/36
0% Branches 0/9
0% Functions 0/8
30.55% Lines 11/36

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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76    34x 34x         34x                                 34x                 34x             34x             34x                 34x         34x       34x           34x      
import lib from 'query-string';
 
const PARAM_SEPARATOR = ';';
const PARAM_PATTERN_IDENTIFIER = ':';
 
function toLowerCaseFirstLetter(word) {
  return word[0].toLowerCase() + word.slice(1);
}
const getQueryFilters = (location = {}) => {
  const { search } = location;
 
  Iif (!search) {
    return;
  }
 
  const searchParameters = parse(search);
  const filters = {};
 
  Object.entries(searchParameters).forEach(([key, value]) => {
    filters[toLowerCaseFirstLetter(key)] = value;
  });
 
  return filters;
};
 
const decode = (strToDecode = '') => {
  try {
    const decoded = window.atob(strToDecode);
    return decoded;
  } catch (e) {
    return strToDecode;
  }
};
 
const parse = toParse => {
  Iif (toParse) {
    return lib.parse(toParse);
  }
 
  return {};
};
const parseParam = paramStr => {
  const _paramDecoded = decode(paramStr);
  Iif (_paramDecoded && typeof _paramDecoded === 'string') {
    return _paramDecoded.split(PARAM_SEPARATOR);
  }
};
 
const replaceParam = (path = '', paramKey, paramValue) => {
  const paramPattern = `${PARAM_PATTERN_IDENTIFIER}${paramKey}`;
  Iif (paramValue) {
    return path.replace(paramPattern, paramValue);
  }
 
  return path;
};
 
const isValidPath = path => {
  const paramPatternPiece = `/${PARAM_PATTERN_IDENTIFIER}`;
  return path.indexOf(paramPatternPiece) < 0;
};
 
const queryString = {
  getQueryFilters,
};
 
const paramString = {
  isValidPath,
  parseParam,
  replaceParam,
};
 
const urlUtil = { parse, queryString, paramString };
 
export default urlUtil;