Skip to content

Instantly share code, notes, and snippets.

Avatar

Øyvind Sean Kinsey oyvindkinsey

View GitHub Profile
View array_permutate.js
function array_permutate(set /*, additionalSets*/ ) {
var additionalSets = Array.prototype.slice.call(arguments, 1);
if (additionalSets.length === 0) {
return set.map(function (item) { return [item]; });
}
var permutations = array_permutate.apply(null, additionalSets);
var result = [];
for (var i = 0; i < set.length; i++) {
for (var j = 0; j < permutations.length; j++) {
result.push([set[i]].concat(permutations[j]));
View problem.js
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,
@oyvindkinsey
oyvindkinsey / README.md
Created Nov 20, 2012
Core implementation needed for ES5 to ES3 transforms using 'jspatch'
View README.md

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
View sample1.js
var mylib = (function() {
var api = {
provide: function(name, fn) {
this[name] = fn;
}
};
...
api.provide('foo', function() {
...
});
View server param.js
// 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
View toType.js
// 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";
}
View stacktrace.js
// 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);
View Common iterators of ES6.js
//
// 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
// ---------------------------------------
@oyvindkinsey
oyvindkinsey / Weird switch statement.js
Created Jan 19, 2011
How to make 'if x <= foo, else if x <= faa' statments elegant..
View Weird switch statement.js
var a = 23;
switch (true){
case a <= 25:
//do foo
break;
case a <= 30:
// do faa
break;
default: