Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { soundex } from 'locutus/php/strings/soundex'.
Or with CommonJS: const { soundex } = require('locutus/php/strings/soundex')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
soundex('Kevin')
'K150'
2
soundex('Ellery')
'E460'
3
soundex('Euler')
'E460'
Here's what our current TypeScript equivalent to PHP's soundex looks like.
let c = input.charAt(i++) while (c && s < 4) { const j = m[c] if (j) { if (j !== p) { sdx[s++] = p = j } } else { s += Number(i === 1) p = 0 } c = input.charAt(i++) }
sdx[0] = input.charAt(0)
return sdx.join('') }
exportfunctionsoundex(str) { // discuss at: https://locutus.io/php/soundex/ // parity verified: PHP 8.3 // original by: Jonas Raoni Soares Silva (https://www.jsfromhell.com) // original by: Arnout Kazemier (https://www.3rd-Eden.com) // improved by: Jack // improved by: Kevin van Zonneveld (https://kvz.io) // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman) // bugfixed by: Kevin van Zonneveld (https://kvz.io) // input by: Brett Zamir (https://brett-zamir.me) // revised by: Rafał Kukawski (https://blog.kukawski.pl) // example 1: soundex('Kevin') // returns 1: 'K150' // example 2: soundex('Ellery') // returns 2: 'E460' // example 3: soundex('Euler') // returns 3: 'E460'
const input = String(str).toUpperCase() if (!input) { return'' }
const sdx = [0, 0, 0, 0] const 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, } let i = 0 let s = 0 let p = 0
let c = input.charAt(i++) while (c && s < 4) { const j = m[c] if (j) { if (j !== p) { sdx[s++] = p = j } } else { s += Number(i === 1) p = 0 } c = input.charAt(i++) }
sdx[0] = input.charAt(0)
return sdx.join('') }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.