parseViews function

String? parseViews(
  1. String text
)

Parses views

Implementation

String? parseViews(String text) {
  String localText = text;
  // only for non-latin scripts: "Maroon 5" is indistinguishable from a prefixed count
  var prefixed = 0;

  if (!RegExp('[a-zA-Z]').hasMatch(localText)) {
    final match = viewsPrefix.firstMatch(localText);

    if (match != null) {
      localText = localText.substring(match.end);
      prefixed = 1;
    }
  }

  if (!RegExp(r'^\d').hasMatch(localText)) {
    return null;
  }

  // a bare ASCII token like "2Pac" is an artist, not a view count with a stripped word
  if (prefixed == 0 && localText.isAscii && !localText.contains(' ')) {
    return null;
  }

  // note: \xa0 glues number and magnitude ("1,7\xa0Mrd. Aufrufe"), but some locales use
  // it before the views word too ("88\xa0k\xa0vues"); CJK has no separator ("3406万回視聴")
  final head = localText.split(' ')[0].split('\xa0');

  return head.take(2).join('\xa0');
}