-
Notifications
You must be signed in to change notification settings - Fork 544
feat: add acronymize function to string utilities #1690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arimotearipo
wants to merge
1
commit into
toss:main
Choose a base branch
from
arimotearipo:feat/acronymize
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+82
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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('𝔘𝐓'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | ||
| */ | ||
|
|
||
| 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(''); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.