Skip to content

Commit

Permalink
feat: Add a util for splitting name
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavrajs committed Nov 28, 2024
1 parent 4a7209d commit 99f3bfe
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"analyze": "size-limit --why",
"docs": "typedoc --theme default --out docs ./src"
},
"peerDependencies": {},
"prettier": {
"printWidth": 80,
"semi": true,
Expand All @@ -44,6 +43,7 @@
],
"devDependencies": {
"@size-limit/preset-small-lib": "^7.0.8",
"@types/jest": "^29.5.14",
"husky": "^8.0.1",
"size-limit": "^8.0.0",
"tsdx": "^0.14.1",
Expand Down
39 changes: 39 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,42 @@ export const fileNameWithEllipsis = (

return `${name.slice(0, maxLength)}${ellipsis}${extension}`;
};

/**
* @name splitName
* @description Splits a full name into firstName and lastName
* @param {string} name - Full name of the user
* @returns {Object} Object with firstName and lastName
* @example
* splitName('Mary Jane Smith') // { firstName: 'Mary Jane', lastName: 'Smith' }
* splitName('Alice') // { firstName: 'Alice', lastName: '' }
* splitName('John Doe') // { firstName: 'John', lastName: 'Doe' }
* splitName('') // { firstName: '', lastName: '' }
*/
export const splitName = (fullName: string): { firstName: string; lastName: string } => {

Check failure on line 165 in src/helpers.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `fullName:·string` with `⏎··fullName:·string⏎`

Check failure on line 165 in src/helpers.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and macOS-latest

Replace `fullName:·string` with `⏎··fullName:·string⏎`
const trimmedName = fullName.trim();
if (!trimmedName) {
return {
firstName: '',
lastName: ''

Check failure on line 170 in src/helpers.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Insert `,`

Check failure on line 170 in src/helpers.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and macOS-latest

Insert `,`
};
}

// Split the name by spaces
const nameParts = trimmedName.split(/\s+/);

// If only one word, treat it as firstName
if (nameParts.length === 1) {
return {
firstName: nameParts[0],
lastName: ''

Check failure on line 181 in src/helpers.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Insert `,`

Check failure on line 181 in src/helpers.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and macOS-latest

Insert `,`
};
}

// Last element is lastName, everything else is firstName
const lastName = nameParts.pop() || '';
const firstName = nameParts.join(' ');

return { firstName, lastName

Check failure on line 189 in src/helpers.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `⏎` with `·`

Check failure on line 189 in src/helpers.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and macOS-latest

Replace `⏎` with `·`
};
}

Check failure on line 191 in src/helpers.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Insert `;`

Check failure on line 191 in src/helpers.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and macOS-latest

Insert `;`
33 changes: 32 additions & 1 deletion test/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertSecondsToTimeUnit, fileNameWithEllipsis } from '../src/helpers';
import { convertSecondsToTimeUnit, fileNameWithEllipsis, splitName } from '../src/helpers';

Check failure on line 1 in test/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `·convertSecondsToTimeUnit,·fileNameWithEllipsis,·splitName·` with `⏎··convertSecondsToTimeUnit,⏎··fileNameWithEllipsis,⏎··splitName,⏎`

Check failure on line 1 in test/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and macOS-latest

Replace `·convertSecondsToTimeUnit,·fileNameWithEllipsis,·splitName·` with `⏎··convertSecondsToTimeUnit,⏎··fileNameWithEllipsis,⏎··splitName,⏎`

describe('#convertSecondsToTimeUnit', () => {
it("it should return { time: 1, unit: 'm' } if 60 seconds passed", () => {
Expand Down Expand Up @@ -93,4 +93,35 @@ describe('fileNameWithEllipsis', () => {
const file = { name: 'a.txt' };
expect(fileNameWithEllipsis(file)).toBe('a.txt');
});

Check failure on line 96 in test/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Delete `⏎`

Check failure on line 96 in test/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and macOS-latest

Delete `⏎`
});

describe('splitName', () => {
it('splits a basic first and last name', () => {
expect(splitName('John Doe')).toEqual({
firstName: 'John',
lastName: 'Doe'

Check failure on line 103 in test/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Insert `,`

Check failure on line 103 in test/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and macOS-latest

Insert `,`
});
});

it('handles single name', () => {
expect(splitName('John')).toEqual({
firstName: 'John',
lastName: ''

Check failure on line 110 in test/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Insert `,`

Check failure on line 110 in test/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and macOS-latest

Insert `,`
});
});

it('handles empty string', () => {
expect(splitName('Mary John Ann')).toEqual({
firstName: 'Mary John',
lastName: 'Ann'

Check failure on line 117 in test/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Insert `,`

Check failure on line 117 in test/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and macOS-latest

Insert `,`
});
});

it('handles extra whitespace', () => {
expect(splitName(' Jane Doe ')).toEqual({
firstName: 'Jane',
lastName: 'Doe'
});
});
});
Loading

0 comments on commit 99f3bfe

Please sign in to comment.