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

Snippets has posted 5883 posts at DZone. View Full User Profile

Soundex //JavaScript Function

11.02.2005
| 12855 views |
  • submit to reddit
        <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");
};
    

Comments

Snippets Manager replied on Sat, 2011/08/27 - 1:13pm

This has two things going for it: it's cute and may even given the correct result. Unfortunately, as with any associative array, the for-in loop may give many more values than you expect (e.g., any prototypes that may have assigned). Same goes for the string for-in loop. Simple often works best: String.prototype.soundex = function(msz) { var sndx = this.charAt(0).toUpperCase(); if ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.indexOf(sndx)<0) return ''; var sz = (isNaN(msz)|| msz<4) ? 4 : Math.max(Math.min(msz,10),4); var key = 'BCDFGJKLMNPQRSTVXZ'; var map = '123122245512623122'; // may be cheaper to uppercase first than to double the scan length [maybe not!] var s = this.substr(1) // already have the first letter .toUpperCase() // see 'cheaper' comment .replace(/[^BCDFGJKLMNPQRSTVXZ]/g,'') // the only letters that matter // .replace(/(.)\1+/g,'$1') // remove double letters [usually too slow] for (var c,p='',i=0;i

Snippets Manager replied on Sat, 2011/08/27 - 1:13pm

This has two things going for it: it's cute and may even given the correct result. Unfortunately, as with any associative array, the for-in loop may give many more values than you expect (e.g., any prototypes that may have assigned). Same goes for the string for-in loop. Simple often works best: String.prototype.soundex = function(msz) { var sndx = this.charAt(0).toUpperCase(); if ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.indexOf(sndx)<0) return ''; var sz = (isNaN(msz)|| msz<4) ? 4 : Math.max(Math.min(msz,10),4); var key = 'BCDFGJKLMNPQRSTVXZ'; var map = '123122245512623122'; // may be cheaper to uppercase first than to double the scan length [maybe not!] var s = this.substr(1) // already have the first letter .toUpperCase() // see 'cheaper' comment .replace(/[^BCDFGJKLMNPQRSTVXZ]/g,'') // the only letters that matter // .replace(/(.)\1+/g,'$1') // remove double letters [usually too slow] for (var c,p='',i=0;i

Snippets Manager replied on Sat, 2011/08/27 - 1:13pm

This has two things going for it: it's cute and may even given the correct result. Unfortunately, as with any associative array, the for-in loop may give many more values than you expect (e.g., any prototypes that may have assigned). Same goes for the string for-in loop. Simple often works best: String.prototype.soundex = function(msz) { var sndx = this.charAt(0).toUpperCase(); if ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.indexOf(sndx)<0) return ''; var sz = (isNaN(msz)|| msz<4) ? 4 : Math.max(Math.min(msz,10),4); var key = 'BCDFGJKLMNPQRSTVXZ'; var map = '123122245512623122'; // may be cheaper to uppercase first than to double the scan length [maybe not!] var s = this.substr(1) // already have the first letter .toUpperCase() // see 'cheaper' comment .replace(/[^BCDFGJKLMNPQRSTVXZ]/g,'') // the only letters that matter // .replace(/(.)\1+/g,'$1') // remove double letters [usually too slow] for (var c,p='',i=0;i