/// <summary> /// Gets an IEnumerable of random identifiers based on the settings /// <para>Bear in mind that enumerating the result set multiple times will yield different results</para> /// </summary> /// <param name="numberOfNamesToReturn">How many outputs to get</param> /// <param name="components">The components.</param> /// <param name="orderStyle">The order style.</param> /// <param name="separator">The separator between the words.</param> /// <param name="forceSingleLetter">if set to <c>true</c> all words of the result will start with the same letter, e.g. Irena The Idiosyncratic, Tammara The Twinkly or Quintana The Qualified</param> /// <param name="lengthRestriction">Maximum number of characters in the returned string</param> /// <returns>System.String.</returns> /// <exception cref="ArgumentOutOfRangeException">orderStyle - null</exception> public IEnumerable <string> Get(int numberOfNamesToReturn, IdentifierComponents components = IdentifierComponents.Adjective | IdentifierComponents.Noun, NameOrderingStyle orderStyle = DefaultOrderStyle, string separator = " ", bool forceSingleLetter = false, int lengthRestriction = 0) { for (int i = 0; i < numberOfNamesToReturn; i++) { yield return(this.Get(components, orderStyle, separator, forceSingleLetter, lengthRestriction)); } }
private string Get(IdentifierComponents components, NameOrderingStyle orderStyle, string separator, char?forcedSingleLetter) { StringBuilder sb = new StringBuilder(); switch (orderStyle) { case NameOrderingStyle.SilentBobStyle: return(this.GetSilentBobOrder(components, separator, sb, forcedSingleLetter)); case NameOrderingStyle.BobTheBuilderStyle: return(this.GetBobTheBuilderOrder(components, separator, sb, forcedSingleLetter)); default: throw new ArgumentOutOfRangeException(nameof(orderStyle), orderStyle, null); } }
private string GetSilentBobOrder(IdentifierComponents components, string separator, StringBuilder sb, char?forcedSingleLetter) { if (components.HasFlag(IdentifierComponents.Adjective)) { sb.Append(Helpers.GetToken(this.adjectives, this.randomIndex, forcedSingleLetter) + separator); } if (components.HasFlag(IdentifierComponents.Nationality)) { sb.Append(Helpers.GetToken(this.nationalities, this.randomIndex, forcedSingleLetter) + separator); } this.GetTheNounPart(components, separator, sb, forcedSingleLetter); if (components.HasFlag(IdentifierComponents.FirstName)) { sb.Append(Helpers.GetToken(this.firstNames, this.randomIndex, forcedSingleLetter) + separator); } return(sb.ToString().TrimEnd(separator)); }
private void GetTheNounPart(IdentifierComponents components, string separator, StringBuilder sb, char?forcedSingleLetter) { var combinedNouns = new List <string>(); if (components.HasFlag(IdentifierComponents.Animal)) { combinedNouns.AddRange(this.animals); } if (components.HasFlag(IdentifierComponents.Noun)) { combinedNouns.AddRange(this.nouns); } if (components.HasFlag(IdentifierComponents.Profession)) { combinedNouns.AddRange(this.professions); } if (combinedNouns.Any()) { sb.Append(Helpers.GetToken(combinedNouns, this.randomIndex, forcedSingleLetter) + separator); } }
/// <summary> /// Gets a random identifier based on the settings /// </summary> /// <param name="components">The components.</param> /// <param name="orderStyle">The order style.</param> /// <param name="separator">The separator between the words.</param> /// <param name="forceSingleLetter">if set to <c>true</c> all words of the result will start with the same letter, e.g. Irena The Idiosyncratic, Tammara The Twinkly or Quintana The Qualified</param> /// <param name="lengthRestriction">Maximum number of characters in the returned string</param> /// <returns>System.String.</returns> /// <exception cref="ArgumentOutOfRangeException">orderStyle - null</exception> public string Get(IdentifierComponents components = IdentifierComponents.Adjective | IdentifierComponents.Noun, NameOrderingStyle orderStyle = DefaultOrderStyle, string separator = " ", bool forceSingleLetter = false, int lengthRestriction = 0) { char?forcedSingleLetter = Helpers.GetForcedSingleCharacter(forceSingleLetter, this.randomIndex); int attempt = 0; while (true) //if there is a length restriction, retry a couple of times. If the string is still too long, just trim it { attempt++; var name = this.Get(components, orderStyle, separator, forcedSingleLetter); if (lengthRestriction <= 0) { return(name); } else if (name.Length <= lengthRestriction) { return(name); } if (attempt >= 100) { return(name.Remove(lengthRestriction)); } } }