Line data Source code
1 : import 'package:flutter/foundation.dart';
2 :
3 : /// A class that masks a string by replacing all occurrences of a given string
4 : @immutable
5 : class MaskingString {
6 : /// Create a [MaskingString] instance.
7 6 : MaskingString(
8 : this.from, {
9 : this.caseSensitive = true,
10 : this.maskedString = _defaultMaskedString,
11 : bool isRegExp = false,
12 6 : }) : _regExp = RegExp(
13 6 : isRegExp ? from : RegExp.escape(from),
14 : caseSensitive: caseSensitive,
15 : );
16 :
17 : /// The string to search for in the source string.
18 : final String from;
19 :
20 : /// Whether the search for the [from] string is case sensitive.
21 : final bool caseSensitive;
22 :
23 : /// What to replace the [from] string with.
24 : final String maskedString;
25 :
26 : static const String _defaultMaskedString = '***';
27 : final RegExp _regExp;
28 :
29 : /// Mask the input string.
30 24 : String mask(String input) => input.replaceAll(_regExp, maskedString);
31 :
32 1 : @override
33 : bool operator ==(Object other) =>
34 : identical(this, other) ||
35 1 : (other is MaskingString &&
36 3 : runtimeType == other.runtimeType &&
37 3 : from == other.from &&
38 3 : caseSensitive == other.caseSensitive &&
39 3 : maskedString == other.maskedString);
40 :
41 5 : @override
42 : int get hashCode =>
43 40 : from.hashCode ^ caseSensitive.hashCode ^ maskedString.hashCode;
44 : }
|