Skip to content

Instantly share code, notes, and snippets.

@izb
izb / isnode
Created May 15, 2013 10:12
Is this code running on node.js?
var isnode = (typeof module !== 'undefined' && module.exports);
@izb
izb / New AMD class
Created May 15, 2013 10:18
New AMD Class module
/*global define*/
define(function() {
'use strict';
/**
* @module my/amd/path
*/
/**
@izb
izb / JSDoc patterns
Created May 15, 2013 10:43
JSDoc common doc patterns
{
/**
* module prologue comment
*
* @module my/amd/path
*
* if private
* @private
*/
@izb
izb / pager
Created August 1, 2013 08:39
Pager control
/*
Call pagerList with the page number and count.
Alter pageLink to modify page link HTML.
Alter the name, or remove the page link function that gets placed into the
global namespace. Alternatively, implement its behaviour.
@izb
izb / string-format1
Created August 12, 2013 09:46
String helpers
// String format function
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
@izb
izb / asyncForEach
Created August 12, 2013 10:33
Loop over an array in order, processing elements with an asynchronous function.
function asyncForEach(array, cb) {
var queue = array.slice();
var out = [];
function next() {
if (queue.length > 0) {
var item = queue.shift();
someAsyncFunc(item, function(o) {
out.push(o);
next();
});
@izb
izb / clamp
Created August 12, 2013 10:35
Clamp value to a range
//clamp(x,a,b) will return the value in the range [a,b] that's closest to x. With just two arguments, clip(x,a) will use the range [-a,a].
function clamp(x,a,b){return b===undefined?x>a?a:x<-a?-a:x:x>b?b:x<a?a:x}
@izb
izb / queryString
Created August 12, 2013 10:38
Convert object to/from query string
function objqstring (obj) {
var a = []
for (var s in obj) {
a.push(s + "=" + encodeURIComponent(JSON.stringify(obj[s])))
}
return a.join("&")
}
function qstringobj (qstring) {
var obj = {}
@izb
izb / cookies
Created August 12, 2013 10:39
Object view on cookie values.
var cookies = {}
document.cookie.split(";").forEach(function (kv) {
kv = kv.replace(/^\s\s*/, "").split("=")
cookies[kv[0]] = decodeURIComponent(kv[1])
})
@izb
izb / canvas-screenshot
Created August 12, 2013 10:39
canvas screenshot in pop-up window
window.open(canvas.toDataURL())