All files / src/matchers Url.js

100% Statements 5/5
92.31% Branches 12/13
100% Functions 4/4
100% Lines 5/5
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                          3x       2999x           1132x       2001x             1505x                          
/**
 * @copyright   2016, Miles Johnson
 * @license     https://opensource.org/licenses/MIT
 * @flow
 */
 
import React from 'react';
import Matcher from '../Matcher';
import Url from '../components/Url';
import { URL_PATTERN } from '../constants';
 
import type { MatchResponse, UrlProps } from '../types';
 
const URL_REGEX = new RegExp(URL_PATTERN, 'i');
 
export default class UrlMatcher extends Matcher<Object> {
  replaceWith(match: string, props: Object = {}): React.Element<UrlProps> {
    return (
      <Url {...props}>{match}</Url>
    );
  }
 
  asTag(): string {
    return 'a';
  }
 
  match(string: string): ?MatchResponse {
    return this.doMatch(string, URL_REGEX, this.handleMatches);
  }
 
  /**
   * Package the mached response.
   */
  handleMatches(matches: string[]): { [key: string]: string | Object } {
    return {
      urlParts: {
        scheme: matches[1] ? matches[1].replace('://', '') : 'http',
        auth: matches[2] ? matches[2].substr(0, matches[2].length - 1) : '',
        host: matches[3],
        port: matches[4] ? matches[4].substr(1) : '',
        path: matches[5] || '',
        query: matches[6] || '',
        fragment: matches[7] || '',
      },
    };
  }
}