Never been to DZone Snippets before?

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

About this user

Jonas Raoni Soares Silva http://www.jsfromhell.com

« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS 

Replace working as PHP str_replace //JavaScript Function

Useless JavaScript implementation of the php function str_replace.

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

function replace(f, r, s){
	var ra = r instanceof Array, sa = s instanceof Array, l = (f = [].concat(f)).length, r = [].concat(r), i = (s = [].concat(s)).length;
	while(j = 0, i--)
		while(s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j < l);
	return sa ? s : s[0];
}

repeat //JavaScript Function

This function adds an efficient repeater to the String prototype.

example:
alert("Jonas\n".repeat(5));


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

String.prototype.repeat = function(l){
	return new Array(l+1).join(this);
};

Pad //JavaScript Function



[UPDATED CODE AND HELP CAN BE FOUND HERE]



usage:

var s = "Jonas";
document.write(
  s, " => ", s.pad(20, "[]", 0), "<br />",
  s, " => ", s.pad(20, "[====]", 1), "<br />",
  s, " => ", s.pad(20, "~", 2)
);


code:

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

String.prototype.pad = function(l, s, t){
	return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
		+ 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
		+ this + s.substr(0, l - t) : this;
};

Capitalize //JavaScript Function



[UPDATED CODE AND HELP CAN BE FOUND HERE]


usage:

document.write(s = "jonas Raoni a SOARES silva", " => ", s.capitalize());


code:

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

String.prototype.capitalize = function(){ //v1.0
    return this.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
    });
};

WordWrap //JavaScript Function



[UPDATED CODE AND HELP CAN BE FOUND HERE]


usage

alert("My world is biiiiiiiiiiiiig".wordWrap(10, "\n+", true));
alert("My world is biiiiiiiiiiiiig".wordWrap(10, "\n>", false));


code

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

String.prototype.wordWrap = function(m, b, c){
	var i, j, l, s, r;
	if(m < 1)
		return this;
	for(i = -1, l = (r = this.split("\n")).length; ++i < l; r[i] += s)
		for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : ""))
			j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length
			|| c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
	return r.join("\n");
};

ROT13 //JavaScript Function



[UPDATED CODE AND HELP CAN BE FOUND HERE]


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);
	});
};

Soundex //JavaScript Function



[UPDATED CODE AND HELP CAN BE FOUND HERE]


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");
};

pascal triangle plotter XD

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

#include <stdio.h>
#include <conio.h>

#define MAX 16

int main () {
	int unsigned vetor[MAX], grau, i = 0, j, top, left;

	clrscr();

	printf( "Dado o grau, printar o triangulo de pascal\n" );

	while( grau > MAX && printf( "Digite um numero entre 0 e %d para o grau: ", MAX ) && scanf( "%u", &grau ) );
	while( i++ < grau && !( j = 0 ) && printf( "\n" ) )
		while( j < i && ( j == 0 && ( top = left = vetor[j] = 1 ) ? 1 : j > 0 && j < i-1 && ( top = vetor[j] ) && ( vetor[j] = left + top ) && ( left = top ) ? 1 : ( vetor[j] = 1 ) ) && printf( "%-5d", vetor[j] ) && ++j );
	getch();
	return 0;
}

« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS