Skip to content

Commit 9d2a2d9

Browse files
committed
updated the code
1 parent 1d77531 commit 9d2a2d9

File tree

6 files changed

+112
-104
lines changed

6 files changed

+112
-104
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# [shortname-js](https://github.com/moser-jose/shortname-js)
22

3-
`shortname-js` is a JavaScript function designed to shorten names (Portuguese - PT 🇵🇹 | BR 🇧🇷 | AO 🇦🇴) in a simple and user-friendly way. The function allows users to quickly enter full names and receive abbreviated versions of those names in return. 😅
3+
`shortname-js` is a JavaScript function designed to shorten names (Portuguese - PT 🇵🇹 | BR 🇧🇷 | AO 🇦🇴) in a simple and user-friendly way. The function allows users to quickly enter full names and receive abbreviated versions of those names in return.
44

55
[![The MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT) [![The MIT License](https://img.shields.io/github/package-json/v/moser-jose/shortname-js)](https://github.com/moser-jose/shortname-js)
66

7-
8-
97
## Install
108

119
### yarn
@@ -20,18 +18,18 @@
2018

2119
```javascript
2220

23-
const {shortName} = required('@mosmmy/shortname-js')
21+
import shortName = '@mosmmy/shortname-js'
2422

2523
console.log(shortName('Jorge Pedro André dos Santos')) //Jorge P. A. dos Santos
2624

2725
console.log(shortName('Pedro de Sousa André')) //**Pedro** S. André
28-
****
26+
2927
console.log(shortName('Marcos Ângelo Mateus dos Santos')) //Marcos A. M. dos Santos
3028
```
3129

32-
To run the test locally:
30+
To run the test:
3331

34-
`npm run dev`
32+
`npm run test`
3533

3634
## Licence
3735

dist/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export declare const shortName: (fullName: string) => string | undefined;
1+
export default function shortName(fullName: string): string | undefined;

dist/index.js

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
11
"use strict";
22
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])) {
923
penultimate = nameSplit[nameSplit.length - 2];
1024
}
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));
1729
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;
3442
}
35-
if (!flag) {
36-
if (string[0] !== undefined) {
37-
middleName += string[0].toUpperCase() + '. ';
38-
}
43+
if (normalizedName[0]) {
44+
middleName += `${normalizedName[0].toUpperCase()}. `;
3945
}
4046
}
41-
if (flag)
42-
middleName += prep + ' ';
47+
if (lastPreposition) {
48+
middleName += `${lastPreposition} `;
49+
}
4350
}
44-
let first = nameSplit.shift();
45-
let last = nameSplit.pop();
51+
let first = capitalizeWord(nameSplit[0]);
52+
let last = capitalizeWord(nameSplit[nameSplit.length - 1]);
4653
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, "");
5154
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;
5456
}
55-
return first[0].toUpperCase() + first.substring(1).toLowerCase() + middleName +
56-
last[0].toUpperCase() + last.substring(1).toLowerCase();
57+
return first + middleName + last;
5758
}
58-
};
59-
exports.shortName = shortName;
59+
}

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
{
22
"name": "@mosmmy/shortname-js",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "shortname-js is a JavaScript function designed to shorten names in a simple and user-friendly way. The function allows users to quickly enter full names and receive abbreviated versions of those names in return",
55
"displayName": "shortname-js",
66
"main" : "dist/index.js",
77
"types" : "dist/index.d.ts",
88
"files": ["src", "dist", "LICENSE", "README.md", "package.json"],
99
"scripts": {
1010
"build": "tsc --declaration",
11-
"test": "jest",
12-
"prepare": "npm run build"
11+
"test": "jest"
1312
},
1413
"keywords": [
1514
"shortname-js",

src/index.ts

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,65 @@
1-
export const shortName= (fullName: string) => {
1+
const PREPOSITIONS = ['de', 'do', 'dos', 'da', 'das', 'e'];
22

3-
let nameSplit: string[] = fullName.split(' ').filter(Boolean);
4-
nameSplit=nameSplit.map(item => item.replace(/[^a-záàâãéèêíïóôõöúçA-ZÁÀÂÃÉÈÊÍÏÓÔÕÖÚÇ\s]/g, '')).filter(item => item !== '')
5-
let array: string[] = ['de', 'do', 'dos', 'da', 'das', 'e'];
6-
var penultimate: string = '';
3+
const normalized=(name:string):string=>{
4+
return name.normalize("NFD")
5+
.replace(/[\u0300-\u036f]/g, "")
6+
.replace(/[^a-zA-Z\s]/g, "");
7+
}
8+
9+
const capitalizeWord = (word: string): string => {
10+
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
11+
};
12+
13+
export default function shortName(fullName: string):string|undefined{
14+
if (!fullName?.trim()) return undefined;
715

8-
if (array.includes(nameSplit[nameSplit.length - 2])) {
9-
penultimate = nameSplit[nameSplit.length - 2];
10-
}
16+
let nameSplit: string[] = fullName
17+
.split(' ')
18+
.filter(Boolean)
19+
.map(part => part.replace(/[^a-záàâãéèêíïóôõöúçA-ZÁÀÂÃÉÈÊÍÏÓÔÕÖÚÇ\s]/g, ''))
20+
.filter(part => part !== '');
1121

12-
for (let i: number = 0; i < nameSplit.length - 2; i++) {
13-
if (array.includes(nameSplit[i])) {
14-
nameSplit.splice(i, 1);
15-
i--;
16-
}
22+
let penultimate: string = '';
23+
24+
if (PREPOSITIONS.includes(nameSplit[nameSplit.length - 2])) {
25+
penultimate = nameSplit[nameSplit.length - 2];
1726
}
1827

28+
nameSplit = nameSplit
29+
.slice(0, -2)
30+
.filter(word => !PREPOSITIONS.includes(word))
31+
.concat(nameSplit.slice(-2));
32+
1933
let middleName: string = ' ';
20-
if (nameSplit.length > 2) {
21-
let integer: number = 0;
22-
penultimate ? integer=nameSplit.length - 2 : integer=nameSplit.length - 1;
23-
let prep:string =''
24-
let flag:boolean=false
25-
for (let i: number = 1; i < integer; i++) {
26-
let string: string =nameSplit[i].normalize("NFD")
27-
.replace(/[\u0300-\u036f]/g, "")
28-
.replace(/[^a-zA-Z\s]/g, "");
29-
flag=false
30-
for (let j = 0; j < array.length; j++) {
31-
if(string===array[j]){
32-
prep=string
33-
flag=true
34-
break;
35-
}
34+
35+
if (nameSplit.length === 0) return undefined;
36+
if(nameSplit.length===1) return capitalizeWord(normalized(nameSplit[0]))
37+
38+
else if (nameSplit.length > 2) {
39+
const lastIndex = penultimate ? nameSplit.length - 2 : nameSplit.length - 1;
40+
let lastPreposition = '';
41+
42+
for (let i = 1; i < lastIndex; i++) {
43+
const normalizedName = normalized(nameSplit[i])
44+
if (PREPOSITIONS.includes(normalizedName)) {
45+
lastPreposition = normalizedName;
46+
continue;
3647
}
37-
if (!flag) {
38-
if (string[0]!==undefined) {
39-
middleName += string[0].toUpperCase() + '. ';
40-
}
48+
if (normalizedName[0]) {
49+
middleName += `${normalizedName[0].toUpperCase()}. `;
4150
}
42-
4351
}
44-
45-
if(flag) middleName +=prep+' '
52+
if (lastPreposition) {
53+
middleName += `${lastPreposition} `;
54+
}
4655
}
47-
48-
let first: string | undefined = nameSplit.shift();
49-
let last: string | undefined = nameSplit.pop();
56+
let first = capitalizeWord(nameSplit[0]);
57+
let last = capitalizeWord(nameSplit[nameSplit.length - 1]);
58+
5059
if (first && last) {
5160
if (penultimate) {
52-
return first[0].toUpperCase() + first.substring(1).toLowerCase() +
53-
middleName + penultimate + ' ' + last[0].toUpperCase() + last.substring(1).toLowerCase();
61+
return first + middleName + penultimate + ' ' + last;
5462
}
55-
56-
return first[0].toUpperCase() + first.substring(1).toLowerCase() + middleName +
57-
last[0].toUpperCase() + last.substring(1).toLowerCase();
63+
return first + middleName + last;
5864
}
5965
}

src/tests/index.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { shortName } from '../index';
1+
import shortName from '../index';
22

33
describe('shortName', () => {
44
test('should format simple first and last name', () => {
55
expect(shortName('John Doe')).toBe('John Doe');
66
});
77

88
test('should format name with middle name', () => {
9-
expect(shortName('John James Doe')).toBe('John J. Doe');
9+
expect(shortName(' John James Doe ')).toBe('John J. Doe');
1010
});
1111

1212
test('should format name with multiple middle names', () => {
@@ -48,10 +48,15 @@ describe('shortName', () => {
4848
});
4949

5050
test('should handle single name', () => {
51-
expect(shortName('John')).toBeUndefined();
51+
expect(shortName('John')).toBe('John');
5252
});
5353

5454
test('should handle empty string', () => {
5555
expect(shortName('')).toBeUndefined();
5656
});
57+
58+
test('should handle numbers', () => {
59+
expect(shortName('343434343434')).toBeUndefined();
60+
});
61+
5762
});

0 commit comments

Comments
 (0)