Package Exports
- unique-abbr
- unique-abbr/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (unique-abbr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
UniqueAbbr
Overview
uniqueAbbr is a utility function that generates a unique abbreviation for a given name, ensuring it does not collide with existing abbreviations.
Installation
npm install unique-abbrUsage
import { uniqueAbbr } from 'unique-abbr';
const existingCodes = ['ABC', 'DEF', 'GHI'];
const abbreviation = uniqueAbbr('John Doe', existingCodes);
console.log(abbreviation); // Outputs a unique 3-letter abbreviationCustom Length
const abbreviation = uniqueAbbr('John Doe', existingCodes, 4);
console.log(abbreviation); // Outputs a unique 4-letter abbreviationFunction Details
uniqueAbbr(name: string, existingCodes: string[], length: number = 3): string
- name: The input name to generate an abbreviation from.
- existingCodes: A list of already used abbreviations to avoid duplication.
- length (optional, default = 3): The desired length of the abbreviation.
Features
✅ Handles single and multi-word names ✅ Ensures uniqueness against existing codes ✅ Case-insensitive matching for existing codes ✅ Pads short names to meet the required length ✅ Supports custom abbreviation lengths
Tests
This package includes a full suite of Jest tests.
npm testExample Test Cases
test('Generates a unique abbreviation of default length 3', () => {
const existingCodes = ['ABC', 'DEF', 'GHI'];
const result = uniqueAbbr('John Doe', existingCodes);
expect(result).toHaveLength(3);
expect(existingCodes).not.toContain(result.toUpperCase());
});
// More test cases are included in the test suiteThrows an error if a unique abbreviation cannot be found.
Repository
- Repository: https://github.com/liu-purnomo/unique-abbr
- Bugs: https://github.com/liu-purnomo/unique-abbr/issues
- Homepage: https://github.com/liu-purnomo/unique-abbr#readme
- Author: liupurnomo.com
License
MIT License