PHP's metaphone in JavaScript

Here’s what our current JavaScript equivalent to PHP's metaphone looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
module.exports = function metaphone (word, maxPhonemes) {
// discuss at: http://locutus.io/php/metaphone/
// original by: Greg Frazier
// improved by: Brett Zamir (http://brett-zamir.me)
// improved by: Rafał Kukawski (http://blog.kukawski.pl)
// example 1: metaphone('Gnu')
// returns 1: 'N'
// example 2: metaphone('bigger')
// returns 2: 'BKR'
// example 3: metaphone('accuracy')
// returns 3: 'AKKRS'
// example 4: metaphone('batch batcher')
// returns 4: 'BXBXR'

var type = typeof word

if (type === 'undefined' || type === 'object' && word !== null) {
// weird!
return null
}

// infinity and NaN values are treated as strings
if (type === 'number') {
if (isNaN(word)) {
word = 'NAN'
} else if (!isFinite(word)) {
word = 'INF'
}
}

if (maxPhonemes < 0) {
return false
}

maxPhonemes = Math.floor(+maxPhonemes) || 0

// alpha depends on locale, so this var might need an update
// or should be turned into a regex
// for now assuming pure a-z
var alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
var vowel = 'AEIOU'
var soft = 'EIY'
var leadingNonAlpha = new RegExp('^[^' + alpha + ']+')

word = typeof word === 'string' ? word : ''
word = word.toUpperCase().replace(leadingNonAlpha, '')

if (!word) {
return ''
}

var is = function (p, c) {
return c !== '' && p.indexOf(c) !== -1
}

var i = 0
var cc = word.charAt(0) // current char. Short name because it's used all over the function
var nc = word.charAt(1) // next char
var nnc // after next char
var pc // previous char
var l = word.length
var meta = ''
// traditional is an internal param that could be exposed for now let it be a local var
var traditional = true

switch (cc) {
case 'A':
meta += nc === 'E' ? nc : cc
i += 1
break
case 'G':
case 'K':
case 'P':
if (nc === 'N') {
meta += nc
i += 2
}
break
case 'W':
if (nc === 'R') {
meta += nc
i += 2
} else if (nc === 'H' || is(vowel, nc)) {
meta += 'W'
i += 2
}
break
case 'X':
meta += 'S'
i += 1
break
case 'E':
case 'I':
case 'O':
case 'U':
meta += cc
i++
break
}

for (; i < l && (maxPhonemes === 0 || meta.length < maxPhonemes); i += 1) { // eslint-disable-line no-unmodified-loop-condition,max-len
cc = word.charAt(i)
nc = word.charAt(i + 1)
pc = word.charAt(i - 1)
nnc = word.charAt(i + 2)

if (cc === pc && cc !== 'C') {
continue
}

switch (cc) {
case 'B':
if (pc !== 'M') {
meta += cc
}
break
case 'C':
if (is(soft, nc)) {
if (nc === 'I' && nnc === 'A') {
meta += 'X'
} else if (pc !== 'S') {
meta += 'S'
}
} else if (nc === 'H') {
meta += !traditional && (nnc === 'R' || pc === 'S') ? 'K' : 'X'
i += 1
} else {
meta += 'K'
}
break
case 'D':
if (nc === 'G' && is(soft, nnc)) {
meta += 'J'
i += 1
} else {
meta += 'T'
}
break
case 'G':
if (nc === 'H') {
if (!(is('BDH', word.charAt(i - 3)) || word.charAt(i - 4) === 'H')) {
meta += 'F'
i += 1
}
} else if (nc === 'N') {
if (is(alpha, nnc) && word.substr(i + 1, 3) !== 'NED') {
meta += 'K'
}
} else if (is(soft, nc) && pc !== 'G') {
meta += 'J'
} else {
meta += 'K'
}
break
case 'H':
if (is(vowel, nc) && !is('CGPST', pc)) {
meta += cc
}
break
case 'K':
if (pc !== 'C') {
meta += 'K'
}
break
case 'P':
meta += nc === 'H' ? 'F' : cc
break
case 'Q':
meta += 'K'
break
case 'S':
if (nc === 'I' && is('AO', nnc)) {
meta += 'X'
} else if (nc === 'H') {
meta += 'X'
i += 1
} else if (!traditional && word.substr(i + 1, 3) === 'CHW') {
meta += 'X'
i += 2
} else {
meta += 'S'
}
break
case 'T':
if (nc === 'I' && is('AO', nnc)) {
meta += 'X'
} else if (nc === 'H') {
meta += '0'
i += 1
} else if (word.substr(i + 1, 2) !== 'CH') {
meta += 'T'
}
break
case 'V':
meta += 'F'
break
case 'W':
case 'Y':
if (is(vowel, nc)) {
meta += cc
}
break
case 'X':
meta += 'KS'
break
case 'Z':
meta += 'S'
break
case 'F':
case 'J':
case 'L':
case 'M':
case 'N':
case 'R':
meta += cc
break
}
}

return meta
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/strings/metaphone'). You could also require the strings module in full so that you could access strings.metaphone instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1metaphone('Gnu')'N'
2metaphone('bigger')'BKR'
3metaphone('accuracy')'AKKRS'
4metaphone('batch batcher')'BXBXR'

« More PHP strings functions