JSPM

unique-abbr

2.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q22891F
  • License MIT

A simple TypeScript utility to generate unique three-letter abbreviations based on names, ensuring no duplicates from a given list.

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-abbr

Usage

import { uniqueAbbr } from 'unique-abbr';

const existingCodes = ['ABC', 'DEF', 'GHI'];
const abbreviation = uniqueAbbr('John Doe', existingCodes);
console.log(abbreviation); // Outputs a unique 3-letter abbreviation

Custom Length

const abbreviation = uniqueAbbr('John Doe', existingCodes, 4);
console.log(abbreviation); // Outputs a unique 4-letter abbreviation

Function 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 test

Example 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 suite

Throws an error if a unique abbreviation cannot be found.

Repository

License

MIT License