idTextSlug function

String idTextSlug(
  1. String text, {
  2. int maxLength = 24,
})

The text slug used for an ID, with data-bearing tokens removed: every whitespace-separated token whose first alphanumeric character is a digit (prices, counts, percentages, dates, OTPs — "₹8,994", "+359.55%", "169", "12th"). A label made only of such tokens yields '' and the caller falls back to the structural role_n slug, so a node named by a live value ("NIFTY50 22716.20 (-0.28 %)") keeps one ID across value changes instead of minting a new node every tick.

Implementation

String idTextSlug(String text, {int maxLength = 24}) {
  final kept = <String>[];
  for (final tok in text.split(RegExp(r'\s+'))) {
    if (tok.isEmpty) continue;
    var firstAlnum = -1;
    for (var i = 0; i < tok.length; i++) {
      final c = tok.codeUnitAt(i);
      final isLetter = (c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A);
      final isDigit = c >= 0x30 && c <= 0x39;
      if (isLetter || isDigit) {
        firstAlnum = c;
        break;
      }
    }
    if (firstAlnum == -1) continue;
    if (firstAlnum >= 0x30 && firstAlnum <= 0x39) continue;
    kept.add(tok);
  }
  if (kept.isEmpty) return '';
  final slug = slugify(kept.join(' '), maxLength: maxLength);
  return slug == 'node' ? '' : slug;
}