Search code examples
javascriptcoffeescriptrickshaw

Convert javascript code into coffescript


Im currently working with Dashing and I need to convert javascript code into coffescript in order to make use of rickshaw graph library. (According to the source code on this website http://code.shutterstock.com/rickshaw/examples/x_axis.html)

I'm trying to have a customized x axis on the graph.

Default Coffescript code= x_axis = new Rickshaw.Graph.Axis.X(graph: @graph)

JS code to be converted

var format = function(n) {

    var map = {
        0: 'zero',
        1: 'first',
        2: 'second',
        3: 'third',
        4: 'fourth'
    };

    return map[n];
}

var x_ticks = new Rickshaw.Graph.Axis.X( {
    graph: graph,
    tickFormat: format
} );

Into coffescript

    var format = function(n) {

    var map = {
        0: 'zero',
        1: 'first',
        2: 'second',
        3: 'third',
        4: 'fourth'
    };

    return map[n];
}


    x_axis = new Rickshaw.Graph.Axis.X(graph: @graph,tickFormat: format)//Make use of format

Solution

  • format = (n)->
    
      map =
        0: 'zero',
        1: 'first',
        2: 'second',
        3: 'third',
        4: 'fourth'
      map[n]