// lookup methods
let lookups = {
  'cookie': function (req, options) {
    if (req.cookies) {
      return req.cookies[options.cookie.name];
    }
  },
  'domain': function (req, options) {
    if (options.map && options.map.domain && req.host) {
      return options.map.domain[req.host];
    }
  },
  'accept-language': function (req, options) {
    let locale;
    let accepted;

    if (req.acceptsLanguages) {
      accepted = req.acceptsLanguages();
    } else if (req.acceptedLanguages) {
      accepted = req.acceptedLanguages;
    } else {
      return false;
    }

    accepted.some(item => {
      locale = filter(complete(item.replace(/-+/g, '_'), options), options);
      return locale;
    });

    return locale;
  },
  'default': function (req, options) {
    return options['default'];
  }
};

// filter wrong formats (and optionally non-allowed values)
function filter (locale, options) {
  if (locale && locale.length === 5) {
    if (!options.allowed) {
      return locale;
    }
    if (options.allowed.indexOf(locale) >= 0) {
      return locale;
    }
  }
}

// complete languages to locale (if options available)
function complete (locale, options) {
  if (locale && locale.length === 2 && options.map) {
    if (options.map.language) {
      return options.map.language[locale.toLowerCase()];
    }
  }
  return locale;
}

// lookup locale using specified source method
function lookup (source, req, options) {
  if (!lookups[source]) {
    throw Error('Locale lookup source method "' + source + '" not defined');
  }
  return filter(complete(lookups[source](req, options), options), options);
}

function createLocaleMiddleware (config) {
  let options = config || {};

  options['default'] = options['default'] || 'en_GB';
  options.cookie = options.cookie || {name: 'locale'};
  options.priority = options.priority || ['accept-language', 'default'];

  return function (req, res, next) {
    options.priority.some(source => {
      let locale = lookup(source, req, options);

      if (locale) {
        req.locale = {
          code: locale,
          source: source
        };
      }
      return locale;
    });

    next();
  };
}

createLocaleMiddleware.prototype.addLookup = function (name, lookup) {
  lookups[name] = lookup;
};

module.exports = createLocaleMiddleware;

@@ -1440,31 +1440,35 @@
 %0A  if (!
-lookups%5Bsource%5D
+(source in lookups)
 ) %7B%0A    
@@ -1660,101 +1660,72 @@
 re (
-config) %7B%0A  let options = config %7C%7C %7B%7D;%0A%0A  options%5B'default'%5D = options%5B'
+options = %7B%7D) %7B%0A  options = Object.assign(%7B%0A    
 default
-'%5D %7C%7C
+:
  'en_GB'
 ;%0A  
@@ -1724,46 +1724,21 @@
 _GB'
-;%0A  options.cookie = options.
+,%0A    
 cookie
- %7C%7C
+:
  %7Bna
@@ -1754,47 +1754,22 @@
 le'%7D
-;%0A  options.priority = options.
+,%0A    
 priority
  %7C%7C 
@@ -1768,11 +1768,9 @@
 rity
- %7C%7C
+:
  %5B'a
@@ -1796,16 +1796,30 @@
 efault'%5D
+%0A  %7D, options)
 ;%0A%0A  ret
@@ -2045,16 +2045,16 @@
  %7D;%0A
-
       %7D%0A
     
@@ -2045,24 +2045,25 @@
  %7D;%0A      %7D%0A
+%0A
       return

improve defaults