Skip to content

Instantly share code, notes, and snippets.

View ded's full-sized avatar
🏃
always working, sometimes running

Dustin Diaz ded

🏃
always working, sometimes running
View GitHub Profile
test_script_elem = document.createElement("script"),
explicit_preloading = typeof test_script_elem.preload == "boolean",
real_preloading = explicit_preloading || (test_script_elem.readyState && test_script_elem.readyState == "uninitialized"),
script = document.createElement('script')
if (real_preloading) {
registry_item.elem = script;
if (explicit_preloading) {
@ded
ded / app.html
Created June 29, 2011 04:09
ender CLI example
<script src="ender.min.js"></script>
<script>
$.require('/js/core.min.js', 'core')
$.ready('core', function () {
$(document).ready(function () {
$('<p>hello world</p>').appendTo('body')
.bind('click', function (e) {
$.require('/js/ajax.min.js', function () {
@ded
ded / rtltr.js
Created June 15, 2011 07:27
rtltr bookmarklet
!function () {
var s = document.createElement('script');
s.onload = function () {
document.body.dir = 'rtl';
!function ($) {
var links = $('link[rel="stylesheet"]');
links.forEach(function (link) {
var href = link.href;
link.href = 'http://dustindiaz.com/r2?url=' + href;
});
@ded
ded / gist:975897
Created May 17, 2011 03:39
command line prompt - bash_profile
function prompt {
local WHITE="\[\033[1;37m\]"
local GREEN="\[\033[0;32m\]"
local CYAN="\[\033[0;36m\]"
local GRAY="\[\033[0;37m\]"
local BLUE="\[\033[0;34m\]"
local BROWN="\[\033[0;33m\]"
# export PS1="${GREEN}\u${CYAN}@ ${CYAN}\w${GRAY} "
export PS1="${GREEN}\u${CYAN}\w @${BROWN} \`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (\
.+)$/, '(\1) ')\"\`${WHITE}"
@ded
ded / tween.js
Created May 13, 2011 06:07
generic time-based tween with easing support
!function ($) {
function tween(duration, from, to, tween, ease) {
ease = ease || function (t) {
return t;
}
var self = this,
time = duration || 1000,
animDiff = to - from,
startTime = new Date(),
@ded
ded / location.js
Created April 13, 2011 02:52
parse the parts from a known URL
// the point of this is to create a similar api to window.location from a known URL
// usage
/*
var parts = new Location('http://twitter.com/path/to/?q=foo=bar&baz=1#hashbang');
console.log(parts);
*/
function Location(url) {
if (url.match(/^\/\//)) {
@ded
ded / input.txt
Created March 22, 2011 06:39
use this as input
<a href="http://en.wikipedia.org/wiki/Autocomplete">Autocomplete widgets</a> live in nearly all systems that requires filtering items via input against large amounts of data. This includes address books, email contacts, restaurants, even social graphs.
However, in most matching algorithms, Engineers don't take into account that people don't know how to spell AND/OR are lazy. Thus here is a <em>really simple</em> solution to work around this problem, and in my own opinion, will vastly improve the user experience.
<h3>Look ahead matching</h3>
Let's say you have five people. Daniel, Dustin, David, Damarcus, and Russ. Now let's say a user types in <em>dus</em>. We would match <b>Dus</b>tin and <b>D</b>amarc<b>us</b>. Likewise, if we typed in <em>us</em>, we would get an output of D<b>us</b>tin, Damarc<b>us</b>, and R<b>us</b>s.
<h3>Enter RegExp</h3>
At this point, it's sort of a no-brainer. Your input-based regular expression can be created as such:
<pre><code>var people = ['Daniel', 'Dustin', 'David', 'Damarcu
@ded
ded / text-to-html.js
Created March 21, 2011 21:32
works for me
function convert(text) {
var tokens = text.split('\n'), i, token, src = [];
for (var i=0; i < tokens.length; i++) {
var token = tokens[i];
token && token.charAt(0).match(/\w/) ? (function () {
src.push('<p>' + token + '</p>');
}()) : (function () {
token && token.match(/^\s*<(a|b|i|strong|em|cite)([ \=\'\"\w\-\/\.\:]+)*>/i) ?
(function() {
@ded
ded / gist:877800
Created March 19, 2011 21:04
there's probably a better way to do this
/* replace faux text (sometimes html) into html with paragraphs - or whatever that thing wordpress does to their posts */
function paragraphs(story) {
story = story.replace(/<pre>([\s\S]*)<\/pre>/g, function (m, content) {
return '<pre>' + content.replace(/\n/g, 'iiiiii') + '</pre>';
});
return _(story.split('\n')).compact().map(function(par, i) {
return par == '' || par.match(/^(<p(?:re)?>|\s+)/) ? par : '<p>' + par + '</p>';
}).join('\n').replace(/<p><\/p>/g, '').replace(/(i{6})/g, '\n');
}
@ded
ded / supplant.js
Created March 19, 2011 19:05
super basic templater
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g, function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
});
};