hasSequentialRun static method

bool hasSequentialRun(
  1. String input, {
  2. int minRun = 4,
})

Returns whether the input contains a long increasing or decreasing run.

Implementation

static bool hasSequentialRun(String input, {int minRun = 4}) {
  if (input.length < minRun) {
    return false;
  }

  final lowered = input.toLowerCase();
  var increasing = 1;
  var decreasing = 1;

  for (var index = 1; index < lowered.length; index++) {
    final previous = lowered.codeUnitAt(index - 1);
    final current = lowered.codeUnitAt(index);

    if (current == previous + 1) {
      increasing++;
      if (increasing >= minRun) {
        return true;
      }
    } else {
      increasing = 1;
    }

    if (current == previous - 1) {
      decreasing++;
      if (decreasing >= minRun) {
        return true;
      }
    } else {
      decreasing = 1;
    }
  }

  return false;
}