Refactor switch statements to reduce cyclomatic complexity

The concept of cyclomatic complexity is pretty new to me, and when faced with JSHint errors regarding it in the past I've been mostly likely to take the easy way out and skipped over the problem by bumping the maximum complexity. But when faced with a complex switch statement used to add support for directives to Stylperjade 1.2 I decided to solve the problem properly by using a hash table instead.

The problem

The following function has a high cyclomatic complexity:

function parseDirective(directive) {
  switch (directive.name) {
    case 'whitelist':
      options.cssWhitelist.push(directive.value)
      options.jadeWhitelist.push(directive.value)
      break
    case 'csswhitelist':
      options.cssWhitelist.push(directive.value)
      break
    case 'jadewhitelist':
      options.jadeWhitelist.push(directive.value)
      break
    case 'blacklist':
      options.cssBlacklist.push(directive.value)
      options.jadeBlacklist.push(directive.value)
      break
    case 'cssblacklist':
      options.cssBlacklist.push(directive.value)
      break
    case 'jadeblacklist':
      options.jadeBlacklist.push(directive.value)
      break
  }
}

The solution

Refactor the function to make use of a hash table:

var directives =
  { whitelist: function (directive) {
      directives.csswhitelist(directive)
      directives.jadewhitelist(directive)
    }
  , csswhitelist: function (directive) {
      options.cssWhitelist.push(directive.value)
    }
  , jadewhitelist: function (directive) {
      options.jadeWhitelist.push(directive.value)
    }
  , blacklist: function (directive) {
      directives.cssblacklist(directive)
      directives.jadeblacklist(directive)
    }
  , cssblacklist: function (directive) {
      options.cssBlacklist.push(directive.value)
    }
  , jadeblacklist: function (directive) {
      options.jadeBlacklist.push(directive.value)
    }
  }

function parseDirective(directive) {
  var isDirective = directives[directive.name]
  if (isDirective) {
    return isDirective(directive)
  }
  return null
}