Skip to content

Instantly share code, notes, and snippets.

View chilts's full-sized avatar

Andrew Chilton chilts

View GitHub Profile
@chilts
chilts / gist:1141448
Created August 12, 2011 04:15 — forked from Gurpartap/gist:1141421
Generate unique alphanumeric incrementing string keys
#!/usr/bin/env node
var base_str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
console.log( number_to_base(process.argv[2], base_str) );
function number_to_base(n, str) {
if ( n === 0 ) return "0";
var b = '';
while ( n > 0 ) {
package main
import (
"strconv"
"http"
"io"
"log"
"memcache"
)
@chilts
chilts / gist:1158499
Created August 20, 2011 02:05
Instead of passing 'r' and calling r.Body.Close() in some other function, just use 'defer'
// Current Code
r, err := http.DefaultClient.Do(&req)
if err != nil {
return nil, err
}
function_dowork_that_also_calls_r_body_close(r)
// My Thoughts
@chilts
chilts / gist:1163510
Created August 22, 2011 20:52
Program to take a fully formed URL and print out the contents
package main
import (
"flag"
"os"
"http"
"fmt"
"io/ioutil"
)
@chilts
chilts / test-substring-v-index.js
Created November 30, 2011 10:43
test-substring v index
var alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var alphabetLength = alphabet.length;
var i, rnd, trips = 1000000000;
var d1 = new Date();
console.log(d1);
for (i = 0; i < trips; i++) {
rnd = Math.floor(Math.random() * alphabetLength);
alphabet.substring(rnd, rnd + 1);
@chilts
chilts / test.js
Created January 9, 2012 23:34
middleware which needs access to 'app'
// middleware which needs access to 'app'
app.use(function(req, res, next) {
myOtherFunctionWhichNeedsApp(app, req, res, next);
});
@chilts
chilts / test.js
Created January 11, 2012 09:14
Weird data structure into an object
var str = "a:0,1,2,3|b:4,5,6|c:0,2";
var a = {};
str.split('|').forEach(function(v) {
var s = v.split(':');
a[s[0]] = s[1].split(',');
});
console.log(a);
@chilts
chilts / .gitignore
Created January 16, 2012 20:58
The application itself
node_modules
*.log
*~
@chilts
chilts / gist:2416572
Created April 18, 2012 21:07
Using https://github.com/hazlema/'s upload.js for a streaming file
# node amazon/s3/upload.js --bucket pie-18 --file README.md
Region: us-east-1
EndPoint: s3.amazonaws.com
AccessKeyId: [removed]
SecretAccessKey: [removed]
AwsAccountId: [removed]
Bucket: pie-18
Filename: README.md
File Size: 2644
@chilts
chilts / gist:2505347
Created April 27, 2012 03:04
All the Streams!
// instead of ...
var writestream = new stream.Stream()
writestream.writable = true
var readstream = new stream.Stream()
readstream.readable = true
// wish we could do