All files index.ts

100% Statements 49/49
100% Branches 4/4
100% Functions 2/2
100% Lines 49/49

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 501x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import * as url from "url";
import * as querystring from "querystring";
 
export const parseUrl = (inputUrl: string) => {
  const parsedUrl = new url.URL(inputUrl);
 
  const {
    host,
    protocol,
    username,
    password,
    pathname: path,
    port,
    search,
  } = parsedUrl;
 
  const output = {
    host,
    path,
    port,
    protocol,
    username,
    password,
    query: querystring.parse(search.substring(1)),
  };
 
  return cleanEmptyProps(output);
};
 
const cleanEmptyProps = (obj: ParseUrl) => {
  const keys = Object.keys(obj);
 
  keys.forEach((key) => {
    if (obj[key as keyof typeof obj] === "") {
      delete obj[key as keyof typeof obj];
    }
  });
 
  return obj;
};
 
export type ParseUrl = {
  host: string;
  path: string;
  port: string;
  protocol: string;
  username: string;
  password: string;
};