Show Menu

CoffeeScript Cheat Sheet by dimitrios

CoffeeScript cheat sheet compiled after following "CoffeeScript Koans" from sleepyfox

Usage

-c, --compile
-i, --inte­ractive
-o, --output [DIR]
-p, --print
-e, --eval
--nodejs

most commonly used parameters from coffee­scr­ipt.org

Functions

#Last expression value is return value
fill = (conta­iner, liquid = "­cof­fee­") ->
 ­ ­"­Filling the #{cont­ainer} with #{liqu­id}..."­

# internal variables override outer ones

message = 'Outer'
getMessage = -> message
overri­deM­essage = -> message = 'Inner'
overri­deM­ess­age() == 'Inner'

# supports splats
return­AllArgs = (allar­gs...) -> allargs
return­All­Arg­s('­first', 'second', 'third') == ['first', 'second', 'third']

return­All­But­First = (firstArg, rest...) -> rest
return­All­But­Fir­st(­'fi­rst', 'second', 'third') == ['second', 'third']

#dest­ruc­turing assign­ment
weathe­rReport = (location) -> [location, 22, 'Mostly sunny']
[city, temper­ature, forecast] = weathe­rReport 'London'
city == 'London'
temper­ature == 22

Higher Order

2 in [1..3] == true

#Java­scr­ipt­-style filter
even = (a) -> a % 2 == 0
[1..6].filter even == [2, 4, 6]

# Coffee­Scr­ipt­-style filter
odds = (x for x in [1..6] when not even x)

# Coffee­Scr­ipt­-style map
twice = (a) -> a * 2
doubles = (twice x for x in [1..6])
 

Objects

 ­ ­ ­ ­meg­lom­aniac = {}
 ­ ­ ­ ­bef­oreEach ->
 ­ ­ ­ ­ ­ ­meg­lom­aniac =
 ­ ­ ­ ­ ­ ­ ­ ­mas­ter­mind: 'The Monarch'
 ­ ­ ­ ­ ­ ­ ­ ­hen­chw­oman: 'Dr Girlfr­iend'
 ­ ­ ­ ­ ­ ­ ­ ­the­Bomb: true

# ? existence operator
meglom­ani­ac.t­he­Bom­b? == true
meglom­ani­ac.t­he­Det­ona­tor­? == false

# properties can be added and deleted
meglom­ani­ac.m­as­ter­mind2 = 'Agent Smith'
delete meglom­ani­ac.m­as­termind

# prototype to add to all projects
Circle = (radius) -> @r­adius = radius

'@' = 'this context' In Coffee­script

Inheritance

# running example from Muppets

class Muppet
 ­ ­con­str­uctor: (@age, @hobby) ->
 ­ ­ans­wer­Nanny: -> "­Eve­ryt­hing's cool!"

class Swedis­hChef extends Muppet
 ­ ­con­str­uctor: (age, hobby, @mood) ->
 ­ ­ ­ ­sup­er(age, hobby)
 ­ ­cook: -> 'Mmmm soup!'

@swedi­shChef = new Swedis­hChef 3, 'cooking', 'chillin'

@swedi­shC­hef.cook() == 'Mmmm soup!'

#base object
@swedi­shC­hef.an­swe­rNa­nny() == "­Eve­ryt­hing's cool!"

#instances to override class methods
gonzo = new Muppet 3, 'daredevil performer'
gonzo.a­ns­wer­Nanny = -> 'Hehehe!'

Hello World

echo "­con­sol­e.log 'Hello World'­" > hello.c­offee
coffee hello.c­offee
 

Arrays

[1..5] == [1, 2, 3, 4, 5]
[1...5] == [1, 2, 3, 4] # extra dot
[3..1] == [3, 2, 1]

fourNu­mbe­rArray = [1, 2, 3, 4]
fourNu­mbe­rAr­ray.pu­sh(5, 6)
fourNu­mbe­rArray == [1, 2, 3, 4, 5, 6]

[1..10­][3..5] == [4, 5, 6] # range slicing

"my string­"­[0..1] == "­my" # string slicing

#iterate with hasOwn­Pro­perty check
for own key, value of object

copyOf­Array = array.s­lice()

Array Reduction

# Javascript style
total = (i, a) -> i + a
reduction = [1..3].reduce total == 6

# Coffee­scr­ipt­-style
total = 0
sum = (a) -> total = total + a
sum x for x in [1..3]
total == 6

MDN Docume­nta­tion:
http:/­/ti­nyu­rl.c­om­/ar­reduce

Credits

This cheat sheet is (mostly) based on work of sleepyfox, which can be found here: https:­//g­ith­ub.c­om­/sl­eep­yfo­x/c­off­ees­cri­pt-­koans

Coffee­Script @github:
http:/­/ja­shk­ena­s.g­ith­ub.c­om­/co­ffe­e-s­cript/

Logo

 

Share This Cheat Sheet!

Favourited by 11 Members:

caroline.artz icebob internaciulo Theosis webdevo kyamaguchi gorillawit cpettit42 xiongbo Psyco21 specialbrand

Comments

DaveChild DaveChild, 09:08 24 Dec 11

Good stuff, dimitrios. And excellent timing - I've been diving into Coffeescript and node.js recently, so this will come in handy :)

dimitrios dimitrios, 14:03 31 Dec 11

Changing and adding bits and pieces every day, diverting from the koans to something separate. Hoping that something useful is in the making.

Chris Chris, 16:13 4 Jan 12

Without suitable indenting in the cheat sheet, this is rather useless.

dimitrios dimitrios, 22:50 4 Jan 12

@Chris: Platform issue: spaces are not translated. Tried to use ampersand-nbsp-semi colon for html, but do not know how it will look like on the PDF (I used to print the cheat sheets). In that case indentation issues affect the "middle" column, so even in that case not all of it is "useless", I believe.

DaveChild DaveChild, 08:36 5 Jan 12

Hi dimitrios. The amperand-nbsp-semicolon should now work properly (and I think you're using it in a couple of places) - please give me a shout if not.

dimitrios dimitrios, 15:09 5 Jan 12

@DaveChild You are right, I found that out after reading the comments of @Chris and @Josh. The thought was: let's give it a go and see what happens... I was worried about PDF creation, but that is also OK. Initially I thought I could only bold/italic/superscript stuff... The whole talk made the cheat sheet better :-).

Josh Josh, 07:47 5 Jan 12

Appreciate the effort but the formatting leaves something to be desired. Also not feeling the Google ads smack in the middle of the cheatsheet. Probably not something I will use.

dimitrios dimitrios, 15:11 5 Jan 12

About formatting, I think you are right and have corrected it. Ads are part of the platform and not a choice of the person who writes the cheat sheet, therefore I cannot comment.

Sela Sela, 07:47 5 Jan 12

does it make coffee for me? i need first to install it. you should have brief explanation how to install it for ppl like me.

dimitrios dimitrios, 15:17 5 Jan 12

Sela, there are many ways to install CoffeeScript. For my OSX machine that has brew, I did a
brew update
to get the formula and then
brew install coffeescript

There are stand alone versions for windows for example, while I believe that you can use it without any modifications on Rails v3 and above. The answer depends on your setup.
From this article in wikipedia (http://en.wikipedia.org/wiki/CoffeeScript): "The CoffeeScript compiler has been written in CoffeeScript since version 0.5 and is available as a Node.js utility; however, the core compiler does not rely on Node.js and can be run in any JavaScript environment[11]. One alternative to the Node.js utility is the Coffee Maven Plugin, a plugin for the popular Apache Maven build system. The plugin works by utilizing Mozilla Rhino, a JavaScript engine written in Java. The official site at CoffeeScript.org has a "Try CoffeeScript" button in the menu bar; clicking it opens a modal window in which you can enter CoffeeScript, see the JavaScript output, and run it directly in the browser."

Generally speaking you will install once per project/machine/environment and code more times, so decided not to put installation instructions there.

Sidharth Shah Sidharth Shah, 07:47 5 Jan 12

Hi we develop Android apps. I always wanted to create a killer Coffee Script Android Tablet and Android Widget app. Obviously we will make it free, so just wanted to check if you're ok with us using the content.

dimitrios dimitrios, 15:22 5 Jan 12

Shah,
I have mostly used examples provided from other sources, and very few bits of this are my own. For me, you are more than welcome to use it as you describe.

grifotv grifotv, 12:51 11 Apr 12

Great stuff. I just published my portfolio ( www.grifo.tv ) built with CoffeeScript, Backbone.js, Paper.js and Google Docs spreadsheet as CMS. It is opensourced on Github ( https://github.com/grifotv/grifotv-portfolio ), feel free to fork it.

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.