This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Only instantiate once and append the calls in your loop | |
// Because XHR responses pop out of the stack, all of the | |
// requests will be linked and sent before the first can | |
// return. | |
// Also, when passing the settings object as a config, | |
// the callback is inside the object. The signatures for | |
// sexy methods are (url, callback) or (settings) | |
// Can configure globally for the Sexy instance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function truncate (str, n) { | |
return (str=str||'').length<n?str:(new RegExp('^(.{0,'+(n-1)+'}\\S)(\\s|$)')).exec(str)[1]+'...'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function truncate (str, n) { | |
return (str=str||'').length<n?str:(new RegExp('^(.{0,'+(n-1)+'}\\S)(\\s|$)')).exec(str)[1]+'...'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pure JS | |
function shuffle(array) { | |
return array.sort(function(){ | |
return .5 - Math.random(); | |
}); | |
} | |
// with Prototype.js you can do | |
function shuffle(array){ | |
return array.sortBy(Math.random); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function repeat(c,n,a){return(a=[]).join(a[n-1]=c);} | |
// usage | |
repeat('x', 3); // returns "xxx" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function work (arr, size, fn, callback) { | |
var i = 0, n, ret = []; | |
setTimeout(function(){ | |
for (n = Math.min(i + size, arr.length); i < n; ++i) { | |
ret.push(fn(i, arr[i])); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function GoodNews (headline, author) { | |
if (!(this instanceof GoodNews)) { | |
return new GoodNews(headline, author); | |
} | |
this.headline = headline; | |
this.author = author; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Detection and simple fix for broken Array.prototype.sort in Chrome */ | |
// var arraySortIsBroken = !![{a:0,b:1},{a:1,b:1}].sort(function(a,b){return a.b-b.b})[0].a; | |
var arraySortIsBroken = ![1,0].sort(function(){return 0})[0]; | |
var arraySortMaintainsIndex = ![0,1].sort(function(){return 0})[0]; | |
function fixArraySort (items, prop) { | |
var i, n, item, idx = '__' + prop + (new Date()).getTime(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* jQuery.getImg */ | |
$.getImg = function (src, callback, errback) { | |
var offscreen = $('<div/>').css({ display:'none' }).appendTo('body'); | |
$('<img/>') | |
.attr('src', src) | |
.bind('load error', function (e) { | |
var fn = e.type === 'load' ? callback : errback, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sexy | |
// Authenticate user | |
.json({ | |
type: 'POST', | |
url: 'login', | |
data: { username: 'furf', password: 's3xy' } | |
}) | |
// GET article | |
.json('article/1', true, function (article, previous, nextCfg) { | |
article.author = 'furf'; // modify article |