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://jsfromhell.com

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

Soundex //JavaScript Function



[UPDATED CODE AND HELP CAN BE FOUND HERE]


This function returns the phonetic code of a word.

example
   1  
   2  
   3  alert("Jonas\nJones\nMatrix\nMaytrix".replace(/\w+/g, function(s){
   4  	return s + " = " + s.soundex();
   5  }));
   6  
   7  


   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com/string/soundex [v1.0]
   4  
   5  String.prototype.soundex = function(p){ //v1.0
   6  	var i, j, r, p = isNaN(p) ? 4 : p > 10 ? 10 : p < 4 ? 4 : p,
   7  	m = {BFPV: 1, CGJKQSXZ: 2, DT: 3, L: 4, MN: 5, R: 6},
   8  	r = (s = this.toUpperCase().replace(/[^A-Z]/g, "").split("")).splice(0, 1);
   9  	for(i in s)
  10  		for(j in m)
  11  			if(j.indexOf(s[i]) + 1 && r[r.length-1] != m[j] && r.push(m[j]))
  12  				break;
  13  	return r.length > p && (r.length = p), r.join("") + (new Array(p - r.length + 1)).join("0");
  14  };
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS