Skip to content

Commit 6069597

Browse files
committed
Merge pull request #6 from cbojar/master
Quite a few changes
2 parents 15bf4a8 + 5943d07 commit 6069597

File tree

4 files changed

+322
-130
lines changed

4 files changed

+322
-130
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ JavaScript library to split names into their respective components (first, last,
22

33
**Usage:**
44

5-
var parser = new NameParse();
6-
var parsed = parser.parse("Mr. Chales P. Wooten, III");
5+
var parsed = NameParse.parse("Mr. Chales P. Wooten, III");
76

87
**Results:**
98

browser-shims.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
if (!String.prototype.trim) {
2+
String.prototype.trim = function () {
3+
return this.replace(/^\s+|\s+$/gm, '');
4+
};
5+
}
6+
7+
if (!Array.prototype.indexOf) {
8+
Array.prototype.indexOf = function (searchElement, fromIndex) {
9+
if ( this === undefined || this === null ) {
10+
throw new TypeError( '"this" is null or not defined' );
11+
}
12+
13+
var length = this.length >>> 0; // Hack to convert object.length to a UInt32
14+
15+
fromIndex = +fromIndex || 0;
16+
17+
if (Math.abs(fromIndex) === Infinity) {
18+
fromIndex = 0;
19+
}
20+
21+
if (fromIndex < 0) {
22+
fromIndex += length;
23+
if (fromIndex < 0) {
24+
fromIndex = 0;
25+
}
26+
}
27+
28+
for (;fromIndex < length; fromIndex++) {
29+
if (this[fromIndex] === searchElement) {
30+
return fromIndex;
31+
}
32+
}
33+
34+
return -1;
35+
};
36+
}
37+
38+
if (!Array.prototype.filter)
39+
{
40+
Array.prototype.filter = function(fun /*, thisArg */)
41+
{
42+
if (this === undefined || this === null)
43+
throw new TypeError();
44+
45+
var t = Object(this);
46+
var len = t.length >>> 0;
47+
if (typeof fun !== "function")
48+
throw new TypeError();
49+
50+
var res = [];
51+
var thisArg = arguments.length >= 2 ? arguments[1] : undefined;
52+
for (var i = 0; i < len; i++)
53+
{
54+
if (i in t)
55+
{
56+
var val = t[i];
57+
58+
// NOTE: Technically this should Object.defineProperty at
59+
// the next index, as push can be affected by
60+
// properties on Object.prototype and Array.prototype.
61+
// But that method's new, and collisions should be
62+
// rare, so use the more-compatible alternative.
63+
if (fun.call(thisArg, val, i, t))
64+
res.push(val);
65+
}
66+
}
67+
68+
return res;
69+
};
70+
}
71+
72+
if (!Array.prototype.map)
73+
{
74+
Array.prototype.map = function(fun /*, thisArg */)
75+
{
76+
if (this === undefined || this === null)
77+
throw new TypeError();
78+
79+
var t = Object(this);
80+
var len = t.length >>> 0;
81+
if (typeof fun !== "function")
82+
throw new TypeError();
83+
84+
var res = new Array(len);
85+
var thisArg = arguments.length >= 2 ? arguments[1] : undefined;
86+
for (var i = 0; i < len; i++)
87+
{
88+
// NOTE: Absolute correctness would demand Object.defineProperty
89+
// be used. But this method is fairly new, and failure is
90+
// possible only if Object.prototype or Array.prototype
91+
// has a property |i| (very unlikely), so use a less-correct
92+
// but more portable alternative.
93+
if (i in t)
94+
res[i] = fun.call(thisArg, t[i], i, t);
95+
}
96+
97+
return res;
98+
};
99+
}

parse-names-test.html

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<!DOCTYPE html>
2+
<html lang="en-us">
3+
<head>
4+
<title>Javascript Name Parser Unit Tests</title>
5+
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.14.0.css" />
6+
</head>
7+
<body>
8+
<div id="qunit"></div>
9+
<div id="qunit-fixture"></div>
10+
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
11+
<script src="browser-shims.js"></script>
12+
<script src="parse-names.js"></script>
13+
<script>
14+
var names = [
15+
{
16+
name: "John Doe",
17+
result: {
18+
"salutation": "",
19+
"firstName": "John",
20+
"initials": "",
21+
"lastName": "Doe",
22+
"suffix": ""
23+
}
24+
}, {
25+
name: "Mr Anthony R Von Fange III",
26+
result: {
27+
"salutation": "Mr.",
28+
"firstName": "Anthony",
29+
"initials": "R",
30+
"lastName": "Von Fange",
31+
"suffix": "III"
32+
}
33+
}, {
34+
name: "Sara Ann Fraser",
35+
result: {
36+
"salutation": "",
37+
"firstName": "Sara Ann",
38+
"initials": "",
39+
"lastName": "Fraser",
40+
"suffix": ""
41+
}
42+
}, {
43+
name: "Adam",
44+
result: {
45+
"salutation": "",
46+
"firstName": "Adam",
47+
"initials": "",
48+
"lastName": "",
49+
"suffix": ""
50+
}
51+
}, {
52+
name: "Jonathan Smith",
53+
result: {
54+
"salutation": "",
55+
"firstName": "Jonathan",
56+
"initials": "",
57+
"lastName": "Smith",
58+
"suffix": ""
59+
}
60+
}, {
61+
name: "Anthony Von Fange III",
62+
result: {
63+
"salutation": "",
64+
"firstName": "Anthony",
65+
"initials": "",
66+
"lastName": "Von Fange",
67+
"suffix": "III"
68+
}
69+
}, {
70+
name: "Mr John Doe",
71+
result: {
72+
"salutation": "Mr.",
73+
"firstName": "John",
74+
"initials": "",
75+
"lastName": "Doe",
76+
"suffix": ""
77+
}
78+
}, {
79+
name: "Smarty Pants Phd",
80+
result: {
81+
"salutation": "",
82+
"firstName": "Smarty",
83+
"initials": "",
84+
"lastName": "Pants",
85+
"suffix": "PhD"
86+
}
87+
}, {
88+
name: "Mark P Williams",
89+
result: {
90+
"salutation": "",
91+
"firstName": "Mark",
92+
"initials": "P",
93+
"lastName": "Williams",
94+
"suffix": ""
95+
}
96+
}];
97+
98+
test('Test names', function() {
99+
names.forEach(function(testCase){
100+
console.log("Check Name: %s", testCase.name);
101+
var parsed = NameParse.parse(testCase.name);
102+
propEqual(parsed, testCase.result);
103+
});
104+
});
105+
</script>
106+
</body>
107+
</html>

0 commit comments

Comments
 (0)