Skip to content

Commit b1d7af3

Browse files
committed
Merge branch 'HEAD' of git@github.com:kvz/phpjs.git
2 parents 6b0a317 + 28b50f9 commit b1d7af3

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

functions/strings/soundex.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,34 @@ function soundex (str) {
77
// + input by: Brett Zamir (http://brett-zamir.me)
88
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
99
// + original by: Arnout Kazemier (http://www.3rd-Eden.com)
10+
// + revised by: Rafał Kukawski (http://blog.kukawski.pl)
1011
// * example 1: soundex('Kevin');
1112
// * returns 1: 'K150'
1213
// * example 2: soundex('Ellery');
1314
// * returns 2: 'E460'
1415
// * example 3: soundex('Euler');
1516
// * returns 3: 'E460'
1617

17-
var upStr = (str+'').toUpperCase();
18-
var sdx = [upStr[0],0,0,0],
19-
m = {BFPV: 1, CGJKQSXZ: 2, DT: 3, L: 4, MN: 5, R: 6 },
20-
k = ['BFPV', 'CGJKQSXZ', 'DT', 'L', 'MN', 'R'],
21-
i = 1, j = 0, s = 0, key, code,
22-
l = upStr.length;
18+
str = (str + '').toUpperCase();
19+
if (!str) {
20+
return '';
21+
}
22+
var sdx = [0, 0, 0, 0],
23+
m = {B: 1, F: 1, P: 1, V: 1, C: 2, G: 2, J: 2, K: 2, Q: 2, S: 2, X: 2, Z: 2, D: 3, T: 3, L: 4, M: 5, N: 5, R: 6},
24+
i = 0, j, s = 0, c, p;
2325

24-
for (; i < l; i++){
25-
j = k.length;
26-
while (s != 3 && j--){
27-
key = k[j];
28-
if (key.indexOf(upStr[i]) !== -1) {
29-
code = m[key];
30-
if (code != sdx[s]){
31-
sdx[++s] = code;
32-
}
26+
while ((c = str.charAt(i++)) && s < 4) {
27+
if (j = m[c]) {
28+
if (j !== p) {
29+
sdx[s++] = p = j;
3330
}
31+
} else {
32+
s += i === 1;
33+
p = 0;
3434
}
3535
}
36-
36+
37+
sdx[0] = str.charAt(0);
3738
return sdx.join('');
38-
}
39+
}
40+

0 commit comments

Comments
 (0)