makeConvertor static method

Convertor makeConvertor([
  1. String? toAlphabet,
  2. PaddingParams? options
])

Create a custom convertor using a specified alphabet.

Implementation

static Convertor makeConvertor([String? toAlphabet, PaddingParams? options]) {
  // Default to Flickr 58
  final useAlphabet = toAlphabet ?? constants.flickrBase58;

  // Default to baseOptions
  final selectedOptions = options ?? PaddingParams(
    shortIdLength: getShortIdLength(useAlphabet.length),
    paddingChar: useAlphabet[0],
  );

  // Check alphabet for duplicate entries
  if (Set.from(useAlphabet.split('')).length != useAlphabet.length) {
    throw FormatException(
        'The provided Alphabet has duplicate characters resulting in unreliable results');
  }

  // UUIDs are in hex, so we translate to and from.
  final anyBase = AnyBase(AnyBase.hex, useAlphabet);
  final fromHex = anyBase.convert;
  final toHex = anyBase.revert;
  final generate = () => shortenUUID(uuidV4(), fromHex, options);

  return Convertor(
    generate: generate,
    uuid: uuidV4,
    fromUUID: (uuid) => shortenUUID(uuid, fromHex, selectedOptions),
    toUUID: (shortUuid) => enlargeUUID(shortUuid, toHex),
    alphabet: useAlphabet,
  );
}