src/index.js
function addWeight (canonicalNodeWithPath, query) {
  const cnwp = canonicalNodeWithPath

  const name = presentableName(cnwp.node, preferredLocale)

  const synonym = cnwp.path
    .map(pathNode => presentableName(pathNode.node, pathNode.locale))
    .map(nameInPath => nameInPath.toLowerCase())
    .pop()

  const indexOfQuery = indexOfLowerCase(name, query)

  const isUk = name === 'United Kingdom'
  const isUs = name === 'United States'

  // Temporary country weighting
  const ukBias = 2
  const usBias = 1.5
  const defaultCountryBias = 1

  const isExactMatchToCanonicalName = name.toLowerCase() === query.toLowerCase()

  const canonicalNameStartsWithQuery = indexOfQuery === 0

  const wordInCanonicalNameStartsWithQuery = name
      .split(' ')
      .filter(w => w.toLowerCase().indexOf(query.toLowerCase()) === 0)
      .length > 0

  // TODO: make these const
  var synonymIsExactMatch = false
  var synonymStartsWithQuery = false
  var wordInSynonymStartsWith = false
  var indexOfSynonymQuery = false
  var synonymContainsQuery = false

  if (synonym) {
    synonymIsExactMatch = synonym === query.toLowerCase()

    synonymStartsWithQuery = synonym
      .indexOf(query.toLowerCase()) === 0

    wordInSynonymStartsWith = synonym
      .split(' ')
      .filter(w => w.toLowerCase().indexOf(query.toLowerCase()) === 0)
      .length > 0

    indexOfSynonymQuery = indexOfLowerCase(synonym, query)
  }

  // TODO: Contains consts don't work
  const canonicalNameContainsQuery = indexOfQuery > 0
  synonymContainsQuery = indexOfSynonymQuery > 0

  // Canonical name matches
  if (isExactMatchToCanonicalName) {
    cnwp.weight = 100
  } else if (canonicalNameStartsWithQuery) {
    cnwp.weight = 76
  } else if (wordInCanonicalNameStartsWithQuery) {
    cnwp.weight = 60
  } else if (synonymIsExactMatch) { // Synonmn matches
    cnwp.weight = 50
  } else if (synonymStartsWithQuery) {
    cnwp.weight = 45
  } else if (wordInSynonymStartsWith) {
    cnwp.weight = 37
  } else if (canonicalNameContainsQuery) { // Contains (DOES NOT WORK YET)
    cnwp.weight = 25
  } else if (synonymContainsQuery) {
    cnwp.weight = 22
  } else { // Not sure if else is possible
    cnwp.weight = 15
  }

  // Longer paths mean canonical node is further from matched synonym, so rank it lower.
  // TODO - pruning multiple matches should happen elsewhere
  cnwp.weight -= cnwp.path.length

  var countryBias = isUk ? ukBias : defaultCountryBias
  countryBias = isUs ? usBias : countryBias

  cnwp.weight *= countryBias

  return cnwp
}
function addWeight (canonicalNodeWithPath, query) {
  const cnwp = canonicalNodeWithPath

  const name = presentableName(cnwp.node, preferredLocale)

  const synonym = cnwp.path
    .map(pathNode => presentableName(pathNode.node, pathNode.locale))
    .map(nameInPath => nameInPath.toLowerCase())
    .pop()

  const indexOfQuery = indexOfLowerCase(name, query)

  const isUk = name === 'United Kingdom'
  const isUs = name === 'United States'

  // Temporary country weighting
  const ukBias = 2
  const usBias = 1.5
  const defaultCountryBias = 1

  const isExactMatchToCanonicalName = name.toLowerCase() === query.toLowerCase()

  const canonicalNameStartsWithQuery = indexOfQuery === 0

  const wordInCanonicalNameStartsWithQuery = name
      .split(' ')
      .filter(w => w.toLowerCase().indexOf(query.toLowerCase()) === 0)
      .length > 0

  // TODO: make these const
  var synonymIsExactMatch = false
  var synonymStartsWithQuery = false
  var wordInSynonymStartsWith = false
  var indexOfSynonymQuery = false
  var synonymContainsQuery = false

  if (synonym) {
    synonymIsExactMatch = synonym === query.toLowerCase()

    synonymStartsWithQuery = synonym
      .indexOf(query.toLowerCase()) === 0

    wordInSynonymStartsWith = synonym
      .split(' ')
      .filter(w => w.toLowerCase().indexOf(query.toLowerCase()) === 0)
      .length > 0

    indexOfSynonymQuery = indexOfLowerCase(synonym, query)
  }

  // TODO: Contains consts don't work
  const canonicalNameContainsQuery = indexOfQuery > 0
  synonymContainsQuery = indexOfSynonymQuery > 0

  // Canonical name matches
  if (isExactMatchToCanonicalName) {
    cnwp.weight = 100
  } else if (canonicalNameStartsWithQuery) {
    cnwp.weight = 76
  } else if (wordInCanonicalNameStartsWithQuery) {
    cnwp.weight = 60
  } else if (synonymIsExactMatch) { // Synonmn matches
    cnwp.weight = 50
  } else if (synonymStartsWithQuery) {
    cnwp.weight = 45
  } else if (wordInSynonymStartsWith) {
    cnwp.weight = 37
  } else if (canonicalNameContainsQuery) { // Contains (DOES NOT WORK YET)
    cnwp.weight = 25
  } else if (synonymContainsQuery) {
    cnwp.weight = 22
  } else { // Not sure if else is possible
    cnwp.weight = 15
  }

  // Longer paths mean canonical node is further from matched synonym, so rank it lower.
  // TODO - pruning multiple matches should happen elsewhere
  cnwp.weight -= cnwp.path.length

  var countryBias = isUk ? ukBias : defaultCountryBias
  countryBias = isUs ? usBias : countryBias

  cnwp.weight *= countryBias

  return cnwp
}
function addWeight ( canonicalNodeWithPath , query ) { const cnwp = canonicalNodeWithPath const name = presentableName ( cnwp . node , preferredLocale ) const synonym = cnwp . path . map ( pathNode => presentableName ( pathNode . node , pathNode . locale ) ) . map ( nameInPath => nameInPath . toLowerCase ( ) ) . pop ( ) const indexOfQuery = indexOfLowerCase ( name , query ) const isUk = name === 'United Kingdom' const isUs = name === 'United States' // Temporary country weighting const ukBias = 2 const usBias = 1.5 const defaultCountryBias = 1 const isExactMatchToCanonicalName = name . toLowerCase ( ) === query . toLowerCase ( ) const canonicalNameStartsWithQuery = indexOfQuery === 0 const wordInCanonicalNameStartsWithQuery = name . split ( ' ' ) . filter ( w => w . toLowerCase ( ) . indexOf ( query . toLowerCase ( ) ) === 0 ) . length > 0 // TODO: make these const var synonymIsExactMatch = false var synonymStartsWithQuery = false var wordInSynonymStartsWith = false var indexOfSynonymQuery = false var synonymContainsQuery = false if ( synonym ) { synonymIsExactMatch = synonym === query . toLowerCase ( ) synonymStartsWithQuery = synonym . indexOf ( query . toLowerCase ( ) ) === 0 wordInSynonymStartsWith = synonym . split ( ' ' ) . filter ( w => w . toLowerCase ( ) . indexOf ( query . toLowerCase ( ) ) === 0 ) . length > 0 indexOfSynonymQuery = indexOfLowerCase ( synonym , query ) } // TODO: Contains consts don't work const canonicalNameContainsQuery = indexOfQuery > 0 synonymContainsQuery = indexOfSynonymQuery > 0 // Canonical name matches if ( isExactMatchToCanonicalName ) { cnwp . weight = 100 } else if ( canonicalNameStartsWithQuery ) { cnwp . weight = 76 } else if ( wordInCanonicalNameStartsWithQuery ) { cnwp . weight = 60 } else if ( synonymIsExactMatch ) { // Synonmn matches cnwp . weight = 50 } else if ( synonymStartsWithQuery ) { cnwp . weight = 45 } else if ( wordInSynonymStartsWith ) { cnwp . weight = 37 } else if ( canonicalNameContainsQuery ) { // Contains (DOES NOT WORK YET) cnwp . weight = 25 } else if ( synonymContainsQuery ) { cnwp . weight = 22 } else { // Not sure if else is possible cnwp . weight = 15 } // Longer paths mean canonical node is further from matched synonym, so rank it lower. // TODO - pruning multiple matches should happen elsewhere cnwp . weight -= cnwp . path . length var countryBias = isUk ? ukBias : defaultCountryBias countryBias = isUs ? usBias : countryBias cnwp . weight *= countryBias return cnwp }
Takes a canonical node with the path to reach it, and the typed in query. Returns the same node and path, with added weight based on a number of criteria. Higher weight means higher priority, so it should be ranked higher in the list.
Takes a canonical node with the path to reach it and the typed in query . Returns the same node and path with added weight based on a number of criteria . Higher weight means higher priority so it should be ranked higher in the list .