-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mrstebo/feature/number-faker
Feature/number faker
- Loading branch information
Showing
22 changed files
with
548 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Faker.Number | ||
|
||
```js | ||
Faker.Number.Number(10) //=> "1968353479" | ||
|
||
Faker.Number.LeadingZeroNumber(10) //=> "0669336915" | ||
|
||
Faker.Number.Decimal(2) //=> "11.88" | ||
|
||
Faker.Number.Decimal(2, 3) //=> "18.843" | ||
|
||
Faker.Number.Normal(50, 3.5) //=> 47.14669604069156 | ||
|
||
Faker.Number.Hexadecimal(3) //=> "e74" | ||
|
||
Faker.Number.Between(1, 10) //=> 7 | ||
|
||
Faker.Number.Positive() //=> 235.59238499107653 | ||
|
||
Faker.Number.Negative() //=> -4480.042585669558 | ||
|
||
Faker.Number.NonZeroDigit() //=> "8" | ||
|
||
Faker.Number.Digit() //=> "1" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,4 +99,4 @@ internal class AppData | |
"#.#." | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,4 @@ public string Author() | |
return $"{_fakerContainer.Name.FirstName()} {_fakerContainer.Name.LastName()}"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using FakerDotNet.Wrappers; | ||
|
||
namespace FakerDotNet.Fakers | ||
{ | ||
public interface INumberFaker | ||
{ | ||
string Number(int digits = 10); | ||
string LeadingZeroNumber(int digits = 10); | ||
string Decimal(int leftDigits = 5, int rightDigits = 2); | ||
double Normal(int mean = 1, int standardDeviation = 1); | ||
string Hexadecimal(int digits = 6); | ||
double Between(double min, double max); | ||
double Within(Range<double> range); | ||
double Positive(double from = 1.00D, double to = 5000.00D); | ||
double Negative(double from = -5000.00D, double to = -1.00D); | ||
string NonZeroDigit(); | ||
string Digit(); | ||
} | ||
|
||
internal class NumberFaker : INumberFaker | ||
{ | ||
private readonly IRandomWrapper _randomWrapper; | ||
|
||
public NumberFaker() | ||
: this(new RandomWrapper()) | ||
{ | ||
} | ||
|
||
internal NumberFaker(IRandomWrapper randomWrapper) | ||
{ | ||
_randomWrapper = randomWrapper; | ||
} | ||
|
||
public string Number(int digits = 10) | ||
{ | ||
var sb = new StringBuilder(); | ||
|
||
if (digits > 1) | ||
{ | ||
sb.Append(NonZeroDigit()); | ||
digits--; | ||
} | ||
sb.Append(LeadingZeroNumber(digits)); | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
public string LeadingZeroNumber(int digits = 10) | ||
{ | ||
return string.Join("", Enumerable.Range(0, digits).Select(_ => _randomWrapper.Next(0, 9))); | ||
} | ||
|
||
public string Decimal(int leftDigits = 5, int rightDigits = 2) | ||
{ | ||
return $"{Number(leftDigits)}.{Number(rightDigits)}"; | ||
} | ||
|
||
public double Normal(int mean = 1, int standardDeviation = 1) | ||
{ | ||
var theta = 2 * Math.PI * _randomWrapper.NextDouble(); | ||
var rho = Math.Sqrt(-2 * Math.Log(1 - +_randomWrapper.NextDouble())); | ||
var scale = standardDeviation * rho; | ||
return mean + scale * Math.Cos(theta); | ||
} | ||
|
||
public string Hexadecimal(int digits = 6) | ||
{ | ||
return string.Join("", Enumerable.Range(0, digits).Select(_ => int.Parse(Digit()).ToString("X"))); | ||
} | ||
|
||
public double Between(double min, double max) | ||
{ | ||
var realMin = Math.Min(min, max); | ||
var realMax = Math.Max(min, max); | ||
|
||
return _randomWrapper.NextDouble() * (realMax - realMin) + realMin; | ||
} | ||
|
||
public double Within(Range<double> range) | ||
{ | ||
return Between(range.Minimum, range.Maximum); | ||
} | ||
|
||
public double Positive(double from = 1.00D, double to = 5000.00D) | ||
{ | ||
return Between(from, to); | ||
} | ||
|
||
public double Negative(double from = -5000.00D, double to = -1.00D) | ||
{ | ||
return Between(from, to); | ||
} | ||
|
||
public string NonZeroDigit() | ||
{ | ||
return Convert.ToString(_randomWrapper.Next(1, 9)); | ||
} | ||
|
||
public string Digit() | ||
{ | ||
return Convert.ToString(_randomWrapper.Next(0, 9)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace FakerDotNet | ||
{ | ||
public sealed class Range<T> where T : struct | ||
{ | ||
public T Minimum { get; } | ||
public T Maximum { get; } | ||
|
||
public Range(T minimum, T maximum) | ||
{ | ||
Minimum = minimum; | ||
Maximum = maximum; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace FakerDotNet.Wrappers | ||
{ | ||
internal interface IRandomWrapper | ||
{ | ||
int Next(int min, int max); | ||
double NextDouble(); | ||
} | ||
|
||
[ExcludeFromCodeCoverage] | ||
internal class RandomWrapper : IRandomWrapper | ||
{ | ||
private static readonly Random RandomInstance = new Random(); | ||
|
||
public int Next(int min, int max) => RandomInstance.Next(min, max); | ||
|
||
public double NextDouble() => RandomInstance.NextDouble(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.