Skip to content

Instantly share code, notes, and snippets.

View NV's full-sized avatar

Nikita Vasilyev NV

View GitHub Profile
a=['first', 'last'];
a[-1] == null;
a[-1] = a[a.length-1];
a[-1] == 'last'
@NV
NV / pythons_ugly_re.py
Created September 12, 2009 11:49
Why result cannot be accessed like array?
result = re.search(r"#[a-f0-9]{3}", 'Roses are #f00')
result.group(0) == '#f00' # True
result[0] # Damn! TypeError: '_sre.SRE_Match' object is unsubscriptable
# Why result cannot be accessed like array?
window.addEventListener('load', function(){
//Works in Opera
//Doesnt' work in Greasemonkey
alert('window')
}, false)
window.wrappedJSObject.addEventListener('load', function(){
//Doesn't work in Opera
//Works in Greasemonkey. See http://wiki.greasespot.net/UnsafeWindow
alert('wrappedJSObject')
// this gist is response to http://dmitry.baranovskiy.com/post/201475166
// Code by Dmitry Baranovskiy
var w=window,e=w.addEventListener,v=w.attachEvent;e&&e("load",a,!1);v&&v("onload",a);google.timers.load.t.prt=+new Date;
// Mine optimization. 4 characters less!
window[/*@cc_on'attachEvent'||@*/'addEventListener'](/*@cc_on'on'+@*/'load',a,0);google.timers.load.t.prt=+new Date;
// I found /*@cc_on @*/ trick at http://lusever.livejournal.com/43326.html
@NV
NV / String.insert.js
Created October 6, 2009 17:53
JavaScript implementation Ruby's str.insert method
/**
* 'Hello world'.insert(6, 'happy ') == 'Hello happy world'
* 'Hello world'.insert(-1, '!!!') == 'Hello world!!!'
* @param at {Number} index
* @param text {String}
* @see http://ruby-doc.org/core/classes/String.html#M000773
*/
String.prototype.insert = function (at, text) {
if (at < 0) {
at += this.length + 1;
@NV
NV / .htaccess
Created October 13, 2009 10:37
Apache .htaccess
DirectoryIndex index.html index.htm index.xhtml index.xml index.php
AddType message/rfc822 mht
AddType application/octet-stream safariextz
AddType application/atom+xml atom
AddType application/msword doc
AddType application/opensearchdescription+xml osd
AddType application/pdf pdf
AddType application/rdf+xml rdf
AddType application/rss+xml rss
@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 / 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