Skip to content

Instantly share code, notes, and snippets.

@naholyr
naholyr / examples-and-issues.js
Created January 22, 2012 18:08
Writing a "url_for" helper for Express
// What name can I refer to ?
app.get('/hello-world', function () { console.log(3); });
// OK, suppose I can build the path from params
app.get('/:id/:name', function hello1 () { console.log(1); });
// OK, suppose the "callback" is the last one
app.get('/hello/:name', requiresAuthentication, function hello2 () { console.log(2); });
// How do I build the path ?
@naholyr
naholyr / backend.js
Created January 23, 2012 18:28
GeoChatHTML5
var io = require('socket.io').listen(8123);
io.sockets.on('connection', function (socket) {
socket.on('username', function (username) {
this.set('username', username, function () {
socket.broadcast.emit('newcomer', username);
socket.emit('ready', username);
});
});
@naholyr
naholyr / gist:1682312
Created January 26, 2012 11:21 — forked from mhawksey/TAGSStats.R
Read edge list from TAGS Spreadsheet calculate SNA data and POST back
library(igraph)
library(reshape)
library(plyr)
require(RJSONIO)
require(RCurl)
# Google Spreadsheet key (must be published to the web)
key='0AqGkLMU9sHmLdGNYZDVyTWl1ZmtNbmFzWGlpUkt1Tmc'
# Sheet gid name
gid=105
@naholyr
naholyr / svn-branch.sh
Created February 1, 2012 15:51
Branching with SVN
# Retrieve repository URL
function svn-url() {
LANG=en_US svn info "$1" | grep '^URL *:' | sed 's/^URL *: //'
}
# Branching
function svn-branch() {
if [ ! -d .svn ]; then return 1; fi
if svn-url | grep '/trunk' &> /dev/null; then
echo trunk
@naholyr
naholyr / count-char-ranges.js
Created February 6, 2012 19:57
String.prototype.count
function str_char_code(c) {
return c.charCodeAt ? c.charCodeAt(0) : parseInt(c, 10);
}
function cb_between(min, max) {
return function (v) {
return v >= min && v <= max;
};
}
[
{ "keys": ["ctrl+k", "ctrl+i"], "command": "gist" },
{ "keys": ["ctrl+k", "ctrl+l"], "command": "gist_private" },
{ "keys": ["ctrl+k", "ctrl+s"], "command": "gist_update_file" },
{ "keys": ["ctrl+shift+g"], "command": "gist_list" }
]
@naholyr
naholyr / git-ignored.sh
Created February 15, 2012 15:41
List all ignored files
# I didn't look, but I'm even pretty sure there is a dedicated command for this with Git.
# and this is just a piece of cake, 5 minutes max to implement this...
# Edit - actually, you can simply call this:
# git ls-files --others --ignored --exclude-standard
find -name .gitignore | while read f
do
d="$(dirname "$f")"
cat $f | while read p
@naholyr
naholyr / function-argnames.js
Created February 19, 2012 18:30
JS Introspection: extract function parameter names
Function.prototype.argNames = function () {
// Extract function string representation: hopefully we can count on it ?
var s = this.toString();
// The cool thing is: this can only be a syntactically valid function declaration
s = s // "function name (a, b, c) { body }"
.substring( // "a, b, c"
s.indexOf('(')+1, // ----------------^
s.indexOf(')') // ------^
);
@naholyr
naholyr / compare.php
Created February 22, 2012 16:27
Extract namespace from a PHP file
<?php
// Works in every situations
function by_token ($src) {
$tokens = token_get_all($src);
$count = count($tokens);
$i = 0;
$namespace = '';
$namespace_ok = false;
while ($i < $count) {
@naholyr
naholyr / untitled.php
Created March 9, 2012 11:31
cache template
function foo_uncached($args)
{
$result = big_calculations();
return $result;
}