Skip to content

Instantly share code, notes, and snippets.

@robozevel
robozevel / underscore.cacheFor.js
Created February 2, 2014 20:19
Execute function not more than once every X milliseconds, otherwise return cached result.
_.mixin({
cacheFor: function(wait, fn) {
return _.throttle(fn, wait, { trailing: false });
}
});
@robozevel
robozevel / isUsingImperialSystem.js
Created February 2, 2014 20:20
Countries using the Fahrenheit scale: USA, Puerto Rico, Jamaica, Bahamas, Guam, Belize, Virgin Islands (US), Cayman Islands and Palau. (http://en.wikipedia.org/wiki/Fahrenheit)
function isUsingImperialSystem(countryCode) {
return /US|PR|JM|BS|GU|BZ|VI|KY|PW/.test(countryCode);
}
/*
Kilo.js
#toSize: 2048 => "2 KB"
#toBytes: "2 KB" => 2048
*/
var Kilo = (function() {
"use strict";
var kilo = 1024;
var sizes = { "B": 0, "KB": 1, "MB": 2, "GB": 3, "TB": 4 };
var sizesNames = Object.keys(sizes);
function generateOfficeWebAppURL(url) {
return "http://view.officeapps.live.com/op/view.aspx?src=" + encodeURIComponent(url);
}
@robozevel
robozevel / isHTMLString.js
Created March 16, 2014 16:18
Detect HTML strings (extracted from jQuery's core)
// https://github.com/jquery/jquery/blob/master/src/core/init.js#L14
var isHTMLString = (function() {
var rHTML = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/;
return function(str) {
return rHTML.test(str);
};
}());
@robozevel
robozevel / typehead.json
Created March 18, 2014 09:23
FQL Typehead
[
{
"uid": "keywordSELECT",
"subtext": "[FQL keyword]",
"text": "SELECT"
},
{
"uid": "keywordFROM",
"subtext": "[FQL keyword]",
"text": "FROM"
@robozevel
robozevel / esc.html
Created March 23, 2014 08:50
Stop propagation of the ESC key event when closing a file selection dialog
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<label for="uploadFile">UPLOAD</label>
<input type="file" id="uploadFile"/>
<script>
$("input[type='file']").on("keyup", function(e) {
if (e.keyCode === 27) this.blur(), e.stopPropagation();
});
$(document).on("keyup", function(e) {
@robozevel
robozevel / geolocate.js
Created March 27, 2014 16:57
Geolocate using Wikimedia's Geo IP Lookup service
var geolocate = function(callback) {
callback = typeof callback === "function" ? callback : function(){};
if ('Geo' in window) {
callback(window.Geo);
} else {
var script = document.createElement('script');
script.src = "https://geoiplookup.wikimedia.org/";
script.onload = function() { callback(window.Geo) };
document.head.appendChild(script);
}
@robozevel
robozevel / underscore.whenOnline.js
Created March 30, 2014 09:16
Ensure a function is invoked only when online
_.mixin({
whenOnline: function(fn, context) {
return function() {
var args = arguments;
context = context || this;
if (navigator.onLine) {
fn.apply(context, args);
} else {
window.addEventListener('online', function online() {
fn.apply(context, args);
@robozevel
robozevel / escapeSqlString.js
Created April 2, 2014 13:18
Escape SQL string (extracted from node-mysql)
// https://github.com/felixge/node-mysql/blob/master/lib/protocol/SqlString.js
function escapeSqlString(text) {
return text.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function(s) {
switch(s) {
case "\0" : return "\\0";
case "\n" : return "\\n";
case "\r" : return "\\r";
case "\b" : return "\\b";
case "\t" : return "\\t";
case "\x1a": return "\\Z";