DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
  • submit to reddit
        <a href="http://jsfromhell.com/string/rot13">

[UPDATED CODE AND HELP CAN BE FOUND HERE]
</a>

This function codes/decodes strings into ROT13 (rotate the alpha chars by 13 positions...)

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/string/rot13 [v1.0]

String.prototype.rot13 = function(){ //v1.0
	return this.replace(/[a-zA-Z]/g, function(c){
		return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
	});
};
    
        <a href="http://www.jsfromhell.com/string/soundex">

[UPDATED CODE AND HELP CAN BE FOUND HERE]
</a>

This function returns the phonetic code of a word.

example

alert("Jonas\nJones\nMatrix\nMaytrix".replace(/\w+/g, function(s){
	return s + " = " + s.soundex();
}));



//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/string/soundex [v1.0]

String.prototype.soundex = function(p){ //v1.0
	var i, j, r, p = isNaN(p) ? 4 : p > 10 ? 10 : p < 4 ? 4 : p,
	m = {BFPV: 1, CGJKQSXZ: 2, DT: 3, L: 4, MN: 5, R: 6},
	r = (s = this.toUpperCase().replace(/[^A-Z]/g, "").split("")).splice(0, 1);
	for(i in s)
		for(j in m)
			if(j.indexOf(s[i]) + 1 && r[r.length-1] != m[j] && r.push(m[j]))
				break;
	return r.length > p && (r.length = p), r.join("") + (new Array(p - r.length + 1)).join("0");
};
    
        Given a negative or huge angle, it returns a number between 0 and 359 :b

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

function realAngle(a){
	return (a %= 360) < 0 ? a + 360 : a;
}

usage

alert(realAngle(359));
alert(realAngle(-45));
alert(realAngle(1234));
    
        <a href="http://jsfromhell.com/classes/overloader">
This class allows javascript functions to be overloaded.

[UPDATED CODE AND HELP CAN BE FOUND HERE]
</a>



Usage:

myFunction = new Overloader;

myFunction.overload(function(x){
	document.write("Receives: NUMBER<br />");
}, Number);

myFunction.overload(function(x){
	document.write("Receives: STRING<br />");
}, String);

myFunction.overload(function(x,y){
	document.write("Receives: FUNCTION, NUMBER<br />");
}, Function, Number);

myFunction.overload(function(x,y){
	document.write("Receives: NUMBER, STRING<br />");
}, Number, String);

//test...
myFunction(function(){}, 123); //function + number version
myFunction(123); //number version
myFunction("ABC"); //string version
myFunction(123, "ABC"); //number + string version
myFunction({}); /*There's no Object version, so the function will choose the one that has more arguments in common and if there isnt a "best match", it will use the first function that was overloaded...*/


Here's the code

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/classes/overloader [v1.0]

Overloader = function(){ //v1.0
	var f = function(args){
		var i, h = "#";
		for(i in args = [].slice.call(arguments))
			h += args[i].constructor;
		if(!(h = f._methods[h])){
			var x, j, k, m = -1;
			for(i in f._methods){
				for(j in args.length > (k = 0, x = f._methods[i][1]).length ? x : args)
					(args[j] instanceof x[j] || args[j].constructor == x[j]) && ++k;
				k > m && (h = f._methods[i], m = k);
			}
		}
		return h ? h[0].apply(f, args) : undefined;
	};
	f._methods = {};
	f.overload = function(f, args){
		this._methods["#" + (args = [].slice.call(arguments, 1)).join("")] = [f, args];
	};
	f.unoverload = function(args){
		return delete this._methods["#" + [].slice.call(arguments).join("")];
	};
	return f;
};
    
        You can draw directly to the phone screen.
It is useful for a notification from a background program.
You need to install fgimage.pyd from <a href=http://pymbian.sourceforge.net/misc/fgimage-v0.20051022.zip>here</a>.
import fgimage, e32

img = Image.new((100, 16))  # create a notification text
img.text((0, 14), u'Hello world', 0)

fg = fgimage.FGImage()
x, y = 0, 40           # position to draw
fg.set(x, y, img._bitmapapi())  # send it

e32.ao_sleep(1)
fg.unset()                # then clear it

