Guest User

Untitled

a guest
Oct 2nd, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. _ = require 'underscore'
  2.  
  3. YobaEnum =
  4.     PSHHH_PSSHHH: 0
  5.     ALLO_ETO_YOBA: 1
  6.     NYET: 2
  7.  
  8. genSwitch = (EnumType) ->
  9.     keys = _.keys EnumType
  10.     names = _.invert EnumType
  11.     (value, obj) ->
  12.         if not ('default' of obj) and not (_.isEqual _.keys(obj), keys)
  13.             throw new Error "hui sasi"
  14.         else
  15.             obj[names[value] ? 'default']()
  16.  
  17. yobaSwitch = genSwitch YobaEnum
  18.  
  19. x = YobaEnum.PSHHH_PSSHHH + 10
  20. yobaSwitch x,
  21.     PSHHH_PSSHHH: => console.log "PSHHH"
  22.     default: => console.log "DEFAULT"
  23.  
  24. yobaSwitch x,
  25.     PSHHH_PSSHHH: => console.log "PSHHH"
  26.     NYET: => console.log "NYET"
Advertisement
Add Comment
Please, Sign In to add comment