Skip to content

Instantly share code, notes, and snippets.

View mjackson's full-sized avatar
💿

Michael Jackson mjackson

💿
View GitHub Profile
<!DOCTYPE html>
<html>
<head>
<title>XML.js Test</title>
<script type="text/javascript" src="xml.js"></script>
<script type="text/javascript">
var xml = new XML('root');
xml.attr('one', 1);
require 'citrus'
Citrus.eval(<<'CODE')
grammar RubyString
rule string
single_quoted_string | double_quoted_string
end
# This should be expanded to account for escaped single quotes.
rule single_quoted_string
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#wrapper {
width: 800px;
margin: 0 auto;
background: green;
@mjackson
mjackson / days.rb
Created October 30, 2010 20:55
A small demonstration of how to use string interpolation in a Citrus grammar.
require 'citrus'
Citrus.eval(<<'CODE')
grammar Days
rule every_n_days
('every ' number ' days') {
"INTERVAL=#{number}"
}
end
@mjackson
mjackson / strftime.js
Created January 4, 2011 18:53
A strftime implementation for JavaScript Date objects.
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var shortDays = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var shortMonths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
function zeropad(n) {
return n > 9 ? String(n) : "0" + String(n);
}
function twelveHour(t) {
@mjackson
mjackson / Array#pick_random.rb
Created May 20, 2011 21:19
Pick n random items from an Array quickly
class Array
# Pick +n+ random items from this array.
def pick_random(n=1)
sort_by { rand }.slice(0...n)
end
end
@mjackson
mjackson / events.js
Created May 31, 2011 18:05
Generic add/remove event functions for JavaScript
// Event handling functions modified from originals by Dean Edwards.
// http://dean.edwards.name/my/events.js
var guid = 1;
// Adds an event handler to the given element. The handler will be called
// in the context of the element with the event object as its only argument.
function addEvent(element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
@mjackson
mjackson / intersect.js
Created July 9, 2011 18:32
Get the intersection of two sorted arrays of numbers or strings.
// Returns the intersection of two sorted arrays of numbers or strings.
function intersectArrays(a, b) {
var array = [], ai = 0, alen = a.length, bi = 0, blen = b.length;
while (ai < alen && bi < blen) {
if (a[ai] < b[bi]) {
ai++;
} else if (a[ai] > b[bi]) {
bi++;
} else {
@mjackson
mjackson / blocks.rb
Created August 11, 2011 18:30
Demonstrates the difference between Ruby's two different block styles.
def a(*args, &block)
puts "a got a block" if block_given?
end
def b(*args, &block)
puts "b got a block" if block_given?
end
# In this call, `b' is called with the block and the
# return value is given to `a' as an argument.
@mjackson
mjackson / mockstream.js
Created September 7, 2011 17:31
A mock stream wrapper for node.js
var util = require("util"),
Stream = require("stream").Stream;
module.exports = MockStream;
/**
* A constructor that inherits from Stream and emits data from the given
* `source`. If it's a Stream it will be piped through to this stream.
* Otherwise, it should be a string or a Buffer which will be emitted by this
* stream as soon as possible.