Skip to content

Commit

Permalink
Update eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavrajs committed Nov 28, 2024
1 parent 70b947a commit 1ac9e23
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
11 changes: 6 additions & 5 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ export const fileNameWithEllipsis = (
* splitName('John Doe') // { firstName: 'John', lastName: 'Doe' }
* splitName('') // { firstName: '', lastName: '' }
*/
export const splitName = (fullName: string): { firstName: string; lastName: string } => {
export const splitName = (
fullName: string
): { firstName: string; lastName: string } => {
const trimmedName = fullName.trim();
if (!trimmedName) {
return {
firstName: '',
lastName: ''
lastName: '',
};
}

Expand All @@ -178,14 +180,13 @@ export const splitName = (fullName: string): { firstName: string; lastName: stri
if (nameParts.length === 1) {
return {
firstName: nameParts[0],
lastName: ''
lastName: '',
};
}

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

return { firstName, lastName
return { firstName, lastName };
};
}
15 changes: 9 additions & 6 deletions test/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { convertSecondsToTimeUnit, fileNameWithEllipsis, splitName } from '../src/helpers';
import {
convertSecondsToTimeUnit,
fileNameWithEllipsis,
splitName,
} from '../src/helpers';

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

});

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

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

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

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

0 comments on commit 1ac9e23

Please sign in to comment.