|
1 | 1 | "use strict"; |
2 | 2 | Object.defineProperty(exports, "__esModule", { value: true }); |
3 | | -exports.shortName = void 0; |
4 | | -const shortName = (fullName) => { |
5 | | - let nameSplit = fullName.split(' ').filter(Boolean); |
6 | | - let array = ['de', 'do', 'dos', 'da', 'das', 'e']; |
7 | | - var penultimate = ''; |
8 | | - if (array.includes(nameSplit[nameSplit.length - 2])) { |
| 3 | +exports.default = shortName; |
| 4 | +const PREPOSITIONS = ['de', 'do', 'dos', 'da', 'das', 'e']; |
| 5 | +const normalized = (name) => { |
| 6 | + return name.normalize("NFD") |
| 7 | + .replace(/[\u0300-\u036f]/g, "") |
| 8 | + .replace(/[^a-zA-Z\s]/g, ""); |
| 9 | +}; |
| 10 | +const capitalizeWord = (word) => { |
| 11 | + return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); |
| 12 | +}; |
| 13 | +function shortName(fullName) { |
| 14 | + if (!fullName?.trim()) |
| 15 | + return undefined; |
| 16 | + let nameSplit = fullName |
| 17 | + .split(' ') |
| 18 | + .filter(Boolean) |
| 19 | + .map(part => part.replace(/[^a-záàâãéèêíïóôõöúçA-ZÁÀÂÃÉÈÊÍÏÓÔÕÖÚÇ\s]/g, '')) |
| 20 | + .filter(part => part !== ''); |
| 21 | + let penultimate = ''; |
| 22 | + if (PREPOSITIONS.includes(nameSplit[nameSplit.length - 2])) { |
9 | 23 | penultimate = nameSplit[nameSplit.length - 2]; |
10 | 24 | } |
11 | | - for (let i = 0; i < nameSplit.length - 2; i++) { |
12 | | - if (array.includes(nameSplit[i])) { |
13 | | - nameSplit.splice(i, 1); |
14 | | - i--; |
15 | | - } |
16 | | - } |
| 25 | + nameSplit = nameSplit |
| 26 | + .slice(0, -2) |
| 27 | + .filter(word => !PREPOSITIONS.includes(word)) |
| 28 | + .concat(nameSplit.slice(-2)); |
17 | 29 | let middleName = ' '; |
18 | | - if (nameSplit.length > 2) { |
19 | | - let integer = 0; |
20 | | - penultimate ? integer = nameSplit.length - 2 : integer = nameSplit.length - 1; |
21 | | - let prep = ''; |
22 | | - let flag = false; |
23 | | - for (let i = 1; i < integer; i++) { |
24 | | - let string = nameSplit[i].normalize("NFD") |
25 | | - .replace(/[\u0300-\u036f]/g, "") |
26 | | - .replace(/[^a-zA-Z\s]/g, ""); |
27 | | - flag = false; |
28 | | - for (let j = 0; j < array.length; j++) { |
29 | | - if (string === array[j]) { |
30 | | - prep = string; |
31 | | - flag = true; |
32 | | - break; |
33 | | - } |
| 30 | + if (nameSplit.length === 0) |
| 31 | + return undefined; |
| 32 | + if (nameSplit.length === 1) |
| 33 | + return capitalizeWord(normalized(nameSplit[0])); |
| 34 | + else if (nameSplit.length > 2) { |
| 35 | + const lastIndex = penultimate ? nameSplit.length - 2 : nameSplit.length - 1; |
| 36 | + let lastPreposition = ''; |
| 37 | + for (let i = 1; i < lastIndex; i++) { |
| 38 | + const normalizedName = normalized(nameSplit[i]); |
| 39 | + if (PREPOSITIONS.includes(normalizedName)) { |
| 40 | + lastPreposition = normalizedName; |
| 41 | + continue; |
34 | 42 | } |
35 | | - if (!flag) { |
36 | | - if (string[0] !== undefined) { |
37 | | - middleName += string[0].toUpperCase() + '. '; |
38 | | - } |
| 43 | + if (normalizedName[0]) { |
| 44 | + middleName += `${normalizedName[0].toUpperCase()}. `; |
39 | 45 | } |
40 | 46 | } |
41 | | - if (flag) |
42 | | - middleName += prep + ' '; |
| 47 | + if (lastPreposition) { |
| 48 | + middleName += `${lastPreposition} `; |
| 49 | + } |
43 | 50 | } |
44 | | - let first = nameSplit.shift(); |
45 | | - let last = nameSplit.pop(); |
| 51 | + let first = capitalizeWord(nameSplit[0]); |
| 52 | + let last = capitalizeWord(nameSplit[nameSplit.length - 1]); |
46 | 53 | if (first && last) { |
47 | | - first = first |
48 | | - .replace(/[^a-záàâãéèêíïóôõöúçñA-ZÁÀÂÃÉÈÊÍÏÓÔÕÖÚÇÑ\s]/g, ""); |
49 | | - last = last |
50 | | - .replace(/[^a-záàâãéèêíïóôõöúçñA-ZÁÀÂÃÉÈÊÍÏÓÔÕÖÚÇÑ\s]/g, ""); |
51 | 54 | if (penultimate) { |
52 | | - return first[0].toUpperCase() + first.substring(1).toLowerCase() + |
53 | | - middleName + penultimate + ' ' + last[0].toUpperCase() + last.substring(1).toLowerCase(); |
| 55 | + return first + middleName + penultimate + ' ' + last; |
54 | 56 | } |
55 | | - return first[0].toUpperCase() + first.substring(1).toLowerCase() + middleName + |
56 | | - last[0].toUpperCase() + last.substring(1).toLowerCase(); |
| 57 | + return first + middleName + last; |
57 | 58 | } |
58 | | -}; |
59 | | -exports.shortName = shortName; |
| 59 | +} |
0 commit comments