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 / 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";
var isValidUrl = (function() {
var protocols = ["http", "https", "ftp"];
var requireProtocol = false;
var requireTLD = true;
var pattern = new RegExp('^(?!mailto:)(?:(?:' + protocols.join('|') + ')://)' + (requireProtocol ? '' : '?') + '(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))' + (requireTLD ? '' : '?') + ')|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$', 'i');
return function(str) {
return (typeof str === "string") && str.length < 2083 && pattern.test(str);
}
}());
location.search.substr(1).split("&").reduce(function(o, pair) {
pair = pair.split("=");
o[pair[0]] = pair[1] && decodeURIComponent(pair[1]);
return o;
}, {});