Skip to content

Commit

Permalink
Fixes #271, #270, #269. Respect includeFormatSymbols on subsequent ca…
Browse files Browse the repository at this point in the history
…lls to Cpf() with the same Person.
  • Loading branch information
bchavez committed Dec 4, 2019
1 parent c563f8e commit a42467d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v28.4.3
Release Date: 2019-12-03

* Issue 271: Minor bug fix in Brazil `Person.Cpf()` extension method. Previously, only the first call to `Person.Cpf(includeFormatSymbols)` respected the `includeFormatSymbols` parameter due to the final result being saved in `Person` context. `Person.Cpf()` now respects the `includeFormatSymbols` parameter after subsequent repeat calls to `Cpf()` with the same `Person`. Thanks for testing @ArthNRick!

## v28.4.2
Release Date: 2019-11-30

Expand Down
26 changes: 16 additions & 10 deletions Source/Bogus/Extensions/Brazil/ExtensionsForBrazil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public static class ExtensionsForBrazil
/// <param name="includeFormatSymbols">Includes formatting symbols.</param>
public static string Cpf(this Person p, bool includeFormatSymbols = true)
{
int[] finalDigits;

const string Key = nameof(ExtensionsForBrazil) + "CPF";
if( p.context.ContainsKey(Key) )
{
return p.context[Key] as string;
finalDigits = p.context[Key] as int[];
return FormatCpf(finalDigits, includeFormatSymbols);
}

var digits = p.Random.Digits(9);
Expand Down Expand Up @@ -49,23 +52,26 @@ public static string Cpf(this Person p, bool includeFormatSymbols = true)
check2 = 11 - sum2mod;
}

var all = digits.Concat(new[] {check1, check2}).ToArray();
finalDigits = digits.Concat(new[] {check1, check2}).ToArray();

string final;
if( includeFormatSymbols )
p.context[Key] = finalDigits;

return FormatCpf(finalDigits, includeFormatSymbols);
}

public static string FormatCpf(int[] digits, bool includeFormatSymbols)
{
if (includeFormatSymbols)
{
final = $"{all[0]}{all[1]}{all[2]}.{all[3]}{all[4]}{all[5]}.{all[6]}{all[7]}{all[8]}-{all[9]}{all[10]}";
return $"{digits[0]}{digits[1]}{digits[2]}.{digits[3]}{digits[4]}{digits[5]}.{digits[6]}{digits[7]}{digits[8]}-{digits[9]}{digits[10]}";
}
else
{
final = $"{all[0]}{all[1]}{all[2]}{all[3]}{all[4]}{all[5]}{all[6]}{all[7]}{all[8]}{all[9]}{all[10]}";
return $"{digits[0]}{digits[1]}{digits[2]}{digits[3]}{digits[4]}{digits[5]}{digits[6]}{digits[7]}{digits[8]}{digits[9]}{digits[10]}";
}

p.context[Key] = final;

return final;
}


/// <summary>
/// Cadastro Nacional da Pessoa Jurídica
/// </summary>
Expand Down

0 comments on commit a42467d

Please sign in to comment.