Skip to content

Instantly share code, notes, and snippets.

View jchris's full-sized avatar

Chris Anderson jchris

View GitHub Profile
@jchris
jchris / jquery.couchLogin.js
Created May 9, 2011 22:48
jQuery plugin for CouchDB login/signup/logout in 80 lines (2kb minified)
// Copyright Chris Anderson 2011
// Apache 2.0 License
// jquery.couchLogin.js requires jquery.couch.js
//
// Example Usage (loggedIn and loggedOut callbacks are optional):
// $("#mylogindiv").couchLogin({
// loggedIn : function(userCtx) {
// alert("hello "+userCtx.name);
// },
// loggedOut : function() {
@jchris
jchris / index.html
Created April 27, 2011 16:54
jQuery CouchDB Hello World - to use this script, create the map view in futon and click Save As, then upload index.html as an attachment to _design document created with the view. browse to index.html to run the app.
<!DOCTYPE html>
<html>
<head><title>Tiny CouchApp</title></head>
<body>
<h1>Tiny CouchApp</h1>
<form id="new_message">
<label for="message">Message:</label>
<input type="text" name="message" value="">
<p><input type="submit" value="Save &rarr;"></p>
</form>
@jchris
jchris / learnifthen.js
Created December 20, 2010 21:17
teaching amysue javascript
rainPain = function(temp, cip) {
if (temp < 39) {
console.log ("too cold!")
} else if (temp<80 && cip){
console.log("ouch")
} else {
console.log("time to walk the dogs")
}
}
rainPain(75, true)
@jchris
jchris / couchapp.html
Created November 27, 2010 15:14
the smallest possible CouchApp
<!DOCTYPE html>
<html>
<!-- This should be the simplest possible jQuery based CouchApp. Install by uploading to a design document in Futon or putting in _attachments/index.html and running `couchapp push` -->
<head><title>Tiny CouchApp</title></head>
<body>
<h1>Tiny CouchApp</h1>
<ul id="databases"></ul>
</body>
<script src="/_utils/script/jquery.js"></script>
<script src="/_utils/script/jquery.couch.js"></script>
@jchris
jchris / reduce.js
Created November 26, 2010 04:41
unique key count reduce
function(ks, vs, rr) {
// values are discarded by reduce
// todo: configurable key function, for complex queries
var fk, lk;
function uniqCount(keys) {
var count = 0, obj = {};
for (var i=0; i < keys.length; i++) {
if (!obj[keys[i]]) {
count++;
obj[keys[i]] = true;
require 'rubygems'
require 'garb'
# couchrest is only required because it makes the dates to_json into
# something javascript can parse.
require 'couchrest'
class OpenStruct
def to_json
table.to_json
end
@jchris
jchris / topN.js
Created October 31, 2010 15:03
a CouchDB list function for returning the top Terms in where the key is like [Term|_Rest]
function(seq, req) {
var min = 0, term, count, row
, i, top = parseInt(req.query.top), match = [];
while (row = getRow()) {
term = row.key[0];
count = row.value;
if (match.length < top) {
match.push([term, count]);
} else {
if (count > min) {
require 'digest/md5'
# usage: run this in the root directory of your iTunes Music folder, or wherever, and pipe the output to a file
# next, pipe the output of that file through `sort` to a new file
# now, use the next script on that file
ls = Dir['**/*']
ls.each_with_index do |f, i|
STDERR.puts ls.length - i if (i % 100 == 0)
// Copyright J. Chris Anderson 2007
// Retain this notice.
// Released under the LGPL 2.1
// http://creativecommons.org/licenses/LGPL/2.1/
XSPF = {
XMLfromString: function(string) {
if (window.ActiveXObject) {
var doc = new ActiveXObject("Microsoft.XMLDOM");
doc.async = "false";
require 'lib/yajl/http_stream'
require 'uri'
uri = URI.parse('http://jchrisa.net/toast/_changes')
Yajl::HttpStream.get(uri) do |hash|
# will take a few seconds, since the response is a single ~4MB JSON string
# if the response was a bunch of individual JSON strings, this would be fired
# for every string parsed off the stream, as they were parsed
puts hash.inspect
end