index.js
function combineAliases (aliases) {
  var aliasArrays = []
  var change = true
  var combined = {}

  // turn alias lookup hash {key: ['alias1', 'alias2']} into
  // a simple array ['key', 'alias1', 'alias2']
  Object.keys(aliases).forEach(function (key) {
    aliasArrays.push(
      [].concat(aliases[key], key)
    )
  })

  // combine arrays until zero changes are
  // made in an iteration.
  while (change) {
    change = false
    for (var i = 0; i < aliasArrays.length; i++) {
      for (var ii = i + 1; ii < aliasArrays.length; ii++) {
        var intersect = aliasArrays[i].filter(function (v) {
          return aliasArrays[ii].indexOf(v) !== -1
        })

        if (intersect.length) {
          aliasArrays[i] = aliasArrays[i].concat(aliasArrays[ii])
          aliasArrays.splice(ii, 1)
          change = true
          break
        }
      }
    }
  }

  // map arrays back to the hash-lookup (de-dupe while
  // we're at it).
  aliasArrays.forEach(function (aliasArray) {
    aliasArray = aliasArray.filter(function (v, i, self) {
      return self.indexOf(v) === i
    })
    combined[aliasArray.pop()] = aliasArray
  })

  return combined
}
function combineAliases (aliases) {
  var aliasArrays = []
  var change = true
  var combined = {}

  // turn alias lookup hash {key: ['alias1', 'alias2']} into
  // a simple array ['key', 'alias1', 'alias2']
  Object.keys(aliases).forEach(function (key) {
    aliasArrays.push(
      [].concat(aliases[key], key)
    )
  })

  // combine arrays until zero changes are
  // made in an iteration.
  while (change) {
    change = false
    for (var i = 0; i < aliasArrays.length; i++) {
      for (var ii = i + 1; ii < aliasArrays.length; ii++) {
        var intersect = aliasArrays[i].filter(function (v) {
          return aliasArrays[ii].indexOf(v) !== -1
        })

        if (intersect.length) {
          aliasArrays[i] = aliasArrays[i].concat(aliasArrays[ii])
          aliasArrays.splice(ii, 1)
          change = true
          break
        }
      }
    }
  }

  // map arrays back to the hash-lookup (de-dupe while
  // we're at it).
  aliasArrays.forEach(function (aliasArray) {
    aliasArray = aliasArray.filter(function (v, i, self) {
      return self.indexOf(v) === i
    })
    combined[aliasArray.pop()] = aliasArray
  })

  return combined
}
function combineAliases ( aliases ) { var aliasArrays = [ ] var change = true var combined = { } // turn alias lookup hash {key: ['alias1', 'alias2']} into // a simple array ['key', 'alias1', 'alias2'] Object . keys ( aliases ) . forEach ( function ( key ) { aliasArrays . push ( [ ] . concat ( aliases [ key ] , key ) ) } ) // combine arrays until zero changes are // made in an iteration. while ( change ) { change = false for ( var i = 0 ; i < aliasArrays . length ; i ++ ) { for ( var ii = i + 1 ; ii < aliasArrays . length ; ii ++ ) { var intersect = aliasArrays [ i ] . filter ( function ( v ) { return aliasArrays [ ii ] . indexOf ( v ) !== - 1 } ) if ( intersect . length ) { aliasArrays [ i ] = aliasArrays [ i ] . concat ( aliasArrays [ ii ] ) aliasArrays . splice ( ii , 1 ) change = true break } } } } // map arrays back to the hash-lookup (de-dupe while // we're at it). aliasArrays . forEach ( function ( aliasArray ) { aliasArray = aliasArray . filter ( function ( v , i , self ) { return self . indexOf ( v ) === i } ) combined [ aliasArray . pop ( ) ] = aliasArray } ) return combined }
if any aliases reference each other, we should merge them together.
if any aliases reference each other we should merge them together .