Skip to content

Instantly share code, notes, and snippets.

View flesch's full-sized avatar

John Flesch flesch

View GitHub Profile
@flesch
flesch / extend.js
Created July 3, 2012 15:12
Shallow Copy of an Object
function extend(a, b) {
var c = {}, attr;
for (attr in a) {
if (Object.prototype.hasOwnProperty.call(a, attr)) {
c[attr] = a[attr];
}
}
for (attr in b) {
if (Object.prototype.hasOwnProperty.call(b, attr)) {
c[attr] = b[attr];
@flesch
flesch / getQueryParam.js
Last active October 6, 2015 23:38
Query String Memoization
function getQueryParam(param) {
if (!this.params) {
this.params = (function (query) {
var obj = {};
if (query) {
var i = query.length, keyValuePair;
while (i--) {
keyValuePair = query[i].split('=');
if (keyValuePair[0] && keyValuePair[1]) {
obj[keyValuePair[0]] = keyValuePair[1];
@flesch
flesch / README.md
Created July 28, 2012 03:44
Fake SCORM API

Fake SCORM API

Some courses won't work outside of an LMS. That's annoying when you just want to review a course, so drop this in to provide a fake SCORM API implementation - getAPI should then pick up this fake API.

@flesch
flesch / getElementsByClassName.php
Created August 18, 2012 14:49
document::getElementsByClassName("")
<?php
class document {
public static function getElementsByClassName($name) {
global $xpath;
$nodes = array();
$elements = $xpath->query(sprintf("//div[contains(@class, '%s')]", $name));
foreach ($elements as $e) {
array_push($nodes, $e);
}
@flesch
flesch / list_dir.php
Created August 18, 2012 15:00
Recursive Directory Listing in PHP
<?php
function list_dir($resource) {
$files = array();
$scan = glob(rtrim($resource, '/') . '/*');
if (is_file($resource)) {
array_push($files, $resource);
}
if (is_dir($resource)) {
foreach ($scan as $path) {
@flesch
flesch / sequence-as2.as
Created August 18, 2012 15:12
Convert to and from a binary sequence
function str2seq(str:String):String {
var seq:String = "", byte:String, len:Number = str.length, i:Number = 0;
for (; i<len; i++) {
byte = parseInt(ord(str.charAt(i)), 10).toString(2);
seq += "00000000".substr(0, 8-byte.length) + byte;
}
return seq;
}
function seq2str(seq:String):String {
@flesch
flesch / server.coffee
Created August 18, 2012 15:19
Simple node.js static file server.
http = require "http"
url = require "url"
path = require "path"
fs = require "fs"
http.createServer (request, response) ->
file = path.join process.cwd(), url.parse(request.url).pathname
fs.exists file, (exists) ->
@flesch
flesch / CORS.coffee
Created August 18, 2012 15:27
CORS headers (Express)
app.get "*", (request, response, next) ->
response.header "Access-Control-Allow-Origin", "*"
response.header "Access-Control-Allow-Methods", "GET, SETTINGS"
response.header "Access-Control-Allow-Credentials", "true"
response.header "Access-Control-Max-Age", "0"
response.header "Cache-Control", "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0"
return next()
@flesch
flesch / sanitize.coffee
Created August 18, 2012 15:30
Replace the goofy characters Word uses.
sanitize = (str, html) ->
replacements =
"\xa9": if html then "&copy;" else "(c)",
"\xae": if html then "&reg;" else "(r)",
"\u2018": "'",
"\u2019": "'",
"\u201c": if html then "&quot;" else "\"",
"\u201d": if html then "&quot;" else "\"",
"\u2026": "...",
"\u2013": if html then "&ndash;" else "-",
@flesch
flesch / event.js
Created August 18, 2012 15:32
AS2 Event Dispatcher in JS
(function(window){
var EventDispatcher = window.EventDispatcher = function(scope){
this.scope = scope;
this.listeners = {};
};
EventDispatcher.prototype.addEventListener = function(type, listener){
if (listener instanceof Function) {
if(!this.listeners[type]) {