Skip to content

Instantly share code, notes, and snippets.

View garbados's full-sized avatar

DFB garbados

View GitHub Profile
@garbados
garbados / gist:6105137
Created July 29, 2013 15:26
example of using commonjs to separate transformation logic from the map function itself
{
views: {
gender: {
map: function(doc){
var lib = require('/views/lib/transform')
, key_value = lib.gender(doc);
emit(key_value[0], key_value[1]);
}
},
@garbados
garbados / custom.js
Last active January 25, 2018 07:58
Sample CouchDB map functions for working with dates without a library.
function(doc){
// handle a custom format using methods in the date object
var format = "{getFullYear}/{getMonth}/{getDate}",
date = new Date(doc.created_at),
// use a regular expression to replace parts of our format string
formatted_date = format.replace(/\{(\w+)\}/g, function(str, func){
var time = date[func]();
// if `func` is `getMonth` we must offset by one
if(func === 'getMonth'){
time += 1;
@garbados
garbados / list-format.js
Last active December 21, 2015 23:29
Example map function with sample output and list function.
function(head, req){
var i,
x,
results = {},
headers = {},
csv = [];
while(x = getRow()){
(function(row){
var result = results[JSON.stringify(row.key)] || {};
for(i in row.value){
@garbados
garbados / couchdb-tunnel.pl
Created September 7, 2013 21:17
Copy of the Linode script for creating an SSH tunnel to a CouchDB instance on a Linode server. Source: https://library.linode.com/databases/couchdb/ssh-tunnel#sph_create-a-tunnel-with-couchdb-tunnel-on-mac-os-x-or-linux
#!/usr/bin/perl
# CouchDB Tunnel Tool for MacOS X and Linux
# Copyright (c) 2010 Linode, LLC
# Author: Philip C. Paradis <[email protected]>
# Modifications: Sam Kleinman <[email protected]>
# Usage: couchdb-tunnel.pl [start|stop]
# Access a CouchDB database instance by way of an SSH tunnel.
##
@garbados
garbados / app.js
Last active December 24, 2015 00:49
Very basic [node.couchapp.js](https://github.com/mikeal/node.couchapp.js) config files
var couchapp = require('couchapp'),
path = require('path');
ddoc = {
_id: '_design/app',
rewrites: [{
from: '',
to: '/index.html'
},{
from: '*',
@garbados
garbados / relations.js
Last active December 24, 2015 16:19
Example of entity relations in a Cloudant design document.
{
// name of the design doc
_id: '_design/relations',
views: {
lib: couchapp.loadFiles(__dirname + '/../lib'),
// collate entity relations using
collate: {
map: function (doc) {
var deref = require('views/lib/index').deref,
relation = deref[doc.type];
@garbados
garbados / noblizer.py
Created October 10, 2013 19:32
A Python script to generate pithy witticisms, Brad Noble's hallmark.
import random
prefix = [
'do',
'grow',
'edge',
'next',
'build',
'sleep',
'play',
/*
* Given array `records` and function `fn`
* returns an array where
* any records with duplicate `fn(record)` values
* are omitted.
*
* Runs in O(n) where n is records.length
*/
function remove_duplicates (records, fn) {
var uniques = {},
@garbados
garbados / gist:8204632
Last active January 1, 2016 21:39
Using `jq` to pipe couchdb contents into `dat`
# _all_docs
curl http://localhost:5984/eggchair/_all_docs?include_docs=true | jq -c ".rows[] | .doc" | dat --json
# _changes
curl http://localhost:5984/eggchair/_changes?include_docs=true | jq -c ".results[] | .doc" | dat --json
@garbados
garbados / warrior.rb
Last active January 2, 2016 10:59
Solution to every level of RubyWarrior (https://www.bloc.io/ruby-warrior/#/). Heavily borrowed from https://gist.github.com/jnf/6198927
# N.B. doesn't get maximum score:
# * won't get captives behind the warrior
# * will shoot captives at look[1] if an enemy is in look[2]
class Player
def play_turn(warrior)
@warrior = warrior
## uncomment `@max_health` and `@prev_health` on level 3
# @max_health ||= warrior.health # set to `1` to complete level 8 in 27 turns
# @prev_health ||= @max_health