Skip to content

Instantly share code, notes, and snippets.

@thurt
thurt / sublime_keybinding_gotoNextEmptyLine.JSON
Last active August 29, 2015 14:28
Keyboard shortcut for go to next empty line in Sublime Text. Add to Preferences > Key Bindings -- User
[
{"keys": ["alt+up"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": false}},
{"keys": ["alt+down"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": true}},
{"keys": ["shift+alt+up"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": false, "extend": true}},
{"keys": ["shift+alt+down"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": true, "extend": true}},
]
@thurt
thurt / require.js
Last active December 18, 2015 19:10
A non-blocking require function which dynamically loads javascript files synchronously into the DOM
function require(scripts, callback) {
var i, xhr, head, events = {
onload: function(e) {
var tag = document.createElement('script')
tag.textContent = xhr.responseText
head.appendChild(tag)
next()
},
onerror: function(e, message) {
console.error(`require script ${i+1} of ${scripts.length}:
@thurt
thurt / nodemodule.js
Last active January 21, 2016 03:10
node/browser capable module check
(function() {
var Validator = (function() {
var Validator = function(options) {
...
};
Validator.prototype.foo = function foo() {
...
};
////////////////////////////
// using inline closure
////////////////////////////
(function() {
arr = []
for (i = 0; i < 10; i++)
arr[i] = (function(i) {
return function() { console.log("I am #" + i) }
})(i)
@thurt
thurt / uncurrying.js
Created January 11, 2016 17:22
uncurrying() in JavaScript.
Function.prototype.uncurrying = function() {
var _this = this;
return function() {
return Function.prototype.call.apply(_this, arguments);
};
};
/*var push = Array.prototype.push.uncurrying();
var a = [];
push(a, 1, 2, 'zensh');
@thurt
thurt / reload.js
Last active January 17, 2016 19:30 — forked from gleitz/reload.js
Reloading modules from the repl in Node.js
// Reloading modules from the repl in Node.js
// Benjamin Gleitzman ([email protected])
//
// Inspired by Ben Barkay
// http://stackoverflow.com/a/14801711/305414
//
// Usage: `node reload.js`
// You can load the module as usual
// var mymodule = require('./mymodule')
// And the reload it when needed
@thurt
thurt / use_strict.html
Created January 18, 2016 16:53
Examples of 'use strict' in the browser
<!DOCTYPE html>
<html>
<head>
<script>
'use strict'
// this 'use strict' only applies within this <script> tag
</script>
<script>
@thurt
thurt / if_else_vs_try_catch.js
Last active January 25, 2016 17:25
Performance of if else vs try catch
function if_else() {
if (window.foo) {
window.foo.bar = 'never gets here because we explicitly checked for window.foo first'
} else {}
}
// this will always be slower because it creates an exception
// and then navigates that exception to the first catch block
function try_catch() {
try {
@thurt
thurt / data_warehouse.js
Created February 10, 2016 15:58
Simple Data Warehouse
var Data = function() {
var warehouse = {};
var count = 1;
return {
reset: function() {
count = 1;
warehouse = {};
},
set: function (dom, data) {
if (!dom.__data) {
@thurt
thurt / hoist-var-let-const.js
Last active February 11, 2016 22:23
Hoist Tests comparing var let and const
/*
var is function scoped & hoisted
let is block scoped & not hoisted
const is block scoped & not hoisted
*/
(() => {
'use strict'
const tests = [
() => {