JavaScript implementation of Ruby's abbrev module for generating unique string abbreviations
npx @tessl/cli install tessl/npm-abbrev@3.0.0Abbrev 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.
npm install abbrevconst abbrev = require("abbrev");ES Modules:
import abbrev from "abbrev";
// or with explicit default import
import { default as abbrev } from "abbrev";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' }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:
abbrev("foo", "bar", "baz")abbrev(["foo", "bar", "baz"])abbrev([{toString: () => "foo"}])abbrev("foo")Return Value:
Behavior:
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.