-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (34 loc) · 1.94 KB
/
index.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
module.exports.looseCompare = function validateName(source, input) {
// check to make sure input name isn't too different from existing
if (!source.name) return true;
if (!input.name) return false;
var cleanInputName = clean(input.name);
var cleanSourceName = clean(source.name);
var maxLength = Math.min(cleanInputName.length, cleanSourceName.length);
if (cleanInputName.slice(0, maxLength) === cleanSourceName.slice(0, maxLength)) return true;
if (cleanInputName.slice(0, 3) === cleanSourceName.slice(0, 3) && cleanInputName.slice(-3) === cleanSourceName.slice(-3)) return true;
// names are different - need to parse
var sLastName = lastClean(source);
var sFirstName = firstClean(source);
var iFirstName = firstClean(input);
var iLastName = lastClean(input);
console.log('comparing names: %s %s %s %s', sFirstName, sLastName, iFirstName, iLastName);
if (sFirstName.length === 1 && sFirstName[0] === iFirstName[0] && sLastName === iLastName) return true;
if (iFirstName.length === 1 && sFirstName[0] === iFirstName[0] && sLastName === iLastName) return true;
if (sFirstName === iFirstName && iLastName === sLastName) return true;
if (sLastName.length < 2 && sFirstName === iFirstName && iLastName[0] === sLastName[0]) return true;
if (sLastName.slice(0, 3) === iLastName.slice(0, 3) && sFirstName.slice(0, 3) === sLastName.slice(0, 3)) return true;
if (sLastName.slice(0, 3) === iLastName.slice(0, 3) && sFirstName.slice(0, 3) === sLastName.slice(0, 3)) return true;
// also accept if last and first are reversed
if (sLastName.slice(0, 3) === iFirstName.slice(0, 3) && sFirstName.slice(0, 3) === iLastName.slice(0, 3)) return true;
return false;
};
function firstClean(input) {
return clean(input.firstName || input.name.split(' ')[0]);
}
function lastClean(input) {
return clean(input.lastName || input.name.split(' ').slice(-1)[0]);
}
function clean(inputS) {
return inputS.split(',')[0].replace(/[ ."]/g, '').toLowerCase();
}