Skip to content

Commit

Permalink
feat: Add a util for splitting name (#36)
Browse files Browse the repository at this point in the history
* feat: Add a util for splitting name

* v0.0.29

* Update eslint

* Update export

* v0.0.30
  • Loading branch information
pranavrajs authored Dec 3, 2024
1 parent 4a7209d commit 72e7abf
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chatwoot/utils",
"version": "0.0.28",
"version": "0.0.30",
"description": "Chatwoot utils",
"private": false,
"license": "MIT",
Expand All @@ -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
40 changes: 40 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,43 @@ 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 } => {
const trimmedName = fullName.trim();
if (!trimmedName) {
return {
firstName: '',
lastName: '',
};
}

// 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: '',
};
}

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

return { firstName, lastName };
};
24 changes: 13 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
trimContent,
convertSecondsToTimeUnit,
fileNameWithEllipsis,
splitName,
} from './helpers';

import { parseBoolean } from './string';
Expand All @@ -21,21 +22,22 @@ import { createTypingIndicator } from './typingStatus';
import { evaluateSLAStatus } from './sla';

export {
clamp,
convertSecondsToTimeUnit,
createTypingIndicator,
debounce,
formatTime,
evaluateSLAStatus,
fileNameWithEllipsis,
formatDate,
formatTime,
getContrastingTextColor,
trimContent,
getMessageVariables,
getQuantileIntervals,
getUndefinedVariablesInMessage,
parseBoolean,
sortAsc,
quantile,
clamp,
getQuantileIntervals,
getMessageVariables,
replaceVariablesInMessage,
getUndefinedVariablesInMessage,
createTypingIndicator,
convertSecondsToTimeUnit,
fileNameWithEllipsis,
evaluateSLAStatus,
sortAsc,
splitName,
trimContent,
};
36 changes: 35 additions & 1 deletion test/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { convertSecondsToTimeUnit, fileNameWithEllipsis } 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 @@ -94,3 +98,33 @@ describe('fileNameWithEllipsis', () => {
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',
});
});

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

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

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

0 comments on commit 72e7abf

Please sign in to comment.