Ejemplo n.º 1
0
 public static string CreditCardNumber(Bogus.DataSets.CardType provider) => Finance.CreditCardNumber(provider);
Ejemplo n.º 2
0
        /// <summary>
        /// Generate a random credit card number with valid Luhn checksum.
        /// </summary>
        /// <param name="provider">The type of credit card to generate (ie: American Express, Discover, etc.). Passing null, a random card provider will be chosen.</param>
        public string CreditCardNumber(CardType provider = null)
        {
            if (provider is null)
            {
                provider = this.Random.ListItem(CardType.All);
            }

            var format = GetRandomArrayItem($"credit_card.{provider.Value}");

            var symbol         = '#';
            var expandedFormat = RegexStyleStringParse(format);                      // replace [4-9] with a random number in range etc...
            var cardNumber     = this.Random.ReplaceNumbers(expandedFormat, symbol); // replace ### with random numbers

            var numberList = cardNumber.Where(char.IsDigit)
                             .Select(c => int.Parse(c.ToString())).ToList();

            var checkNum = numberList.CheckDigit();

            return(cardNumber.Replace("L", checkNum.ToString()));

            string RegexStyleStringParse(string str = "")
            {
                // Deal with range repeat `{min,max}`
                var RANGE_REP_REG = new Regex(@"(.)\{(\d+)\,(\d+)\}");
                var REP_REG = new Regex(@"(.)\{(\d+)\}");
                var RANGE_REG = new Regex(@"\[(\d+)\-(\d+)\]");
                int min, max, tmp, repetitions;
                var token = RANGE_REP_REG.Match(str);

                while (token.Success)
                {
                    min = Int32.Parse(token.Groups[2].Value);
                    max = Int32.Parse(token.Groups[3].Value);

                    if (min > max)
                    {
                        tmp = max;
                        max = min;
                        min = tmp;
                    }

                    repetitions = this.Random.Number(min, max);

                    str = str.Substring(0, token.Index) +
                          new string(token.Groups[1].Value[0], repetitions) +
                          str.Substring(token.Index + token.Groups[0].Length);

                    token = RANGE_REP_REG.Match(str);
                }
                // Deal with repeat `{num}`
                token = REP_REG.Match(str);
                while (token.Success)
                {
                    repetitions = Int32.Parse(token.Groups[2].Value);

                    str = str.Substring(0, token.Index) +
                          new string(token.Groups[1].Value[0], repetitions) +
                          str.Substring(token.Index + token.Groups[0].Length);

                    token = REP_REG.Match(str);
                }
                // Deal with range `[min-max]` (only works with numbers for now)
                //TODO: implement for letters e.g. [0-9a-zA-Z] etc.

                token = RANGE_REG.Match(str);
                while (token.Success)
                {
                    min = Int32.Parse(token.Groups[1].Value); // This time we are not capturing the char befor `[]`
                    max = Int32.Parse(token.Groups[2].Value);
                    // switch min and max
                    if (min > max)
                    {
                        tmp = max;
                        max = min;
                        min = tmp;
                    }
                    str = str.Substring(0, token.Index) +
                          this.Random.Number(min, max) +
                          str.Substring(token.Index + token.Groups[0].Length);
                    token = RANGE_REG.Match(str);
                }
                return(str);
            }
        }