mcgStringNumberKey static method

String? mcgStringNumberKey(
  1. String? text, {
  2. int length = 3,
})

Implementation

static String? mcgStringNumberKey(String? text, {int length = 3}) {
  if (text == null) return null;
  if (text.length < length) return null;
  const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  const key0 = 'abcdefghijklmnprstuvyz';
  const key1 = '0123456789';
  const key2 = '8904317256';
  const key3 = '7903641258';
  const key4 = '7903625841';
  const key5 = '5904318762';

  final characters = text.split('');
  var whichFirstCharacter = text.length ~/ length;

  var result = '';
  for (var i = 0; i < length; i++) {
    var possibleIndex = possible.indexOf(characters[(whichFirstCharacter * (i + 1)).clamp(0, characters.length - 1).toInt()]);
    if (possibleIndex == -1) possibleIndex = 0;
    final key = i == 0
        ? key0
        : text.length % 5 == 0
            ? key1
            : text.length % 5 == 1
                ? key2
                : text.length % 5 == 2
                    ? key3
                    : text.length % 5 == 3
                        ? key4
                        : key5;
    result += key[((possibleIndex) % key.length)];
  }
  return result;
}