Sha256: 0e819bfd599b573cb833cee1fa3b32abd50f0f119164a020a4a9c2332aae5c06

Contents?: true

Size: 1.58 KB

Versions: 37

Compression:

Stored size: 1.58 KB

Contents

class PhoneNumber {
  /// Returns `String` for a valid number, `null` for invalid.
  String clean(String phoneNumber) {
    /// initialize an empty string.
    String onlyDigits = "";

    /// find all digits.
    Iterable<Match> findDigits = new RegExp(r'\d+').allMatches(phoneNumber);
    findDigits.forEach((match) {
      onlyDigits += match.group(0);

      /// remove all digits **for an edge case**.
      phoneNumber = phoneNumber.replaceAll(match.group(0), "");
    });

    /// Only these special characters are allowed.
    /// **"." "(" ")" "-" "+" ** and space,
    /// remove these characters.
    phoneNumber = phoneNumber.replaceAll(new RegExp(r"^[-.()+\s]*$"), "");

    /// "123-@:!-7890" is invalid, however **"123-@:!-789012"** is valid
    /// by the current logic since the regex grabs all the digits
    /// but "123-@:!-78901256" should be considered as invalid.
    /// hence if anything else still exists its an invalid number.
    if (phoneNumber.length > 0) return null;

    /// remove leading country code **1** to make it easy to process.
    if (onlyDigits.startsWith("1")) {
      onlyDigits = onlyDigits.substring(1);
    }

    if (onlyDigits.length < 10 || onlyDigits.length > 10) return null;

    /// after removing the country code if it exists,
    /// first and fourth digits can only range from **2-9**
    /// i.e. Area code and Exchange code.
    RegExp codeRange = new RegExp(r'^[2-9]$');
    if (codeRange.hasMatch(onlyDigits[0]) && codeRange.hasMatch(onlyDigits[3])) {
      return onlyDigits;
    }

    /// if a condition isn't accounted for.
    return null;
  }
}

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.179 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.178 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.177 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.176 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.175 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.174 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.173 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.172 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.171 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.170 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.169 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.167 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.166 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.165 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.164 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.163 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.162 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.161 tracks/dart/exercises/phone-number/lib/example.dart
trackler-2.2.1.160 tracks/dart/exercises/phone-number/lib/example.dart