├── .babelrc ├── .gitignore ├── .jshintrc ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── src └── full-name-splitter.js └── test └── src └── full-name-splitter.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } 4 | 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | lib/ 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "esversion": 6 3 | } 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | test/ 2 | src/ 3 | .travis.yml 4 | .jshintrc 5 | .gitignore 6 | .babelrc 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | - "iojs" 5 | - "6" 6 | - "5" 7 | - "4" 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2017 Atlassian. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Full Name Splitter 2 | 3 | Human names are complicated. They don't necessarily follow any rules. Here's [a good article about the complexities of names](https://www.w3.org/International/questions/qa-personal-names). 4 | 5 | As that article advises, it's ideal to just store a user's full name, and not assume anything about the form the name takes. 6 | 7 | However, there are times when it is necessary to have a split given name and family name. Such as when integrating with another system that made different design choices. 8 | 9 | This package does a best-guess attempt to split a full name string into given name and family name. For many Western names, it will get it right. It knows about some common family name prefixes used in many European-language names, and initials. It removes salutations (`Miss`, `Doctor`, etc) and suffixes (`III`, `Jr`, etc). 10 | 11 | ## Examples 12 | 13 | ```js 14 | import { splitter } from 'full-name-splitter'; 15 | 16 | splitter("George H. W. Bush") 17 | // => ["George H. W.", "Bush"] 18 | 19 | splitter("Kevin J. O'Connor") 20 | // => ["Kevin J.", "O'Connor"] 21 | 22 | splitter("Thomas G. Della Fave") 23 | // => ["Thomas G.", "Della Fave"] 24 | 25 | splitter("Gabriel Van Helsing") 26 | // => ["Gabriel", "Van Helsing"] 27 | 28 | // If full name isn't complete, it tries to split partially: 29 | 30 | splitter("George W.") 31 | // => ["George W.", null] 32 | 33 | splitter("George") 34 | // => ["George", null] 35 | 36 | splitter("Van Helsing") 37 | // => [null, "Van Helsing"] 38 | 39 | splitter("d'Artagnan") 40 | // => [null, "d'Artagnan"] 41 | ``` 42 | 43 | For other examples see `test/src/full-name-splitter.js` 44 | 45 | If it can't split a name correctly, it is possible to split by comma: 46 | 47 | ```js 48 | splitter("John Quincy Adams") 49 | // => ["John Quincy", "Adams"] 50 | 51 | splitter("John, Quincy Adams") 52 | // => ["John", "Quincy Adams"] 53 | ``` 54 | 55 | ## Usage 56 | 57 | The ES6 source (in `src/`) is transpiled by Babel into `lib/` in the npm `prepublish` hook. 58 | 59 | If you're not using this from npm, you'll need to manually trigger transpilation: 60 | 61 | ```bash 62 | npm install && npm run prepublish 63 | ``` 64 | 65 | ## Copyright 66 | 67 | [Original Ruby version](https://github.com/pahanix/full-name-splitter) created by Pavel Gorbokon, with contributions by Michael S. Klishin and Trevor Creech. 68 | 69 | This JavaScript port by [Matt Dolan](https://twitter.com/_MattDolan). 70 | 71 | Released under the MIT license. 72 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "full-name-splitter", 3 | "version": "1.1.1", 4 | "description": "Splits a string containing a (Western) person's full name into first and last name components", 5 | "main": "lib/full-name-splitter.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "preversion": "npm run test", 11 | "prepublish": "babel -d lib/ src/", 12 | "test-syntax": "echo $'\\e[33m';jshint src/ test/src/ && echo $'\\e[32mjshint loves you.\\e[0m' || echo $'\\e[41mjshint is not happy.\\e[0m'", 13 | "test-functional": "mocha --compilers js:babel-core/register test/src/*", 14 | "test": "npm run test-syntax;npm run test-functional" 15 | }, 16 | "author": [ 17 | "Ruby originally by Pavel Gorbokon ", 18 | "Ruby contributors Michael S. Klishin and Trevor Creech", 19 | "JavaScript port by Matt Dolan " 20 | ], 21 | "license": "MIT", 22 | "devDependencies": { 23 | "babel-cli": "^6.23.0", 24 | "babel-core": "^6.23.1", 25 | "babel-preset-es2015": "^6.22.0", 26 | "jshint": "^2.9.4", 27 | "mocha": "^3.2.0" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/full-name-splitter.js: -------------------------------------------------------------------------------- 1 | const LAST_NAME_PREFIXES = ['de', 'da', 'la', 'du', 'del', 'dei', 'vda.', 2 | 'dello', 'della', 'degli', 'delle', 'van', 'von', 'der', 'den', 'heer', 3 | 'ten', 'ter', 'vande', 'vanden', 'vander', 'voor', 'ver', 'aan', 'mc']; 4 | 5 | const SUFFIX_REGEX = 6 | /,? +(i{1,3}|iv|vi{0,3}|s(enio)?r|j(unio)?r|phd|apr|rph|pe|md|ma|dmd|cme)$/i; 7 | 8 | const SALUTATION_REGEX = 9 | /^(mrs?|m[ia]ster|miss|ms|d(octo)?r|prof|rev|fr|judge|honorable|hon|lord|lady)\.?$/i; 10 | 11 | const isLastNamePrefix = (token) => 12 | LAST_NAME_PREFIXES.indexOf(token.toLowerCase()) != -1; 13 | 14 | const isSalutation = (token) => token && token.match(SALUTATION_REGEX); 15 | 16 | // M or W. 17 | const isInitial = (token) => token.match(/^\w\.?$/); 18 | 19 | // O'Connor, d'Artagnan match 20 | // Noda' doesn't match 21 | const hasApostrophe = (token) => token.match(/\w{1}'\w+/); 22 | 23 | const adjust_exceptions = function (firstNames, lastNames) { 24 | // Adjusting exceptions like 25 | // "Ludwig Mies van der Rohe" => ["Ludwig", "Mies van der Rohe"] 26 | // "Juan Martín de la Cruz Gómez" => ["Juan Martín", "de la Cruz Gómez"] 27 | // "Javier Reyes de la Barrera" => ["Javier", "Reyes de la Barrera"] 28 | // "Rosa María Pérez Martínez Vda. de la Cruz" 29 | // => ["Rosa María", "Pérez Martínez Vda. de la Cruz"] 30 | if (firstNames.length > 1 && 31 | !isInitial(firstNames[firstNames.length-1]) && 32 | lastNames.join(' ').match(/^(van der|(vda\. )?de la \w+$)/i)) { 33 | while (1) { 34 | lastNames.unshift(firstNames.pop()); 35 | if (firstNames.length <= 2) break; 36 | } 37 | } 38 | 39 | return [firstNames, lastNames]; 40 | }; 41 | 42 | const tokenizeFullName = function (fullName) { 43 | fullName = fullName.trim().replace(/\s+/g, ' ') 44 | .replace(SUFFIX_REGEX, ''); 45 | 46 | if (fullName.indexOf(',') != -1) { 47 | return fullName 48 | // ",van helsing" produces ["", "van helsing"] 49 | .split(/\s*,\s*/, 2) 50 | // but it should be [null, "van helsing"] by lib convention 51 | .map(u => u || null).reverse(); 52 | } else { 53 | return fullName.split(/\s+/); 54 | } 55 | }; 56 | 57 | export default function (fullName) { 58 | let token; 59 | let tokens = tokenizeFullName(fullName); 60 | let firstNames = []; 61 | let lastNames = []; 62 | 63 | while ((token = tokens.shift())) { 64 | if (isLastNamePrefix(token) || hasApostrophe(token) || 65 | (firstNames.length && tokens.length === 0 && !isInitial(token))) { 66 | lastNames.push(token); 67 | break; 68 | } else { 69 | firstNames.push(token); 70 | } 71 | } 72 | lastNames = lastNames.concat(tokens); 73 | 74 | if (isSalutation(firstNames[0])) { 75 | firstNames.shift(); 76 | } 77 | [firstNames, lastNames] = adjust_exceptions(firstNames, lastNames); 78 | 79 | return [ 80 | firstNames.join(' ') || null, 81 | lastNames.join(' ') || null 82 | ]; 83 | } 84 | -------------------------------------------------------------------------------- /test/src/full-name-splitter.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert'; 2 | import splitter from './../../src/full-name-splitter.js'; 3 | 4 | const expectations = [ 5 | ["John Smith", ["John", "Smith" ]], 6 | ["John", ["John", null]], 7 | 8 | ["Kevin J. O'Connor", ["Kevin J.", "O'Connor" ]], 9 | ["Gabriel Van Helsing", ["Gabriel", "Van Helsing" ]], 10 | ["Pierre de Montesquiou", ["Pierre", "de Montesquiou" ]], 11 | ["Charles d'Artagnan", ["Charles", "d'Artagnan" ]], 12 | ["Ben Butler-Sandwich", ["Ben", "Butler-Sandwich" ]], 13 | ["Noda' bi-Yehudah", ["Noda'", "bi-Yehudah" ]], 14 | ["Maria del Carmen Menendez", ["Maria", "del Carmen Menendez" ]], 15 | ["Alessandro Del Piero", ["Alessandro", "Del Piero" ]], 16 | 17 | ["George W Bush", ["George W", "Bush" ]], 18 | ["George H. W. Bush", ["George H. W.", "Bush" ]], 19 | ["James K. Polk", ["James K.", "Polk" ]], 20 | ["William Henry Harrison", ["William Henry", "Harrison" ]], 21 | ["John Quincy Adams", ["John Quincy", "Adams" ]], 22 | 23 | ["John Quincy", ["John", "Quincy" ]], 24 | ["George H. W.", ["George H. W.", null]], 25 | ["Van Helsing", [null, "Van Helsing" ]], 26 | ["d'Artagnan", [null, "d'Artagnan" ]], 27 | ["O'Connor", [null, "O'Connor" ]], 28 | 29 | ["George", ["George", null]], 30 | ["Kevin J. ", ["Kevin J.", null]], 31 | 32 | ["Thomas G. Della Fave", ["Thomas G.", "Della Fave" ]], 33 | ["Anne du Bourg", ["Anne", "du Bourg" ]], 34 | 35 | // German 36 | ["Johann Wolfgang von Goethe", ["Johann Wolfgang", "von Goethe" ]], 37 | 38 | // Spanish-speaking countries 39 | ["Juan Martín de la Cruz Gómez", ["Juan Martín", "de la Cruz Gómez" ]], 40 | ["Javier Reyes de la Barrera", ["Javier", "Reyes de la Barrera" ]], 41 | ["Rosa María Pérez Martínez Vda. de la Cruz", 42 | ["Rosa María", "Pérez Martínez Vda. de la Cruz"]], 43 | 44 | // Italian 45 | ["Federica Pellegrini", ["Federica", "Pellegrini" ]], 46 | ["Leonardo da Vinci", ["Leonardo", "da Vinci" ]], 47 | // sounds like a fancy medival action movie star pseudonim 48 | ["Alberto Del Sole", ["Alberto", "Del Sole" ]], 49 | // horror movie star pseudonim? 50 | ["Adriano Dello Spavento", ["Adriano", "Dello Spavento" ]], 51 | ["Luca Delle Fave", ["Luca", "Delle Fave" ]], 52 | ["Francesca Della Valle", ["Francesca", "Della Valle" ]], 53 | ["Guido Delle Colonne", ["Guido", "Delle Colonne" ]], 54 | ["Tomasso D'Arco", ["Tomasso", "D'Arco" ]], 55 | 56 | // Dutch 57 | ["Johan de heer Van Kampen", ["Johan", "de heer Van Kampen" ]], 58 | ["Han Van De Casteele", ["Han", "Van De Casteele" ]], 59 | ["Han Vande Casteele", ["Han", "Vande Casteele" ]], 60 | ["Albert Van Der Haart", ["Albert", "Van Der Haart"]], 61 | 62 | // Exceptions? 63 | // the architect Ludwig Mies van der Rohe, from the West German city of Aachen, was originally Ludwig Mies; 64 | ["Ludwig Mies van der Rohe", ["Ludwig", "Mies van der Rohe" ]], 65 | 66 | ["E. Mark Slater", ["E. Mark", "Slater"]], 67 | ["Ken E. Mark Slater", ["Ken E. Mark", "Slater"]], 68 | ["Lord Sebastian Coe", ["Sebastian", "Coe"]], 69 | ["King Richard V", ["King", "Richard"]], 70 | ["Dr Who", [null, "Who"]], 71 | ["Mr Van", [null, "Van"]], 72 | ["Marie-Anne Richmond-Smithe", ["Marie-Anne", "Richmond-Smithe"]], 73 | 74 | // Test ignoring unnecessary whitespaces 75 | ["\t Ludwig Mies\t van der Rohe ", ["Ludwig", "Mies van der Rohe" ]], 76 | ["\t van der Rohe,\t Ludwig Mies ", ["Ludwig Mies", "van der Rohe" ]], 77 | ["\t Ludwig ", ["Ludwig", null]], 78 | [" van helsing ", [null, "van helsing"]], 79 | [", van helsing ", ["van helsing", null]], 80 | ["\t van der Rohe,\t Ludwig Mies ", ["Ludwig Mies", "van der Rohe" ]], 81 | 82 | // from humanparser 83 | ['Mr. William R. Hearst, III', ['William R.', 'Hearst']], 84 | ['William Randolph Hearst', ['William Randolph', 'Hearst']], 85 | ['William R. De La Cruz', ['William R.', 'De La Cruz']], 86 | ['Mr. William R. De La Cruz III', ['William R.', 'De La Cruz']], 87 | ['William De Cruz', ['William', 'De Cruz']], 88 | ['William De La Cruz', ['William', 'De La Cruz']], 89 | ['William A. B. De La Cruz', ['William A. B.', 'De La Cruz']], 90 | ['James Hugh Calum Laurie', ['James Hugh Calum', 'Laurie']], 91 | ['Kiefer William Frederick Dempsey George Rufus Sutherland', 92 | ['Kiefer William Frederick Dempsey George Rufus', 'Sutherland']], 93 | ['William Hearst', ['William', 'Hearst']], 94 | ['William Hearst Jr', ['William', 'Hearst']], 95 | ['Hearst, William Jr', ['William', 'Hearst']], 96 | ['Hearst, William Randolph', ['William Randolph', 'Hearst']], 97 | ['Hearst, William, M.D.', ['William', 'Hearst']], 98 | ['William', ['William', null]], 99 | ['', [null, null]], 100 | 101 | // from humanname 102 | ["John Doe", ["John", "Doe"]], 103 | ["Mr Anthony R Von Fange III", ["Anthony R", "Von Fange"]], 104 | ["Sara Ann Fraser", ["Sara Ann", "Fraser"]], 105 | ["Adam", ["Adam", null]], 106 | ["Jonathan Smith", ["Jonathan", "Smith"]], 107 | ["Anthony Von Fange III", ["Anthony", "Von Fange"]], 108 | ["Mr John Doe", ["John", "Doe"]], 109 | ["Smarty Pants Phd", ["Smarty", "Pants"]], 110 | ["Mark P Williams", ["Mark P", "Williams"]] 111 | ]; 112 | 113 | describe('full-name-splitter', function() { 114 | expectations.forEach( ([fullName, expected]) => { 115 | it(`${fullName} should split to [${expected[0]}, ${expected[1]}]`, function() { 116 | const split = splitter(fullName); 117 | assert(split[0] == expected[0]); 118 | assert(split[1] == expected[1]); 119 | }); 120 | }); 121 | }); 122 | --------------------------------------------------------------------------------