Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/string/acronymize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, expect, it } from 'vitest';
import { acronymize } from './acronymize';

describe('acronymize', () => {
it('should work with an empty string', () => {
expect(acronymize('')).toBe('');
expect(acronymize(' ')).toBe('');
});

it('should work with just a single word', () => {
expect(acronymize('hello')).toBe('H');
});

it('should form a word containing only first letter of each word', () => {
expect(acronymize('Portable Network Graphics')).toBe('PNG');
});

it('should always output acronym in all uppercase', () => {
expect(acronymize('best friend forever')).toBe('BFF');
expect(acronymize('manchester United football Club')).toBe('MUFC');
});

it('should work with words with mixed separators', () => {
expect(acronymize('HyperText Markup Language')).toBe('HTML');
expect(acronymize('Catherine Zeta-Jones')).toBe('CZJ');
expect(acronymize('J. K. Rowling')).toBe('JKR');
});

it('should work with strings with trailing whitespaces', () => {
expect(acronymize(' Random Access Memory ')).toBe('RAM');
});

it('should work with words that start with numbers', () => {
expect(acronymize('3D Graphics Library')).toBe('3DGL');
});

it('should ignore special characters', () => {
expect(acronymize('@Home !Network')).toBe('HN');
});

it('should ignore emojis', () => {
expect(acronymize('😀 Happy Face')).toBe('HF');
});

it('should work with non-BMP letters', () => {
expect(acronymize('𐐷𐐲𐑉𐑌𐐻 𐐹𐐲𐑉𐑁𐐭𐑋𐑌𐑅')).toBe('𐐏𐐑');
expect(acronymize('𐐷eseret 𐐹erformance')).toBe('𐐏𐐑');
});

it('should preserve non-BMP uppercase letters in acronyms', () => {
expect(acronymize('𝔘nicode 𝐓esting')).toBe('𝔘𝐓');
});
});
28 changes: 28 additions & 0 deletions src/string/acronymize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { words as getWords } from './words.ts';

/**
* Converts a string to an acronym.
*
* An acronym is a word formed from the initial letters of a phrase, with each letter capitalized.
*
* @param {string} str - The string to be converted to an acronym.
* @returns {string} - The acronymized version of the input string.
*
* @example
* const acronym1 = acronymize('Portable Network Graphics'); // returns 'PNG'
* const acronym2 = acronymize('HyperText Markup Language'); // returns 'HTML'
* const acronym3 = acronymize('As Soon As Possible'); // returns 'ASAP'
* const acronym4 = acronymize('Federal Bureau of Investigation'); // returns 'FBI'
*/
Comment thread
arimotearipo marked this conversation as resolved.

const INITIAL_LETTER_OR_NUMBER = /^[\p{L}\p{N}]/u;

export function acronymize(str: string): string {
const words = getWords(str);
return words
.map(word => {
const [initial = ''] = Array.from(word);
return INITIAL_LETTER_OR_NUMBER.test(initial) ? initial.toUpperCase() : '';
})
.join('');
}
1 change: 1 addition & 0 deletions src/string/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { acronymize } from './acronymize.ts';
export { camelCase } from './camelCase.ts';
export { capitalize } from './capitalize.ts';
export { constantCase } from './constantCase.ts';
Expand Down
Loading