See (a bit) more discussion <a href=http://discussion.forum.nokia.com/forum/showthread.php?t=68866>here</a>.    
        in /etc/sudoers:

  # Added by Ubuntu installer
  username   ALL=(ALL) ALL, NOPASSWD: /usr/sbin/synaptic

With 'NOPASSWD: /usr/sbin/synaptic' you can enter 'sudo synaptic' without a password prompt.     
        
  validates_inclusion_of :birthday,
    :in => Date.new(1900)..Time.now.years_ago(18).to_date,
    :message => 'Too young, dude!'
    
        to start teamspeak correct with artsd: 
#!/bin/sh

artsd -F 6 -S 256 -d & 
artsd -m $HOME/TeamSpeak2RC2/TeamSpeak  # or wherever you have put your ts
    
        To disable the beep in the x-terminal: 

  xset -b 
    
        A quick rollover snippet...

.menu_img1 {
	width: 25px;
	height: 25px;
	background: no-repeat url(image1.jpg);
}

a.rollover_img1:hover img {
	background: no-repeat url(image2.jpg)
}

And the html hooblah:

<div class="menu_img1">
  <a href="#" class="rollover_img1">
    <img src="t.gif" width="25" height="25" border="0">
  </a>
</div>

The t.gif is a 1x1 transparent image. Tested in IE5 (Windows/Mac), Firefox, Safari.    
        Pys60 provide you with 3 types for app.body
- Text
- ListBox
- Canvas

Sometimes you want some text and image together, you need to
use canvas. However, there's no way to calculate the length
of text and wrap them properly. Simo's dashboard provide a
solution to this using his akntextutils C++ extension.
from akntextutils import wrap_text_to_array

# long_str is a long string to be wrapped
lines = wrap_text_to_array(long_str, 'dense', 176)

x, y = 2, 0
for line in lines:
   y += 14
   canvas.text((x, y), line, font='dense')
    
        I wrote <a href=http://www.bigbold.com/snippets/posts/show/632>a hack</a> to get the color of a pixel.
Now pys60 1.2 has support for getpixel (though undocumented).
Cyke64 has shown me in <a href=http://discussion.forum.nokia.com/forum/showpost.php?p=168137&postcount=8>an example</a>.
>>> from graphics import Image
>>> im = Image.new((10,10))   # create a new 10x10 image
>>> im.clear(0xff0000)        # make it all red
>>> im.getpixel((0,0))   # top-left is red
[(255, 0, 0)]
>>> im.getpixel([(0,0), (10,10)])  # you can get multiple points
[(255, 0, 0), (255, 0, 0)]
>>> 
    
        e32.ao_sleep has a 2147-sec limit. Here is how to
sleep longer. Based on dashboard's exttimer
import e32

def sleep(delay, callback):
    MAX = 2147
    if delay > MAX:
        e32.ao_sleep(MAX, lambda : sleep(delay-MAX, callback))
    else:
        e32.ao_sleep(delay, callback)
    
        Assumes you've already downloaded the Windows installer to your desktop. For more details see <a href="http://robbevan.com/blog/2005/10/23/working-with-flex-2-on-mac-os-x/">this post</a>.


#!/bin/sh

mkdir /Library/flex2
cd /Library/flex2
unzip ~/Desktop/flexbuilder2_a1_standalone_10-14.exe InstallerData/Disk1/InstData/Resource1.zip -d tmp
unzip tmp/InstallerData/Disk1/InstData/Resource1.zip D_/builds/flex2/frameworks_zg_ia_sf.jar -d tmp
unzip tmp/D_/builds/flex2/frameworks_zg_ia_sf.jar -d frameworks
unzip tmp/InstallerData/Disk1/InstData/Resource1.zip D_/builds/flex2/lib_zg_ia_sf.jar -d tmp
unzip tmp/D_/builds/flex2/lib_zg_ia_sf.jar -d lib
unzip tmp/InstallerData/Disk1/InstData/Resource1.zip D_/builds/flex2/player/Version\ 8.5\ alpha\ 1/Debug/Install\ Flash\ Player\ 8.5\ OSX.dmg -d tmp
mv tmp/D_/builds/flex2/player/Version\ 8.5\ alpha\ 1/Debug/Install\ Flash\ Player\ 8.5\ OSX.dmg ~/Desktop/
rm -R tmp


    
        
sudo dpkg-reconfigure proftpd