RegexModel.allowOnly constructor

RegexModel.allowOnly(
  1. List<RegexChar> chars, {
  2. String allow = '',
  3. String? title,
})

Only the character groups in chars, plus the characters in allow: RegexModel.allowOnly([RegexChar.letters, RegexChar.whitespace]).

Implementation

factory RegexModel.allowOnly(
  List<RegexChar> chars, {
  String allow = '',
  String? title,
}) {
  assert(chars.isNotEmpty || allow.isNotEmpty, 'Pass chars or allow');
  final special = chars.contains(RegexChar.special);
  final normal = chars.where((c) => c != RegexChar.special);
  final cls = '[${normal.map((c) => c.pattern).join()}${_escape(allow)}]';
  // Special = not a letter, digit or space; joined with an alternation as it is a negated class.
  final one = special ? '(?:$cls|[${RegexChar.special.pattern}])' : cls;
  final only = RegExp('^$one+\$', unicode: true);
  return RegexModel(
    title: title ?? RegexLabels.current.only(_words(chars, allow)),
    test: only.hasMatch,
  );
}