Skip to content

Instantly share code, notes, and snippets.

@DanielFGray
Last active April 10, 2018 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielFGray/68513765b1ccae97c1b0e975f794f88e to your computer and use it in GitHub Desktop.
Save DanielFGray/68513765b1ccae97c1b0e975f794f88e to your computer and use it in GitHub Desktop.
formatting of different branching styles

nested ternary conditional

const type = a => (
  a === null ? 'null'
  : a === undefined ? 'undefined'
  : Object.prototype.toString.call(a).slice(8, -1))

if/else

const type = a => {
  if (a === null) return 'null'
  else if (a === undefined) return 'undefined'
  return Object.prototype.toString.call(a).slice(8, -1)
}

R.cond

const type = cond([
  [eq(null), () => 'null'],
  [eq(undefined), () => 'undefined'],
  [T, a => Object.prototype.toString.call(a).slice(8, -1)]
])

source

sweet.js macro

const realTypeof = a => cond {
  case a === null: 'null'
  case a === undefined: 'undefined'
  default: Object.prototype.toString.call(a).slice(8, -1)
}

source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment