-
Notifications
You must be signed in to change notification settings - Fork 0
/
nameFormatFinal.js
207 lines (190 loc) · 7 KB
/
nameFormatFinal.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
var regex_No_Periods = /"\\.+", ";"/g;
var regex_period_first_letter = (/^([a-zA-Z])([ ])/, (letter) => letter.toUpperCase());
var regex_all_two_letter_words = /(\b\w{2}\b)/g;
var regex_all_one_letter_words = /(\b\w{1}\b)/g;
var regex_isolate_2lett = /\b([a-zA-Z])([a-zA-Z])\b/g;
var regexCapitalize = /[A-Z]+/g;
var regex_lowercase_last_letter = /(\w)(\W*)$/;
var regex_is_letter = /^[a-z]/i;
var regex_is_letter_or_space = /^[a-zA-Z\s]+$/i;
var regex_extra_space_check = /^[A-Za-z]+( [A-Za-z]+)*$/;
var regex_remove_consec_spaces = / +/g;
var regex_remove_beginning_and_end_spaces = /^\s+|\s+$/;
const isUpperCase = (string) => /^[A-Z]*$/.test(string);
var regex_remove_periods_from_dash_words = /\b[^-\s]+-.+[^-\s]+\b/g;
var testString = [
"j handcock",
"je handcock",
"j.y. handcock",
"jim handcock",
"jim j handcock",
"jim je handcock,",
"jim jer handcock",
"jo jenny handcock",
"jo jim le handcock",
"j handcock",
"j. e, handcock",
"j. er handcock jr",
" jim js handcock sr",
" jim. handcock",
"jim handcock nn ",
"bo bo bice",
"J.K Handcock",
"Ro RO Jones",
"Ruby Jo Tarrent",
"Ruby-Jo Mellie",
"D.j. hemmingway",
"Dj hemmingway",
"Dr. John Smith",
"John Dr. Smith",
"Dr John Smith",
"DR. H. J. smith",
"Dr. H.J. smith",
];
const nameProcess = (array) => {
const newArray = array.map((str) => {
// limit all spaces to single spaces and remove spaces at beginning and end of string
const consecSpaceChecked = str.replace(regex_remove_consec_spaces, " ");
const spaceChecked = consecSpaceChecked.replace(regex_remove_beginning_and_end_spaces, "");
//uppercase all 2 letter words. always lowercase the last letter
var initStr = spaceChecked
.split(" ")
.map((i) => (i.replaceAll(/[^a-z]/gi, "").length === 2 ? i.toUpperCase() : i))
.join(" ")
.replace(regex_lowercase_last_letter, function (match, capture1, capture2) {
return capture1.toLowerCase() + capture2;
});
// remove dr, Dr, dr. and Dr, from the beginning of the name.
const noTitleFunct = (initStr) => {
if (initStr.toLowerCase().slice(0, 3) === "dr.") {
return initStr.replace(/dr./gi, "");
} else if (initStr.toLowerCase().slice(0, 2) === "dr") {
return initStr.slice(2, initStr.length);
} else {
return initStr;
}
};
const noTitle = noTitleFunct(initStr);
//remove all periods and commas
var firstStr = noTitle.replaceAll(".", "").replaceAll(",", "");
//add periods to all 2 letter words
var secndStr = firstStr.replace(regex_all_two_letter_words, "$1.");
//add periods to all 1 letter words
var thirdStr = secndStr.replace(regex_all_one_letter_words, "$1.");
//add periods between all 2 letter words that are not the last word
var fourthStr = thirdStr.replace(regex_isolate_2lett, "$1.$2");
// create exceptions for jr and sr
var fifthStr = fourthStr.replaceAll("j.r.", ", jr.").replaceAll("s.r.", ", sr.");
//remove periods from the center of last 2 letter word
const sixthStringFunct = (string) => {
if (string[string.length - 3] === ".") {
const partOne = string.slice(0, -3);
const partTwo = string.slice(-2, -1);
string = partOne + partTwo;
return string;
}
return string;
};
const sixthString = sixthStringFunct(fifthStr);
//capitalize first letter of each word
const seventhString = sixthString.replace(/(^\w{1})|(\s+\w{1})/g, (letter) => letter.toUpperCase());
///// Final String Manilpulation
//check if str first name of two characters includes a period
const initCheck1 = noTitle[1] === "." ? true : false;
const initCheck2 = noTitle[3] === "." ? true : false;
const initCheckNone = !initCheck1 && !initCheck2 ? true : false;
const initCheckAll = initCheck1 && initCheck2 ? true : false;
const twoLettrWordBegin = noTitle[0] !== " " && noTitle[1] !== " " && noTitle[2] == " " ? true : false;
const eighthStringFunct = (eighth_sttring) => {
if (initCheckNone) {
// Assumes a 2letter first name ie: "Bo"
// chck if the the string begins with a 2 letter word
var text = eighth_sttring;
if (twoLettrWordBegin) {
// remove periods from the first name
text = eighth_sttring.slice(0, 1) + eighth_sttring.substr(2);
text = text.slice(0, 2) + text.substr(3);
// console.log("none, 2lett, ", text);
text = text[0] + text[1].toLowerCase() + text.slice(2);
return text;
} else {
var text = eighth_sttring;
// console.log("none ", text);
return text;
}
} else if (initCheckAll) {
// All Assumes a acronym first name: ie: "D.J." None inputs capture inputs like "david Williams or bo bo bice"
// do nothing as the name is already formatted correctly either with or without periods.
var text = eighth_sttring;
// console.log("all", text);
return text;
} else if (initCheck1) {
//user input something like "J. HANDCOCK or J.R Handcock"
// if 4th letter[3] of str is a space, add a space before the 3rd letter[2] in text
if (noTitle[2] !== " ") {
//this means that the second letter is a period and the fourth letter is a space. ie: "J.S Handcock"
// assume spelling error (first initial, middle initial) and add a space before the 3rd letter
var text = eighth_sttring.slice(0, 2) + " " + eighth_sttring.substr(2);
// console.log("init1, sp !=", text, str);
return text;
} else {
// else if 3rd letter[2] of str is a space, add a space before the 2nd letter[1] in text
var text = eighth_sttring;
// console.log("init1, sp =", text, str);
return text;
}
} else if (initCheck2) {
//user input something like "Jus. HANDCOCK
var text = eighth_sttring;
// console.log("init2", text);
return text;
} else {
// user didn't input a name or the name was less than 3 characters
console.log("err", text);
return eighth_sttring;
}
};
const eighthString = eighthStringFunct(seventhString);
const ninthString = (text) => {
text.replace(/\b[a-zA-Z]+-[a-zA-Z]+.[a-zA-Z]+.+\b/g, function (word) {
return word.replace(/\./g, "");
});
return text;
};
const tenthString = ninthString(eighthString)
.replace(/ jr\.?$/i, ", Jr.")
.replace(/ sr\.?$/i, ", Sr.");
return tenthString;
});
return newArray;
};
console.log(
"length: ",
nameProcess(testString).length,
testString.map((v) => v),
nameProcess(testString)
);
const newData = nameProcess(testString);
const oldData = testString;
var newList = document.getElementById("new");
var oldList = document.getElementById("old");
var testInput = document.getElementById("testInput");
var output = document.getElementById("output");
var formatBtn = document.getElementById("formatBtn");
oldData.forEach((item) => {
let li = document.createElement("li");
li.innerText = item;
oldList.appendChild(li);
});
newData.forEach((item) => {
let li = document.createElement("li");
li.innerText = item;
newList.appendChild(li);
});
testInput.addEventListener("keyup", (e) => {
const input = e.target.value;
output.innerText = nameProcess([input])[0];
});
formatBtn.addEventListener("click", (e) => {
testInput.value = nameProcess([testInput.value])[0];
});