Skip to content

Instantly share code, notes, and snippets.

@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);
};
}());
function generateOfficeWebAppURL(url) {
return "http://view.officeapps.live.com/op/view.aspx?src=" + encodeURIComponent(url);
}
/*
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);
@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);
}
@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 });
}
});
var shadowWrap = (function() {
var createShadowRoot = Element.prototype.createShadowRoot || Element.prototype.webkitCreateShadowRoot;
function createWrapper(content) {
var wrapper = document.createElement("shadow-wrap");
var shadowRoot = createShadowRoot.call(wrapper);
wrapper.resetStyleInheritance = true;
switch (typeof content) {
case "string":
@robozevel
robozevel / encodeImage.js
Created January 21, 2014 11:25
Encode images to base64 using FileReader
var encodeImage = (function() {
var DEFAULT_IMAGE = 'data:image/png;base64,',
prefix = "_",
cache = {};
function encode(url, callback) {
var key = prefix + url, value = cache[key];
if (value) {
callback(value);
} else {
@robozevel
robozevel / ES5 Object.extend.js
Last active December 24, 2015 07:29 — forked from livingston/ES5 Extend & Clone Objects.js
ES5 Object.extend
Object.defineProperty(Object, "extend", {
enumerable: false,
value: function extend(obj) {
Array.prototype.slice.call(arguments, 1).forEach(function(current) {
Object.getOwnPropertyNames(current).forEach(function(key) {
Object.defineProperty(obj, key, Object.getOwnPropertyDescriptor(current, key));
});
});
return obj;
}
@robozevel
robozevel / formatNumber.js
Created September 18, 2013 10:04
Unnecessarily clever number formatting function
// 1234567890 => "1,234,567,890"
function formatNumber(n) {
var a = [], s = String(n), i = l = s.length;
while (i--) a.push(s[i]), i && (l - i) % 3 === 0 && a.push(",");
return a.reverse().join("");
}
@robozevel
robozevel / deparam.js
Last active August 19, 2016 08:40 — forked from emilisto/deparam.js
// deparam
//
// Inverse of $.param()
//
// Taken from jquery-bbq by Ben Alman
// https://github.com/cowboy/jquery-bbq/blob/master/jquery.ba-bbq.js
var isArray = Array.isArray || function(obj) {
return Object.prototype.toString.call(obj) == '[object Array]';
};