0.5.2 • Published 1 year ago
aslug v0.5.2
aslug
A lossless slug that preserves uniqueness.
Install
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
new Slug(alphabet?: string | null, options?: Slug.Options)
The Slug class optionally takes an alphabet string and an options object.
alphabetdefaults to:'0123456789ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-'optionsfields include:- A
separatorto be used between the readable string and the payload. Defaults to'.'. - A
mapfunction to determine the strategy to use when replacing characters with a string withinalphabet-otherwise an empty string. It can be used to remove diacritics, leave empty, or any other alternative. Defaults to replacing characters for their ASCII equivalent, if inalphabet.
- A
Simple example
import { Slug } from 'aslug';
const slug = new Slug();
slug.encode('string w/ special chars'); // string-w-special-chars.EjhdB6y8T2PsDU0e6zUv
slug.decode('string-w-special-chars.EjhdB6y8T2PsDU0e6zUv'); // string w/ special chars