Skip to content

Instantly share code, notes, and snippets.

View furf's full-sized avatar
🎯
Focusing

Dave Furfero furf

🎯
Focusing
View GitHub Profile
// 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
function truncate (str, n) {
return (str=str||'').length<n?str:(new RegExp('^(.{0,'+(n-1)+'}\\S)(\\s|$)')).exec(str)[1]+'...';
}
function truncate (str, n) {
return (str=str||'').length<n?str:(new RegExp('^(.{0,'+(n-1)+'}\\S)(\\s|$)')).exec(str)[1]+'...';
}
// 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);
function repeat(c,n,a){return(a=[]).join(a[n-1]=c);}
// usage
repeat('x', 3); // returns "xxx"
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]));
}
function GoodNews (headline, author) {
if (!(this instanceof GoodNews)) {
return new GoodNews(headline, author);
}
this.headline = headline;
this.author = author;
}
/* 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();
/* 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,
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