All files / src ParseEndpoint.js

100% Statements 9/9
100% Branches 4/4
100% Functions 2/2
100% Lines 9/9
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                6x 25x 25x   25x 9x     16x 19x 19x     16x        
/**
 * ParseEndpoint handles templating out react-router formatted url's with a
 * map of keys => values.
 *
 * @param endpoint {string} the react formatted endpoint.
 * @param options {object} The key: value pairs that will be templated into the
 *        endpoint.
 */
const ParseEndpoint = (endpoint, options) => {
  const params = endpoint.match(/:[a-z0-9]+/g);
  let parsedEndpoint = endpoint;
 
  if (!params) {
    return endpoint;
  }
 
  params.forEach(param => {
    const cleanParam = param.replace(":", "");
    parsedEndpoint = parsedEndpoint.replace(param, options[cleanParam] || "");
  });
 
  return parsedEndpoint;
};
 
export default ParseEndpoint;