Skip to content

Instantly share code, notes, and snippets.

Avatar
💿

Michael Jackson mjackson

💿
View GitHub Profile
View index.html
<!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);
View string.rb
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
View fixed-position.html
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#wrapper {
width: 800px;
margin: 0 auto;
background: green;
@mjackson
mjackson / days.rb
Created Oct 30, 2010
A small demonstration of how to use string interpolation in a Citrus grammar.
View days.rb
require 'citrus'
Citrus.eval(<<'CODE')
grammar Days
rule every_n_days
('every ' number ' days') {
"INTERVAL=#{number}"
}
end
@mjackson
mjackson / strftime.js
Created Jan 4, 2011
A strftime implementation for JavaScript Date objects.
View strftime.js
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
Pick n random items from an Array quickly
View Array#pick_random.rb
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
Generic add/remove event functions for JavaScript
View events.js
// 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 Jul 9, 2011
Get the intersection of two sorted arrays of numbers or strings.
View intersect.js
// 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 Aug 11, 2011
Demonstrates the difference between Ruby's two different block styles.
View blocks.rb
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 Sep 7, 2011
A mock stream wrapper for node.js
View mockstream.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.