# isCnpj property

bool get isCnpj

## Implementation

    bool get isCnpj {
      // Get only the numbers from the CNPJ
      String numbers = replaceAll(RegExp(r'[^0-9]'), '');

      // Test if the CNPJ has 14 digits
      if (numbers.length != 14) {
        return false;
      }

      // Test if all digits of the CNPJ are the same
      if (RegExp(r'^(\d)\1*$').hasMatch(numbers)) {
        return false;
      }

      // Divide digits
      List<int> digits = numbers.split('').map(int.parse).toList();

      // Calculate the first check digit
      int calcDv1 = 0;
      int j = 0;
      for (int i in Iterable<int>.generate(12, (int i) => i < 4 ? 5 - i : 13 - i)) {
        calcDv1 += digits[j++] * i;
      }
      calcDv1 %= 11;
      int dv1 = calcDv1 < 2 ? 0 : 11 - calcDv1;

      // Test the first check digit
      if (digits[12] != dv1) {
        return false;
      }

      // Calculate the second check digit
      int calcDv2 = 0;
      j = 0;
      for (int i in Iterable<int>.generate(13, (int i) => i < 5 ? 6 - i : 14 - i)) {
        calcDv2 += digits[j++] * i;
      }
      calcDv2 %= 11;
      int dv2 = calcDv2 < 2 ? 0 : 11 - calcDv2;

      // Test the second check digit
      if (digits[13] != dv2) {
        return false;
      }

      return true;
    }