npmtest-talisman (v0.0.1)

Code coverage report for node-npmtest-talisman/node_modules/talisman/phonetics/soundex.js

Statements: 39.47% (15 / 38)      Branches: 21.43% (3 / 14)      Functions: 25% (1 / 4)      Lines: 43.75% (14 / 32)      Ignored: none     

All files » node-npmtest-talisman/node_modules/talisman/phonetics/ » soundex.js
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    1     1 1   1   1   1   1                                 1   1         1                       1                                                             1                                     1 1  
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: false
});
exports.default = soundex;
exports.refined = refined;
 
var _helpers = require('../helpers');
 
var _deburr = require('lodash/deburr');
 
var _deburr2 = _interopRequireDefault(_deburr);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
/**
 * Translations.
 */
/**
 * Talisman phonetics/soundex
 * ===========================
 *
 * The Soundex algorithm.
 *
 * [Reference]: https://en.wikipedia.org/wiki/Soundex
 *
 * [Authors]:
 * Robert C. Russel
 * Margaret King Odell
 */
var TRANSLATIONS = (0, _helpers.translation)('AEIOUYWHBPFVCSKGJQXZDTLMNR', '000000DD111122222222334556');
 
var REFINED_TRANSLATIONS = (0, _helpers.translation)('AEIOUYWHBPFVCKSGJQXZDTLMNR', '000000DD112233344555667889');
 
/**
 * Helpers.
 */
function pad(code) {
  return (code + '0000').slice(0, 4);
}
 
/**
 * Function taking a single name and computing its Soundex code.
 *
 * @param  {string}  name - The name to process.
 * @return {string}       - The Soundex code.
 *
 * @throws {Error} The function expects the name to be a string.
 */
function soundex(name) {
  if (typeof name !== 'string') throw Error('talisman/phonetics/soundex: the given name is not a string.');
 
  name = (0, _deburr2.default)(name).toUpperCase().replace(/[^A-Z]/g, '');
 
  var firstLetter = name.charAt(0);
 
  // Process the code for the name's tail
  var tail = '';
 
  for (var i = 1, l = name.length; i < l; i++) {
    if (TRANSLATIONS[name[i]] !== 'D') tail += TRANSLATIONS[name[i]];
  }
 
  // Dropping first code's letter if duplicate
  if (tail.charAt(0) === TRANSLATIONS[firstLetter]) tail = tail.slice(1);
 
  // Composing the code from the tail
  var code = (0, _helpers.squeeze)(tail).replace(/0/g, '');
 
  return pad(firstLetter + code);
}
 
/**
 * Function taking a single name and computing its refined Soundex code.
 *
 * @param  {string}  name - The name to process.
 * @return {string}       - The refined Soundex code.
 *
 * @throws {Error} The function expects the name to be a string.
 */
function refined(name) {
  if (typeof name !== 'string') throw Error('talisman/phonetics/soundex#refined: the given name is not a string.');
 
  name = (0, _deburr2.default)(name).toUpperCase().replace(/[^A-Z]/g, '');
 
  var firstLetter = name.charAt(0);
 
  // Process the code for the name's tail
  var tail = '';
 
  for (var i = 0, l = name.length; i < l; i++) {
    if (REFINED_TRANSLATIONS[name[i]] !== 'D') tail += REFINED_TRANSLATIONS[name[i]];
  }
 
  // Composing the code from the tail
  var code = (0, _helpers.squeeze)(tail);
 
  return firstLetter + code;
}
module.exports = exports['default'];
exports['default'].refined = exports.refined;