import _ from 'lodash';
import pathToRegexp from 'path-to-regexp';

export default function matchFromHistory(pattern, history, options = {}) {
  if (!pattern) {
    return {
      url: history.location.pathname,
      isExact: false,
      params: {},
    };
  }

  const keys = [];
  const re = pathToRegexp(pattern, keys, {
    end: options.exact || false,
  });

  const matched = re.exec(history.location.pathname);

  if (!matched) {
    return null;
  }

  const url = matched[0];
  const restValues = _.tail(matched);
  const keyNames = keys.map(k => k.name);
  const isExact = (url === history.location.pathname);

  if (options.exact && !isExact) {
    return null;
  }

  const params = _.zipObject(keyNames, restValues);

  return {
    url,
    isExact,
    params,
  };
}

@@ -421,24 +421,38 @@
 %0A  if (!
+Array.isArray(
 matched)
  %7B%0A    r
@@ -443,16 +443,17 @@
 matched)
+)
  %7B%0A    r

check result of RegExp.exec using Array.isArray (#245)