Skip to content

Instantly share code, notes, and snippets.

View NV's full-sized avatar

Nikita Vasilyev NV

View GitHub Profile
@NV
NV / location.params.js
Created October 19, 2009 17:11
Handy URL params access
/*
* Handy URL params access
* @example
* location.href = 'http://www.google.com/search?q=foo+bar&client=firefox'
* location.params == {q: 'foo+bar', client: 'firefox'}
*/
// JS 1.6
location.params = {};
location.search.substr(1).split('&').forEach(function(el){
function source_of (array, up_to_dimension) {
// Your implementation
}
source_of([1, [2, [3]]], 0) == '[1, ?]'
source_of([1, [2, [3]]], 1) == '[1, [2, ?]]'
source_of([1, [2, [3]]], 2) == '[1, [2, [3]]]'
@NV
NV / post-firewall
Created October 28, 2009 22:25
/usr/local/sbin/post-firewall
#!/bin/sh
# /usr/local/sbin/post-firewall
# $1 = vlan1
# $2 = 192.168.1.1
iptables -t nat -I PREROUTING 1 -p tcp -d "$2" --dport 80 -j DNAT --to "$2":8082
iptables -t nat -D PREROUTING -i "$1" -p tcp --dport 80 -j DROP
iptables -t nat -I PREROUTING 2 -i "$1" -p tcp --dport 8082 -j DROP
iptables -I INPUT 1 -i "$1" -d "$2" -p tcp --syn --dport 8082 -j ACCEPT
@NV
NV / exclude
Created October 29, 2009 11:00
Git global exclude file
# git config --global core.excludesfile /usr/local/git/exclude/exclude
# see http://groups.google.com/group/peepcode/browse_thread/thread/fe6f9c1fc9d6e725
# Rivals
.svn
.hg
# Archives
*.zip
*.gz
@NV
NV / events.js
Created October 30, 2009 02:18
reinventing JS events API
// JS Events API sucks. addEventListener ugly. If I'll design this API from scratch, I've done this:
window.events // {}
window.events.load // []
window.events.load.length // 0
window.events.load.push(function(e){
alert('window loaded')
})
window.events.load.length // 1
@NV
NV / String.prototype.between.js
Created November 18, 2009 13:05
JS string.between method
/**
* Example:
* 'hello {{username}}'.between('{{','}}') === 'username'
*/
String.prototype.between_regexp = function between(begin, end) {
var regexp = new RegExp(begin + '(.+)' + end);
return this.match(regexp)[1];
}
@NV
NV / function_comments.js
Created November 23, 2009 10:54
IE, Opera, Chrome doesn't remove comments after function.toString() coercion
function f(){/* comment */}
alert(f+'');
// Opera, IE, Chrome: function f(){/* comment */}
// Firefox, Safari: function f(){}
//
// Note: in Firefox: f+'' === f.toSource()
@NV
NV / ligatweet.user.js
Created December 1, 2009 00:38
Script for tweet shortening using unicode ligatures and other compound symbols
// ==UserScript==
// @name ligatweet
// @namespace http://leaverou.me/demos/ligatweet/
// @description Script for tweet shortening using unicode ligatures and other compound symbols
// @include htt*://twitter.com/*
// @author Lea Verou (UserJS by Nikita Vasilyev)
// @version 1.0
// @license Licensed under the MIT license http://www.opensource.org/licenses/mit-license.php
// ==/UserScript==
@NV
NV / README.md
Created December 29, 2009 19:57
Simple JS preprocessor with /*> file.js */
@NV
NV / greedy?.rb
Created January 1, 2010 05:17
Regex, why so greedy?
'wrath, greed, sloth, pride, lust, envy, and gluttony'.match(/greed.+/)
#<MatchData "greed, sloth, pride, lust, envy, and gluttony">
'wrath, greed, sloth, pride, lust, envy, and gluttony'.match(/greed.+?/)
#<MatchData "greed,">