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
@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 / 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 / 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 / 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 / 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 / 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 / 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 () {
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 / virtual-hosts-connect.js
Created July 13, 2011 01:08
run multiple hosts (domains) on a single node process
var connect = require('connect')
, app = require('./app')
, app2 = require('./app2')
connect(
connect.vhost('dustindiaz.com', app)
, connect.vhost('foobar.dustindiaz.com', app2)
).listen(3000)
@ded
ded / parallel.js
Created July 21, 2011 01:10
call multiple async methods in parallel and receive the result in a callback
function parallel() {
var args = Array.apply(null, arguments)
, callback = args.pop()
, returns = []
, len = 0
args.forEach(function (el, i) {
el(function () {
var a = Array.apply(null, arguments)
, e = a.shift()
if (e) return callback(e)