README
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
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 inalphabet
. - 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