Skip to content

Commit a42467d

Browse files
committed
Fixes #271, #270, #269. Respect includeFormatSymbols on subsequent calls to Cpf() with the same Person.
1 parent c563f8e commit a42467d

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v28.4.3
2+
Release Date: 2019-12-03
3+
4+
* 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!
5+
16
## v28.4.2
27
Release Date: 2019-11-30
38

Source/Bogus/Extensions/Brazil/ExtensionsForBrazil.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ public static class ExtensionsForBrazil
1717
/// <param name="includeFormatSymbols">Includes formatting symbols.</param>
1818
public static string Cpf(this Person p, bool includeFormatSymbols = true)
1919
{
20+
int[] finalDigits;
21+
2022
const string Key = nameof(ExtensionsForBrazil) + "CPF";
2123
if( p.context.ContainsKey(Key) )
2224
{
23-
return p.context[Key] as string;
25+
finalDigits = p.context[Key] as int[];
26+
return FormatCpf(finalDigits, includeFormatSymbols);
2427
}
2528

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

52-
var all = digits.Concat(new[] {check1, check2}).ToArray();
55+
finalDigits = digits.Concat(new[] {check1, check2}).ToArray();
5356

54-
string final;
55-
if( includeFormatSymbols )
57+
p.context[Key] = finalDigits;
58+
59+
return FormatCpf(finalDigits, includeFormatSymbols);
60+
}
61+
62+
public static string FormatCpf(int[] digits, bool includeFormatSymbols)
63+
{
64+
if (includeFormatSymbols)
5665
{
57-
final = $"{all[0]}{all[1]}{all[2]}.{all[3]}{all[4]}{all[5]}.{all[6]}{all[7]}{all[8]}-{all[9]}{all[10]}";
66+
return $"{digits[0]}{digits[1]}{digits[2]}.{digits[3]}{digits[4]}{digits[5]}.{digits[6]}{digits[7]}{digits[8]}-{digits[9]}{digits[10]}";
5867
}
5968
else
6069
{
61-
final = $"{all[0]}{all[1]}{all[2]}{all[3]}{all[4]}{all[5]}{all[6]}{all[7]}{all[8]}{all[9]}{all[10]}";
70+
return $"{digits[0]}{digits[1]}{digits[2]}{digits[3]}{digits[4]}{digits[5]}{digits[6]}{digits[7]}{digits[8]}{digits[9]}{digits[10]}";
6271
}
63-
64-
p.context[Key] = final;
65-
66-
return final;
6772
}
6873

74+
6975
/// <summary>
7076
/// Cadastro Nacional da Pessoa Jurídica
7177
/// </summary>

0 commit comments

Comments
 (0)