| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TextFaker.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Text; |
| | | 11 | | using MyNet.Generator; |
| | | 12 | | |
| | | 13 | | namespace MyNet.Fakers.Text; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Current implementation of <see cref="ITextFaker"/>. |
| | | 17 | | /// </summary> |
| | | 18 | | public sealed class TextFaker(IRandomGenerator random) : ITextFaker |
| | | 19 | | { |
| | | 20 | | private const string LoremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendis |
| | | 21 | | |
| | 3 | 22 | | private static readonly string[] LoremWords = BuildUniqueWords(); |
| | | 23 | | |
| | | 24 | | private static string[] BuildUniqueWords() |
| | | 25 | | { |
| | 3 | 26 | | var cleaned = LoremIpsum |
| | 3 | 27 | | .Replace(",", string.Empty, StringComparison.Ordinal) |
| | 3 | 28 | | .Replace(".", string.Empty, StringComparison.Ordinal); |
| | | 29 | | |
| | 3 | 30 | | var words = cleaned.Split(' ', StringSplitOptions.RemoveEmptyEntries).Distinct(StringComparer.Ordinal).ToList(); |
| | | 31 | | |
| | 3 | 32 | | return [..words]; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public string Word(int? length = null) |
| | | 37 | | { |
| | 39 | 38 | | var word = random.Item(LoremWords); |
| | | 39 | | |
| | 39 | 40 | | return length is null ? word : word.Length > length ? word[..length.Value] : word; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public string Words(int min, int max) |
| | | 45 | | { |
| | 9 | 46 | | var count = random.Int(min, max); |
| | 9 | 47 | | var builder = new StringBuilder(); |
| | | 48 | | |
| | 90 | 49 | | for (var i = 0; i < count; i++) |
| | | 50 | | { |
| | 36 | 51 | | if (i > 0) |
| | 27 | 52 | | builder.Append(' '); |
| | 36 | 53 | | builder.Append(Word()); |
| | | 54 | | } |
| | | 55 | | |
| | 9 | 56 | | return builder.ToString(); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | public string Sentence(int minWords = 4, int maxWords = 12) |
| | | 61 | | { |
| | 9 | 62 | | var words = Words(minWords, maxWords); |
| | | 63 | | |
| | 9 | 64 | | return words.Length == 0 ? "." : char.ToUpper(words[0], CultureInfo.CurrentCulture) + words[1..] + "."; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | public string Paragraph(int minSentences = 2, int maxSentences = 5) |
| | | 69 | | { |
| | 3 | 70 | | var count = random.Int(minSentences, maxSentences); |
| | 3 | 71 | | var builder = new StringBuilder(); |
| | | 72 | | |
| | 18 | 73 | | for (var i = 0; i < count; i++) |
| | | 74 | | { |
| | 6 | 75 | | if (i > 0) |
| | 3 | 76 | | builder.Append(' '); |
| | 6 | 77 | | builder.Append(Sentence()); |
| | | 78 | | } |
| | | 79 | | |
| | 3 | 80 | | return builder.ToString(); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <inheritdoc /> |
| | | 84 | | public string Lorem(int? wordCount = null) |
| | | 85 | | { |
| | 3 | 86 | | if (!wordCount.HasValue) |
| | 0 | 87 | | return LoremIpsum; |
| | | 88 | | |
| | 3 | 89 | | var words = LoremIpsum.Split(' ', StringSplitOptions.RemoveEmptyEntries); |
| | 3 | 90 | | var builder = new StringBuilder(); |
| | 3 | 91 | | var count = Math.Min(wordCount.Value, words.Length); |
| | | 92 | | |
| | 36 | 93 | | for (var i = 0; i < count; i++) |
| | | 94 | | { |
| | 15 | 95 | | if (i > 0) |
| | 12 | 96 | | builder.Append(' '); |
| | 15 | 97 | | builder.Append(words[i]); |
| | | 98 | | } |
| | | 99 | | |
| | 3 | 100 | | return builder.ToString(); |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |