or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-abbrev

JavaScript implementation of Ruby's abbrev module for generating unique string abbreviations

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/abbrev@3.0.x

To install, run

npx @tessl/cli install tessl/npm-abbrev@3.0.0

index.mddocs/

Abbrev

Abbrev provides JavaScript implementation of Ruby's abbrev module for generating unique string abbreviations. It takes a list of words and returns an object mapping each unique abbreviation to its full word, enabling users to type shortened versions of commands or options while maintaining unambiguous identification.

Package Information

  • Package Name: abbrev
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install abbrev

Core Imports

const abbrev = require("abbrev");

ES Modules:

import abbrev from "abbrev";
// or with explicit default import
import { default as abbrev } from "abbrev";

Basic Usage

const abbrev = require("abbrev");

// Multiple string arguments
const result = abbrev("foo", "fool", "folding", "flop");

// Or array of strings
const result2 = abbrev(["foo", "fool", "folding", "flop"]);

console.log(result);
// {
//   fl: 'flop',
//   flo: 'flop', 
//   flop: 'flop',
//   fol: 'folding',
//   fold: 'folding',
//   foldi: 'folding',
//   foldin: 'folding',
//   folding: 'folding',
//   foo: 'foo',
//   fool: 'fool'
// }

// Single string usage
const single = abbrev("asdf");
// { a: 'asdf', as: 'asdf', asd: 'asdf', asdf: 'asdf' }

Capabilities

Abbreviation Generation

Generates a mapping of unique abbreviations to their full words from input strings.

/**
 * Generate abbreviations for a list of words
 * @param {...(string|string[]|Object)} args - Variable arguments: multiple strings, array of strings/objects, or single string
 * @returns {Object} Object mapping each unique abbreviation to its full word
 */
function abbrev(...args: (string | string[] | Object)[]): { [abbreviation: string]: string };

Parameters:

  • ...args - Variable number of arguments, accepting:
    • Multiple strings: abbrev("foo", "bar", "baz")
    • Array of strings: abbrev(["foo", "bar", "baz"])
    • Array of objects with toString(): abbrev([{toString: () => "foo"}])
    • Single string: abbrev("foo")
    • Objects are converted to strings using String() constructor

Return Value:

  • Object where keys are abbreviations and values are the corresponding full words
  • Each abbreviation uniquely identifies exactly one word from the input
  • Full words are always included as keys mapping to themselves

Behavior:

  • Input words are sorted lexicographically for consistent results
  • Duplicate words are automatically filtered out
  • Non-string inputs are converted to strings via String() constructor
  • Generates all possible unique prefixes for each word
  • Returns empty object for empty input

Usage Examples:

// Mixed types (objects converted to strings)
const mixed = abbrev("fool", "foom", "pool", { toString: () => 'pope' });
// {
//   fool: 'fool',
//   foom: 'foom', 
//   poo: 'pool',
//   pool: 'pool',
//   pop: 'pope',
//   pope: 'pope'
// }

// Array input
const fromArray = abbrev(['a', 'ab', 'abc']);
// { a: 'a', ab: 'ab', abc: 'abc' }

// Duplicate handling
const withDupes = abbrev(['ruby', 'ruby', 'rules']);
// { rub: 'ruby', ruby: 'ruby', rul: 'rules', rule: 'rules', rules: 'rules' }

This package is ideal for implementing command-line shortcuts, auto-completion systems, and any scenario where users need to type abbreviated versions of longer identifiers.