Last Updated: February 25, 2016
·
454
· martin_sunset

Breaking up "for" clauses in Coffeescript

Someone recently asked this on the mailing list, here is my preferred solution:

for thing in $scope.things when thing.boolprop and !thing.otherboolprop
    #Do some stuff!

becomes

fn = (thing) -> thing.boolprop and !thing.otherboolprop
for thing in $scope.things when fn(thing)
    #Do some stuff!

If you export fn you can unit test it too.