aslug

A lossless slug that preserves uniqueness

Usage no npm install needed!

<script type="module">
  import aslug from 'https://cdn.skypack.dev/aslug';
</script>

README

aslug

Version Build Status Coverage Dependencies Vulnerabilities License Types

A lossless slug that preserves uniqueness.

Install

npm install aslug

Features

An example use case for aslug would be a set of ids that have to be made safe for some character set, while maintaining readability and ensuring they won't conflict with other valid ids. Hence, aslug:

  • Produces a readable, lossless, encoding.
  • Includes a decoder to recover the original string.
  • Allows the character set to be modified via options.

Usage

aslug(options?)

aslug takes an options object and returns an object with methods encode and decode -both taking a string.

Optional options fields include:

  • An alphabet for the payload. Defaults to: '0123456789ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-'.
  • A separator to be used between the readable string and the payload. Defaults to '~'.
  • A target regular expression to identify the characters to replace. Defaults to matching all characters not in alphabet.
  • A map function to determine the strategy to use when replacing characters: remove diacritics, leave empty, or any other alternative. Defaults to replacing characters for their ASCII equivalent.
import aslug from 'aslug';

const { encode, decode } = aslug();

encode('string w/ special chars'); // string-w-special-chars~L42YrSYaHpHrCP63nHvD

decode('string-w-special-chars~L42YrSYaHpHrCP63nHvD'); // string w/ special chars