generate static method

Cnpj generate([
  1. int? seed
])

Implementation

static Cnpj generate([int? seed]) {
  final randomGenerator = Random(seed);
  final digits = [for (var i = 1; i <= 12; i++) randomGenerator.nextInt(10)];
  final consts = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
  final consts2 = [6, ...consts];

  final sum1 = [for (var i = 0; i <= 11; i++) digits[i] * consts[i]]
          .fold(0, (x, y) => x + y) %
      11;
  final dv1 = sum1 < 2 ? 0 : 11 - sum1;

  final digits2 = [...digits, dv1];
  final sum2 = ([for (var i = 0; i <= 12; i++) digits2[i] * consts2[i]])
          .fold(0, (x, y) => x + y) %
      11;
  final dv2 = sum2 < 2 ? 0 : 11 - sum2;

  return Cnpj._([...digits2, dv2].toString());
}