Skip to content

Instantly share code, notes, and snippets.

View oyvindkinsey's full-sized avatar

Øyvind Sean Kinsey oyvindkinsey

View GitHub Profile
@oyvindkinsey
oyvindkinsey / javascript array comparator generator
Created May 21, 2010 07:47
Sort array on sub-array column
var arr = [.....]
arr.sort((function(index){
return function(a, b){
if (!a) {
return -1;
} else if (!b) {
return 1;
} else if (!a && !b) {
return 0;
}
var mylib = {};
(function(context){
var easyXDM;
//include easyXDM here
//enjoy your closured ref to easyXDM here
mylib.easyXDM = easyXDM;
})(mylib);
@oyvindkinsey
oyvindkinsey / Weird switch statement.js
Created January 19, 2011 16:22
How to make 'if x <= foo, else if x <= faa' statments elegant..
var a = 23;
switch (true){
case a <= 25:
//do foo
break;
case a <= 30:
// do faa
break;
default:
//
// by Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
// MIT Style License
// see also: http://wiki.ecmascript.org/doku.php?id=strawman:iterators
//
// ---------------------------------------
// 1. Iteration via for-in loop
// ---------------------------------------
// this is the code available in the page
var DEBUG_LOG = (function () {
var timer = null, timeout = 5000;
var head;
return {
add: function (item) {
head = { value: item, next: head };
if (timer) {
clearTimeout(timer);
// Based on Angus Croll's http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
// Also available at http://jsfiddle.net/7xAqT/
Object.toType = (function() {
var global = this,
toString = Object.prototype.toString;
return function(obj) {
if (obj === global) {
return "Global";
}
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src =
('//' + /(www|apps)\.([\w\.]+\.)facebook\.com/.exec(document.referrer)
? RegExp.$2 + '.facebook.com/assets.php/en_US/'
: 'connect.facebook.net/en_US/') +
// the module to load
var mylib = (function() {
var api = {
provide: function(name, fn) {
this[name] = fn;
}
};
...
api.provide('foo', function() {
...
});
@oyvindkinsey
oyvindkinsey / README.md
Created November 20, 2012 22:27
Core implementation needed for ES5 to ES3 transforms using 'jspatch'

Core implementation needed for ES5 to ES3 transforms using 'jspatch'

  1. Install jsgrep
  2. Convert your source code using node spatch-cli.js es5-to-es3.spatch source.js // yields the converted source
  3. Prepare the implementation of ES5 by implemeting the missing polyfills in es5.js
  4. Concat es5.js and the converted code
  5. Wrap the result of the previous steps in a closure and export your object
var input = 'ABbCcc\n\
Good luck in the Facebook Hacker Cup this year!\n\
Ignore punctuation, please :\n)\
Sometimes test cases are hard to make up\n.\
So I just go consult Professor Dalves';
input.split('\n').forEach(function(line) {
console.log(
line,