Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrew Chilton chilts

View GitHub Profile
@chilts
chilts / gist:1141448
Created Aug 12, 2011 — forked from Gurpartap/gist:1141421
Generate unique alphanumeric incrementing string keys
View gist:1141448
#!/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 ) {
View gist:1144739
package main
import (
"strconv"
"http"
"io"
"log"
"memcache"
)
@chilts
chilts / gist:1158499
Created Aug 20, 2011
Instead of passing 'r' and calling r.Body.Close() in some other function, just use 'defer'
View gist:1158499
// 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 Aug 22, 2011
Program to take a fully formed URL and print out the contents
View gist:1163510
package main
import (
"flag"
"os"
"http"
"fmt"
"io/ioutil"
)
View test-substring-v-index.js
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 Jan 9, 2012
middleware which needs access to 'app'
View test.js
// middleware which needs access to 'app'
app.use(function(req, res, next) {
myOtherFunctionWhichNeedsApp(app, req, res, next);
});
@chilts
chilts / test.js
Created Jan 11, 2012
Weird data structure into an object
View test.js
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 Jan 16, 2012
The application itself
View .gitignore
node_modules
*.log
*~
@chilts
chilts / gist:2416572
Created Apr 18, 2012
Using https://github.com/hazlema/'s upload.js for a streaming file
View gist:2416572
# 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 Apr 27, 2012
All the Streams!
View gist:2505347
// instead of ...
var writestream = new stream.Stream()
writestream.writable = true
var readstream = new stream.Stream()
readstream.readable = true
// wish we could do