src/index.js
/**
 * Lookup produces a single language tag (RFC4647)
 * @param {String} range A range string to languagePriorityList.
 * @param {Array} languageTags A list of bcp47 locales that are supported
 * @param {String} defaultValue The default to return if no match can be made
 * @return {String} returns an array of objects or wildcard if input is not valid (RFC2616)
 */
Lookup produces a single language tag (RFC4647)
@param {String} range A range string to languagePriorityList.
@param {Array} languageTags A list of bcp47 locales that are supported
@param {String} defaultValue The default to return if no match can be made
@return {String} returns an array of objects or wildcard if input is not valid (RFC2616)
Lookup produces a single language tag ( RFC4647 ) @param { String } range A range string to languagePriorityList . @param { Array } languageTags A list of bcp47 locales that are supported @param { String } defaultValue The default to return if no match can be made @return { String } returns an array of objects or wildcard if input is not valid ( RFC2616 )
function lookup(range, languageTags, defaultValue) {
  if (arguments.length !== 3) throw new Error('range:String, languageTags:Array, defaultValue:String required!');
  if (!Array.isArray(languageTags) || languageTags.length === 0) return defaultValue;
  const sortedLangTags = languageTags.sort(lengthSort);
  function matchTag(rangeTags, langTags, index, array) {
    if (index >= langTags.length || index >= rangeTags.length) return array;
    if (rangeTags[index].length !== 1) {
      if (!tagEquality(rangeTags[index], langTags[index])) return array;
      array.push(rangeTags[index]);
    }
    return matchTag(rangeTags, langTags, ++index, array);
  }
  function directTagMatch(tag, index, array) {
    if (!languageTags[index]) return array;
    if (tagEquality(tag, languageTags[index])) {
      if (array.indexOf(languageTags[index]) === -1) array.push(languageTags[index]);
    }
    return directTagMatch(tag, ++index, array);
  }
  function languagesTagsComparator(tag, index, array) {
    if (!sortedLangTags[index]) return array;
    const matches = matchTag(tag.split('-'), sortedLangTags[index].split('-'), 0, []).join('-');
    if (matches && array.indexOf(matches) === -1) array.push(matches);
    return languagesTagsComparator(tag, ++index, array);
  }
  const priorityList = languagePriorityList(range);
  const directMatch = filterTags(priorityList, 0, [], directTagMatch, false)[0];
  return directMatch || filterTags(priorityList, 0, [], languagesTagsComparator, false)[0] || defaultValue;
}
function lookup ( range , languageTags , defaultValue ) { if ( arguments . length !== 3 ) throw new Error ( 'range:String, languageTags:Array, defaultValue:String required!' ) ; if ( ! Array . isArray ( languageTags ) || languageTags . length === 0 ) return defaultValue ; const sortedLangTags = languageTags . sort ( lengthSort ) ; function matchTag ( rangeTags , langTags , index , array ) { if ( index >= langTags . length || index >= rangeTags . length ) return array ; if ( rangeTags [ index ] . length !== 1 ) { if ( ! tagEquality ( rangeTags [ index ] , langTags [ index ] ) ) return array ; array . push ( rangeTags [ index ] ) ; } return matchTag ( rangeTags , langTags , ++ index , array ) ; } function directTagMatch ( tag , index , array ) { if ( ! languageTags [ index ] ) return array ; if ( tagEquality ( tag , languageTags [ index ] ) ) { if ( array . indexOf ( languageTags [ index ] ) === - 1 ) array . push ( languageTags [ index ] ) ; } return directTagMatch ( tag , ++ index , array ) ; } function languagesTagsComparator ( tag , index , array ) { if ( ! sortedLangTags [ index ] ) return array ; const matches = matchTag ( tag . split ( '-' ) , sortedLangTags [ index ] . split ( '-' ) , 0 , [ ] ) . join ( '-' ) ; if ( matches && array . indexOf ( matches ) === - 1 ) array . push ( matches ) ; return languagesTagsComparator ( tag , ++ index , array ) ; } const priorityList = languagePriorityList ( range ) ; const directMatch = filterTags ( priorityList , 0 , [ ] , directTagMatch , false ) [ 0 ] ; return directMatch || filterTags ( priorityList , 0 , [ ] , languagesTagsComparator , false ) [ 0 ] || defaultValue ; }
Lookup produces a single language tag (RFC4647)
 @param {String} range A range string to languagePriorityList.
Lookup produces a single language tag ( RFC4647 ) @param { String } range A range string to languagePriorityList .