CountryCode.user constructor

CountryCode.user({
  1. String? alpha2,
  2. String? alpha3,
  3. int? numeric,
})

Creates user-defined country code. Note: Code is not registered in class values, use assign to register user country codes.

Implementation

factory CountryCode.user({String? alpha2, String? alpha3, int? numeric}) {
  assert(!(alpha2 == null && alpha3 == null && numeric == null));
  assert(alpha2 == null || alpha2.length >= 2);
  assert(alpha3 == null || alpha3.length >= 3);

  var a2 = 0;
  if (alpha2 != null) {
    a2 = _packAlpha2(alpha2.codeUnits);
    if (!_isInRange(a2, _userA2Ranges)) {
      throw ArgumentError('Alpha-2 code is not in allowed range');
    }
  }

  var a3 = 0;
  if (alpha3 != null) {
    a3 = _packAlpha3(alpha3.codeUnits);
    if (!_isInRange(a3, _userA3Ranges)) {
      throw ArgumentError('Alpha-3 code is not in allowed range');
    }
  }

  if (numeric != null && (numeric < 900 || numeric > 999)) {
    throw ArgumentError.value(
        numeric, 'numeric', 'Should be between 900..999');
  }

  numeric ??= 0;

  if ((1 << 32) == 0) {
    return CountryCode._(a2 + (a3 | numeric));
  }

  return CountryCode._(a2 | a3 | numeric);
}