Kaffeine

Try Kaffeine

Plugins

Kaffeine uses a plugin style approach, with a core/standard stack (order is important)

Example:

This is an example taken from production code. It show most of Kaffeine's features:

Edge::add = (nick, name, complete) {
  @client.select 15
  user = User.find! {id: nick}
  puzzle = Puzzle.find! {name: name}
  err, data = client.set! "u:#{user}:p:#{puzzle}"
  complete()
}

Bang

Allows function calls with callbacks to be unwrapped via a ! postfix. E.g:

fish = $.get!('/fish')
$("stomach").append(fish)

at

Provide the @ alias for 'this'. It is also linked to the parent 'this' in the case of a function defined via 'async!'. E.g:

@legs = legs
@color = color

implicit var

provides support for omitting the var keyword: the variables will be automagically defined in the closest relevant closure. E.g.

x = 1
y = 2

implicit brackets

Provide optional brackets for function calls. E.g.

remove eggs.shell
mix eggs, milk

multiline strings

Allow multiline strings:

html = "
<body>
<h1>SOY SAUCE</h1>
</body>
"

This would maintain the new lines --- but they can be suppressed with the \ character

extended for

Allows an 'of' operator for looping through arrays:

for(x of [7,3,4])
  sum += x
// sum == 14

Allows allows an optional second parameter to refer to the key or value:

for(x, i of [7,3,4])
  sum += i
// sum == 3

string interpolation

provides ruby style string interpolation via #{}

letter = "Dear #{name},
I am writing to you to inform you of #{purpose}
Kind Regards
#{sender}
"

implicit return

the last statement of a function will be automagically returned (if it has a value). E.g.
getName = { @name } 

hash

provides the # shortcut for referring to the first argument in a function. Additionally, #n refers to the nth argument (n >= 0). Useful for terse function definitions, e.g:

square = { #*# }

implicit_functions

the function keyword can be optionally omitted, along with empty argument lists.

ok = (timeout) {
  sendNote()
  setTimeout { run() }, timeout
  return true
}

Note {} could be ambiguously a function or an object, so it's defined to be an function, to express an empty function, use one of the following :

{;}
{null}
function() { }

pipe

Provides an alternative calling method than can be used for easily chaining. Useful for ruby style enumeration chaining without using prototypes

| = require "pipe_utils" // we can specify the module to use with pipe
People | map { #.name } | detect { #.length > 3 }

Kaffeine

Javascript