Skip to content

Instantly share code, notes, and snippets.

View devongovett's full-sized avatar

Devon Govett devongovett

View GitHub Profile
# Syntax for private methods in CoffeeScript
class Test
publicMethod: ->
alert @foo
private
foo: "hello world!"
privateMethod ->
"private"
#this is the coffeescript with method overloading
class User
find: (id) ->
console.log "find by id"
find: (first, last) ->
console.log "find by first and last name"
class NewUser extends User
// 1: how could you rewrite the following to make it shorter?
foo ? bar.doSomething(el) : bar.doSomethingElse(el);
// 2: what is the faulty logic in the following code?
//you are creating a local version of foo instead of overwriting
//the global version. Thus, foo will always be equal to world.
//The problem is fixed below.
var foo = 'hello';
This CoffeeScript:
15 + if somevar then "%"
used to compile to this JavaScript:
15 + (somevar ? "%" : null)
which returned 15 if somevar was false.
Now it compiles to:
@devongovett
devongovett / example.coffee
Created June 4, 2011 17:09
A CoffeeScript class that parses and renders SVG paths to the <canvas> element.
# fill path to ctx
Path.fill(ctx, "M250 150 L150 350 L350 350 Z")
# stroke
Path.stroke(ctx, "M250 150 L150 350 L350 350 Z")
# clip
Path.render(ctx, "M250 150 L150 350 L350 350 Z")
ctx.clip()
@devongovett
devongovett / gist:1037265
Created June 21, 2011 04:47
jsondb + msgpack
var arr = [],
obj = {'abcdef' : 1, 'qqq' : 13, '19' : [1, 2, 3, 4]};
for(var i = 0; i < 5000; i++)
arr.push(obj);
// jsondb(arr) looks something like this:
// ["abcdef", "qqq", "19", [1, 13, [1, 2, 3, 4]], [1, 13, [1, 2, 3, 4]]...]
> JSON.stringify(arr).length //original
@devongovett
devongovett / gist:1039559
Created June 22, 2011 05:27
JSONDB implementation
###
# JSONDB - a compressed JSON format
# By Devon Govett
# Originally proposed by Peter Michaux - http://michaux.ca/articles/json-db-a-compressed-json-format
#
# jsondb.pack converts an array of objects with the same keys (i.e. from a database)
# and flattens them into a single array, which is much smaller than the pure json
# representation where the keys are repeated for each item in the array.
# Combine with JSON.stringify to send compressed JSON data over the network.
#
PDFDocument = require 'pdfkit'
doc = new PDFDocument
# Embed a font, set the font size, and render some text
doc.font('fonts/PalatinoBold.ttf')
.fontSize(25)
.text('Some text with an embedded font!', 100, 100)
# Add another page
doc.addPage()
doc.addPage
size: 'legal'
layout: 'landscape'
@devongovett
devongovett / alignments.coffee
Created July 10, 2011 20:55
PDFKit Examples part 2
lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed pulvinar diam eu ' +
'dolor bibendum et varius diam laoreet. Morbi eget rutrum enim. Sed enim ipsum, ' +
'posuere nec hendrerit non, commodo quis tortor. Fusce id nisl augue. Fusce at ' +
'lectus ut libero vehicula imperdiet.'
doc.text 'This text is left aligned. ' + lorem, 100, 100,
width: 410
align: 'left'
doc.moveDown()