Last active
December 30, 2021 03:39
-
-
Save GAM3RG33K/7c054b6ed20142ce2f6b242b47905816 to your computer and use it in GitHub Desktop.
Dart implementation for Filter Duplicate numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
/// Mock list of contacts | |
final _rawContacts = [ | |
// Actual Number: 9000000000 | |
"+919000000000", | |
"+9190000 00000", | |
"+91900-000-0000", | |
"919000000000", | |
"9190000 00000", | |
"91900-000-0000", | |
"9000000000", | |
"90000 00000", | |
"900-000-0000", | |
// Actual Number: 9000000015 | |
"+919000000015", | |
"+9190000 00015", | |
"+91900-000-0015", | |
// Actual Number: 9100000000 | |
"919100000000", | |
"9191000 00000", | |
"91910-000-0000", | |
"9900000000", | |
"99000 00000", | |
"990-000-0000", | |
// invalid numbers | |
"99000002", | |
"9900 0009", | |
"9900-0008", | |
]; | |
//print(_rawContacts); | |
final _filtered = _rawContacts.map((number) { | |
final _processed = _removeSpecialSymbols(number); | |
final _actualNumber = _getActualNumber(_processed); | |
return _actualNumber; | |
}).toSet()..removeWhere((number) => number.isEmpty); | |
print(_filtered); | |
} | |
/// Removes +,- & whitespaces from the numbers | |
String _removeSpecialSymbols(String number) { | |
final _result = | |
number.replaceAll('+', '').replaceAll('-', '').replaceAll(' ', ''); | |
// print('_removeSpecialSymbols: $_result'); | |
return _result; | |
} | |
/// strips extra digits or numbers from start of the string | |
String _getActualNumber(String number) { | |
var _result = number; | |
final _length = number.length; | |
if (_length > 10) { // without country code | |
final _diff = _length - 10; | |
_result = number.substring(_diff, _length); | |
} else { | |
// invalid number return as empty string | |
return ''; | |
} | |
// print('_getActualNumber: $_result'); | |
return _result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment