Skip to content

Instantly share code, notes, and snippets.

@donpark
donpark / toutf8.js
Created July 13, 2013 16:00
Convert JavaScript string to UTF-8 bytes in modern browsers using the File API
// Convert JavaScript string to UTF-8 bytes in modern browsers using the File API
// callback function will be called with UTF8 bytes in Uint8Array
//
// jsPerf test reveals this method is slower than looping over each character.
// http://jsperf.com/convert-javascript-string-to-utf-8-bytes-using-file-api
function toUTF8(string, cb) {
var reader = new FileReader();
reader.onload = function(evt) {
cb(new Uint8Array(reader.result));
@donpark
donpark / temple.coffee
Created June 30, 2013 04:21
Sketchy coffee-script that generates precompiled Hogan templates from a HTML file containing script tags of type `text/x-mustache-template'.
FS = require("fs")
Path = require("path")
Cheerio = require("cheerio")
Hogan = require("hogan.js")
htmlFile = process.argv[2]
html = FS.readFileSync process.argv[2], encoding: "utf8"
# console.log html
@donpark
donpark / npm-1.3.0-outdated.txt
Created June 22, 2013 04:40
output of `npm outdated -g` on npm version 1.3.0
$ npm -v
1.3.0
don@ironforge:~
$ npm outdated -g
[email protected] /usr/local/lib/node_modules/c-pm/node_modules/superagent current=0.9.10
[email protected] /usr/local/lib/node_modules/uglify-js/node_modules/optimist current=0.3.7
[email protected] /usr/local/lib/node_modules/docco/node_modules/commander current=1.2.0
[email protected] /usr/local/lib/node_modules/recess/node_modules/less current=1.3.3
[email protected] /usr/local/lib/node_modules/yo/node_modules/yeoman-generator current=0.11.4
@donpark
donpark / timeutil.coffee
Created June 18, 2013 02:34
Time and timer related utility functions in Coffee-Script
TimeUtil =
now: Date.now or -> new Date().getTime()
# Returns wrapped function that no-ops if called within specified
# duration in milliseconds of last call.
throttle: (duration, fn) ->
last = 0
->
now = TimeUtil.now()
if now - last < duration
@donpark
donpark / npm-debug.log
Created April 12, 2013 02:16
npm-debug.log for npm issue #3339
0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'publish' ]
2 info using [email protected]
3 info using [email protected]
4 verbose publish [ '.' ]
5 verbose read json /Users/don/dev/github/node-tld/package.json
6 verbose cache add [ '.', null ]
7 verbose cache add name=undefined spec="." args=[".",null]
8 verbose parsed url { protocol: null,
8 verbose parsed url slashes: null,
@donpark
donpark / fabfile.py
Created August 28, 2012 01:07
fabric/python script for launching node-inspector
import os
import time
def terminal(cmd):
local("osascript -e 'tell application \"Terminal\" to do script \"%s\"'" % cmd)
def debugger(opts):
url = "http://0.0.0.0:8080/debug?port=5858"
build()
terminal("node %(opts)s %(cwd)s/app.js" % { "opts": opts, "cwd": os.getcwd() })
@donpark
donpark / timing.coffee
Created August 22, 2012 00:08
Simple high-resolution timing wrapper module in CoffeeScript
if process.hrtime
console.log "using process.hrtime for timing"
clock = process.hrtime
else
console.log "using microtime for timing"
Microtime = require('microtime')
clock = (s) ->
t = Microtime.nowStruct()
t[1] *= 1000 # microseconds to nanoseconds
if s
@donpark
donpark / resizer.coffee
Created May 3, 2012 18:08
Deferred Resizer in CoffeeScript
# Delayed resize task runner
class DeferredResizer
constructor: ->
@timer = null
@tasks = []
addTask: (task) ->
@tasks.push(task) if @tasks.indexOf(task) is -1
doTasks: =>
@timer = null
@donpark
donpark / tlc.lua
Created March 28, 2012 18:24 — forked from fjolnir/tlc.lua
LuaJIT ObjC bridge
-- TLC - The Tiny Lua Cocoa bridge
-- Note: Only tested with LuaJit 2 Beta 9 on x86_64 with OS X >=10.7.3 & iPhone 4 with iOS 5
-- Copyright (c) 2012, Fjölnir Ásgeirsson
-- Permission to use, copy, modify, and/or distribute this software for any
-- purpose with or without fee is hereby granted, provided that the above
-- copyright notice and this permission notice appear in all copies.
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
@donpark
donpark / sdp2json.coffee
Created March 15, 2012 00:05
Parse SDP to JSON in Coffee
parseSDP = (text) ->
return if not text
lines = text.split('\r\n')
readSession lines
readSession = (lines) ->
session = {}
line = null
while line = lines.shift()
name = line.charAt(0)