unique-abbr
v2.10.0
Published
A simple TypeScript utility to generate unique three-letter abbreviations based on names, ensuring no duplicates from a given list.
Maintainers
Readme
Unique Abbr
A simple TypeScript utility to generate unique three-letter abbreviations based on names, ensuring no duplicates from a given list.
Installation
You can install unique-abbr via npm:
npm install unique-abbror using yarn:
yarn add unique-abbrUsage
Import the function and generate unique abbreviations:
Basic Example
import { uniqueAbbr } from 'unique-abbr';
const existingCodes = ['ABC', 'DEF', 'XYZ'];
const abbreviation = uniqueAbbr('John Doe', existingCodes);
console.log(abbreviation); // Example output: 'JD' or 'JOH'Custom Abbreviation Length
You can specify the abbreviation length:
const abbreviation = uniqueAbbr('Alice Wonderland', existingCodes, 4);
console.log(abbreviation); // Example output: 'ALWO'API
uniqueAbbr(name: string, existingCodes: string[], length: number = 3): string
Generates a unique abbreviation from a name, avoiding duplicates.
Parameters:
name(string) - The name to generate an abbreviation for.existingCodes(string[]) - List of existing abbreviations to avoid duplicates.length(number, optional, default:3) - The desired abbreviation length.
Returns:
- A string containing the unique abbreviation.
Examples
Handling Duplicate Abbreviations
If an abbreviation is already in use, it tries to create a unique variant:
const existingCodes = ['JD', 'JOH'];
const abbreviation = uniqueAbbr('John Doe', existingCodes);
console.log(abbreviation); // Example output: 'JO1'Edge Cases
uniqueAbbr('12345', []); // Ignores non-alphabetic characters
uniqueAbbr('A B C', []); // Returns 'ABC' or variationContributing
Feel free to submit issues or pull requests on GitHub.
License
This project is licensed under the MIT License - see the LICENSE file for details.
