Skip to content

Instantly share code, notes, and snippets.

View tcr's full-sized avatar
🚮
bad command or file name

Tim Ryan tcr

🚮
bad command or file name
View GitHub Profile
@tcr
tcr / code.coffee
Created November 1, 2011 01:39
Can you do Date formatting in JavaScript without a big library?
# tiny date formatter
# curly brackets used as a format specifier; can also zero-pad values
# aka (new Date).format("{FullYear}-{Month:2}-{Date:2}")
Date::format = (fmt) ->
fmt.replace /\{([^}:]+)(?::(\d+))?\}/g, (raw, comp, len) =>
unless (n = @["get"+comp]?())? then raw
else (Array(+len|0).join('0')+(n+(if comp == 'Month' then 1 else '')))[-len..]
Date::getFullMonth = ->
@tcr
tcr / syrup.coffee
Created December 1, 2011 15:05
Syrup, a CoffeeScript-like LISP
util = require('util')
###
Syrup, a syntactically-light LISP
Syrup is whitespace-significant. Function calls are made as so:
defn: test []
print: "Cool language"
@tcr
tcr / summary.md
Created January 5, 2012 01:29
How can you make a CLI with Node.js?

Poll stdin, parse results.

@tcr
tcr / summary.md
Created January 8, 2012 18:41
How do you prevent the play button from launching iTunes in OS X Lion?

It seems to require a script for the moment. It disables the launching capability when iTunes is closed, but preserves all other capabilities.

@tcr
tcr / tagr.coffee
Created April 15, 2012 17:23
Array-like HTML manipulation.
class EventEmitter
listeners: (type) ->
if @hasOwnProperty.call (if @_events? then @_events else @_events = {}), type then @_events[type] else @_events[type] = []
on: (args...) -> @addListener args...
once: (type, f) -> @on type, g = (args...) -> f.apply(this, args); @removeListener type, g
addListener: (type, f) ->
if (@listeners(type).push f) > @_maxListeners and @_maxListeners != 0
console?.warn "Possible EventEmitter memory leak detected. #{@_events[type].length} listeners added. Use emitter.setMaxListeners() to increase limit."
@emit "newListener", type, f
this
@tcr
tcr / output.txt
Created April 30, 2012 08:09
Get a DOM element's unique API
<h4>API for video element</h1>
<pre id="asdf" rows="30" cols="30"></pre>
<script>
var asdf = document.getElementById('asdf');
var v = document.createElement('video');
var s = document.createElement('span');
for (var k in v) if (!(k in s)) asdf.innerText += k + (typeof v[k] == 'function' ? '()' : '') + '\n';
</script>
@tcr
tcr / generator.coffee
Created June 17, 2012 08:53
Generators using IcedCoffeeScript
# Generators and "yield" in IcedCoffee
# See: https://developer.mozilla.org/en/JavaScript/Guide/Iterators_and_Generators
# Try it: http://maxtaco.github.com/coffee-script/
# We can mimick much of the Generator functionality of JavaScript 1.7
# using IcedCoffeeScript's Deferrals.
#
# We define a "generator" function that can be given a function and return a generator.
# To mimick yield, we use a passed argument, "cc", and await/defer on it. This holds on
# to the deferral callback until the next time the function is invoked.
@tcr
tcr / tags.json
Created June 21, 2012 17:46
How do Node.js streams work?
["node.js","streams"]
@tcr
tcr / post-tweet.js
Created July 3, 2012 18:30
[rem-node] Post a tweet from the command line.
var rem = require('rem');
var read = require('read');
// Prompt and post a tweet from the command line.
rem.myConsole('twitter', 1.0, function(err, user) {
read({prompt: "Enter a status to tweet: "}, function (err, txt) {
user('statuses/update').post({status: txt}, function (err, json) {
console.log('Response:', err, json);
});
});
@tcr
tcr / login.js
Created July 31, 2012 05:37
Persisting OAuth Bot for Google Docs using REM
// Run this first from the console to create an authentication object.
var rem = require('rem');
var fs = require('fs');
// Create Google Calendar API, prompting for key/secret.
var gdocs = rem.load('google-docs', 3.0, {format: 'xml'}).prompt();
// Authenticate user via the console.
rem.console(gdocs, function (err, user) {