Skip to content

Commit

Permalink
Merge branch 'main' into add-audit-details-to-lighthouse-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Jun 20, 2024
2 parents 39ef8d6 + 8d6dccb commit ef592b9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
33 changes: 30 additions & 3 deletions packages/utils/src/lib/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,39 @@ export function formatDate(date: Date): string {
.replace(/\u202F/g, ' '); // see https://github.com/nodejs/node/issues/45171
}

export function truncateText(text: string, maxChars: number): string {
export function truncateText(
text: string,
options:
| number
| {
maxChars: number;
position?: 'start' | 'middle' | 'end';
ellipsis?: string;
},
): string {
const {
maxChars,
position = 'end',
ellipsis = '...',
} = typeof options === 'number' ? { maxChars: options } : options;
if (text.length <= maxChars) {
return text;
}
const ellipsis = '...';
return text.slice(0, maxChars - ellipsis.length) + ellipsis;

const maxLength = maxChars - ellipsis.length;
switch (position) {
case 'start':
return ellipsis + text.slice(-maxLength).trim();
case 'middle':
const halfMaxChars = Math.floor(maxLength / 2);
return (
text.slice(0, halfMaxChars).trim() +
ellipsis +
text.slice(-halfMaxChars).trim()
);
case 'end':
return text.slice(0, maxLength).trim() + ellipsis;
}
}

export function truncateTitle(text: string): string {
Expand Down
44 changes: 40 additions & 4 deletions packages/utils/src/lib/formatting.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,55 @@ describe('formatDate', () => {
});

describe('truncateText', () => {
it('should replace overflowing text with ellipsis', () => {
it('should replace overflowing text with ellipsis at the end', () => {
expect(truncateText('All work and no play makes Jack a dull boy', 32)).toBe(
'All work and no play makes Ja...',
);
});

it('should produce truncated text which fits within limit', () => {
it('should leave text unchanged when within character limit passed as number', () => {
expect(truncateText("Here's Johnny!", 32)).toBe("Here's Johnny!");
});

it('should produce truncated text which fits within limit passed as number', () => {
expect(
truncateText('All work and no play makes Jack a dull boy', 32).length,
).toBeLessThanOrEqual(32);
});

it('should leave text unchanged when within character limit', () => {
expect(truncateText("Here's Johnny!", 32)).toBe("Here's Johnny!");
it('should leave text unchanged when within character limit passed as options', () => {
expect(truncateText("Here's Johnny!", { maxChars: 32 })).toBe(
"Here's Johnny!",
);
});

it('should produce truncated text with ellipsis at the start', () => {
expect(
truncateText('Yesterday cloudy day.', {
maxChars: 10,
position: 'start',
}),
).toBe('...dy day.');
});

it('should produce truncated text with ellipsis at the middle', () => {
expect(
truncateText('Horrendous amounts of lint issues are present Tony!', {
maxChars: 10,
position: 'middle',
}),
).toBe('Hor...ny!');
});

it('should produce truncated text with ellipsis at the end', () => {
expect(truncateText("I'm Johnny!", { maxChars: 10, position: 'end' })).toBe(
"I'm Joh...",
);
});

it('should produce truncated text with custom ellipsis', () => {
expect(truncateText("I'm Johnny!", { maxChars: 10, ellipsis: '*' })).toBe(
"I'm Johnn*",
);
});
});

0 comments on commit ef592b9

Please sign in to comment.