Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
frozzare committed Aug 27, 2019
1 parent c735afd commit c3149b9
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions lib/personnummer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ class Personnummer {
return false;
}

bool valid = luhn(parts['year'] + parts['month'] + parts['day'] + parts['nm']) == int.parse(parts['check']);
bool valid =
luhn(parts['year'] + parts['month'] + parts['day'] + parts['nm']) ==
int.parse(parts['check']);

if (valid && testDate(int.parse(parts['year']), int.parse(parts['month']), int.parse(parts['day']))) {
if (valid &&
testDate(int.parse(parts['year']), int.parse(parts['month']),
int.parse(parts['day']))) {
return valid;
}

Expand All @@ -21,7 +25,8 @@ class Personnummer {
}

return valid &&
testDate(int.parse(parts['year']), int.parse(parts['month']), int.parse(parts['day']) - 60);
testDate(int.parse(parts['year']), int.parse(parts['month']),
int.parse(parts['day']) - 60);
}

/// Parse Swedish social security numbers and get the parts.
Expand Down Expand Up @@ -51,7 +56,8 @@ class Personnummer {
}

if (sep != '-' && sep != '+') {
if ((century == null || century.isEmpty) || ((DateTime.now().year - int.parse(century + year))) < 100) {
if ((century == null || century.isEmpty) ||
((DateTime.now().year - int.parse(century + year))) < 100) {
sep = '-';
} else {
sep = '+';
Expand All @@ -66,7 +72,9 @@ class Personnummer {
baseYear = DateTime.now().year;
}

century = (baseYear - (baseYear - int.parse(year)) % 100).toString().substring(0, 2);
century = (baseYear - (baseYear - int.parse(year)) % 100)
.toString()
.substring(0, 2);
}

map['century'] = century;
Expand Down Expand Up @@ -96,10 +104,11 @@ class Personnummer {
day -= 60;
}

DateTime u = new DateTime(int.parse(parts['century'] + parts['year']), int.parse(parts['month']), day);
DateTime u = new DateTime(int.parse(parts['century'] + parts['year']),
int.parse(parts['month']), day);
DateTime dt = dateTimeNow == null ? DateTime.now() : dateTimeNow;

return (dt.difference(u).inMilliseconds/3.15576e+10).floor();
return (dt.difference(u).inMilliseconds / 3.15576e+10).floor();
}

/// Format Swedish social security numbers to official format.
Expand All @@ -120,10 +129,20 @@ class Personnummer {
}

if (longFormat) {
return parts['century'] + parts['year'] + parts['month'] + parts['day'] + parts['nm'] + parts['check'];
return parts['century'] +
parts['year'] +
parts['month'] +
parts['day'] +
parts['nm'] +
parts['check'];
}

return parts['year'] + parts['month'] + parts['day'] + parts['sep'] + parts['nm'] + parts['check'];
return parts['year'] +
parts['month'] +
parts['day'] +
parts['sep'] +
parts['nm'] +
parts['check'];
}

/// Luhn/mod10 algorithm. Used to calculate a checksum from the passed value
Expand Down

0 comments on commit c3149b9

Please sign in to comment.