commit 4b66d928ce9fee609789a4dba18b67f57f3a5516 Author: dochoffiday Date: Tue Aug 19 16:23:08 2014 -0400 first commit diff --git a/Lorem.NET.sln b/Lorem.NET.sln new file mode 100644 index 0000000..e7ac1cf --- /dev/null +++ b/Lorem.NET.sln @@ -0,0 +1,27 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lorem.NET", "Lorem.NET\Lorem.NET.csproj", "{787D3BA7-DB6C-4704-B89C-8D91A4392442}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{438E4420-54B2-45AC-B8C8-010BE97875F3}" + ProjectSection(SolutionItems) = preProject + readme.md = readme.md + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {787D3BA7-DB6C-4704-B89C-8D91A4392442}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {787D3BA7-DB6C-4704-B89C-8D91A4392442}.Debug|Any CPU.Build.0 = Debug|Any CPU + {787D3BA7-DB6C-4704-B89C-8D91A4392442}.Release|Any CPU.ActiveCfg = Release|Any CPU + {787D3BA7-DB6C-4704-B89C-8D91A4392442}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Lorem.NET.v12.suo b/Lorem.NET.v12.suo new file mode 100644 index 0000000..e573c75 Binary files /dev/null and b/Lorem.NET.v12.suo differ diff --git a/Lorem.NET/Extensions.cs b/Lorem.NET/Extensions.cs new file mode 100644 index 0000000..79ccd7b --- /dev/null +++ b/Lorem.NET/Extensions.cs @@ -0,0 +1,28 @@ +using System; + +namespace Lorem.NET +{ + internal static class Extensions + { + internal static String Remove(this string s, string pattern) + { + return s.Replace(pattern, ""); + } + internal static String[] Split(this string s, string separator) + { + return s.Split(new[] { separator }, StringSplitOptions.None); + } + + internal static string UppercaseFirst(this string s) + { + // Check for empty string. + if (string.IsNullOrEmpty(s)) + { + return string.Empty; + } + + // Return char and concat substring. + return char.ToUpper(s[0]) + s.Substring(1); + } + } +} diff --git a/Lorem.NET/Lorem.NET.csproj b/Lorem.NET/Lorem.NET.csproj new file mode 100644 index 0000000..d78baf7 --- /dev/null +++ b/Lorem.NET/Lorem.NET.csproj @@ -0,0 +1,56 @@ + + + + + Debug + AnyCPU + {787D3BA7-DB6C-4704-B89C-8D91A4392442} + Library + Properties + Lorem.NET + Lorem.NET + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Lorem.NET/Lorem.cs b/Lorem.NET/Lorem.cs new file mode 100644 index 0000000..e41c9b8 --- /dev/null +++ b/Lorem.NET/Lorem.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Lorem.NET +{ + public class Lorem + { + public static bool Chance(int successes, int attempts) + { + var number = Number(1, attempts); + + return number <= successes; + } + + public static T Random(T[] items) + { + var index = RandomHelper.Instance.Next(items.Length); + + return items[index]; + } + + /* http://stackoverflow.com/a/6651661/234132 */ + public static long Number(long min, long max) + { + byte[] buf = new byte[8]; + RandomHelper.Instance.NextBytes(buf); + long longRand = BitConverter.ToInt64(buf, 0); + + return (Math.Abs(longRand % ((max + 1) - min)) + min); + } + + #region DateTime + + public static DateTime DateTime(int startYear = 1950, int startMonth = 1, int startDay = 1) + { + return DateTime(new System.DateTime(startYear, startMonth, startDay), System.DateTime.Now); + } + + public static DateTime DateTime(DateTime min) + { + return DateTime(min, System.DateTime.Now); + } + + /* http://stackoverflow.com/a/1483677/234132 */ + public static DateTime DateTime(DateTime min, DateTime max) + { + TimeSpan timeSpan = max - min; + TimeSpan newSpan = new TimeSpan(0, RandomHelper.Instance.Next(0, (int)timeSpan.TotalMinutes), 0); + + return min + newSpan; + } + + #endregion + + #region Text + + public static string Email() + { + return string.Format("{0}@{1}.com", Lorem.Words(1, false), Lorem.Words(1, false)); + } + + public static string Words(int wordCount, bool uppercaseFirstLetter = true, bool includePunctuation = false) + { + return Words(wordCount, wordCount, uppercaseFirstLetter, includePunctuation); + } + + public static string Words(int wordCountMin, int wordCountMax, bool uppercaseFirstLetter = true, bool includePunctuation = false) + { + var source = string.Join(" ", Source.WordList(includePunctuation).Take(RandomHelper.Instance.Next(wordCountMin, wordCountMax))); + + if (uppercaseFirstLetter) + { + source = source.UppercaseFirst(); + } + + return source; + } + + public static string Sentence(int wordCount) + { + return Sentence(wordCount, wordCount); + } + + public static string Sentence(int wordCountMin, int wordCountMax) + { + return string.Format("{0}.", Words(wordCountMin, wordCountMax, true, true)).Replace(",.", ".").Remove(".."); + } + + public static string Paragraph(int wordCount, int sentenceCount) + { + return Paragraph(wordCount, wordCount, sentenceCount, sentenceCount); + } + + public static string Paragraph(int wordCountMin, int wordCountMax, int sentenceCount) + { + return Paragraph(wordCountMin, wordCountMax, sentenceCount, sentenceCount); + } + + public static string Paragraph(int wordCountMin, int wordCountMax, int sentenceCountMin, int sentenceCountMax) + { + var source = string.Join(" ", Enumerable.Range(0, RandomHelper.Instance.Next(sentenceCountMin, sentenceCountMax)).Select(x => Sentence(wordCountMin, wordCountMax))); + + //remove traililng space + return source.Remove(source.Length - 1); + } + + public static IEnumerable Paragraphs(int wordCount, int sentenceCount, int paragraphCount) + { + return Paragraphs(wordCount, wordCount, sentenceCount, sentenceCount, paragraphCount, paragraphCount); + } + + public static IEnumerable Paragraphs(int wordCountMin, int wordCountMax, int sentenceCount, int paragraphCount) + { + return Paragraphs(wordCountMin, wordCountMax, sentenceCount, sentenceCount, paragraphCount, paragraphCount); + } + + public static IEnumerable Paragraphs(int wordCountMin, int wordCountMax, int sentenceCountMin, int sentenceCountMax, int paragraphCount) + { + return Paragraphs(wordCountMin, wordCountMax, sentenceCountMin, sentenceCountMax, paragraphCount, paragraphCount); + } + + public static IEnumerable Paragraphs(int wordCountMin, int wordCountMax, int sentenceCountMin, int sentenceCountMax, int paragraphCountMin, int paragraphCountMax) + { + return Enumerable.Range(0, RandomHelper.Instance.Next(paragraphCountMin, paragraphCountMax)).Select(p => Paragraph(wordCountMin, wordCountMax, sentenceCountMin, sentenceCountMax)).ToArray(); + } + + #endregion + } +} \ No newline at end of file diff --git a/Lorem.NET/Properties/AssemblyInfo.cs b/Lorem.NET/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9033b31 --- /dev/null +++ b/Lorem.NET/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Lorem.NET")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Lorem.NET")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("8af259ae-efba-4e62-a4db-9830488a4085")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Lorem.NET/RandomHelper.cs b/Lorem.NET/RandomHelper.cs new file mode 100644 index 0000000..1bab258 --- /dev/null +++ b/Lorem.NET/RandomHelper.cs @@ -0,0 +1,29 @@ +using System; +using System.Threading; + +namespace Lorem.NET +{ + /* + * http://stackoverflow.com/a/1785821/234132 + */ + public static class RandomHelper + { + private static int seedCounter = new Random().Next(); + + [ThreadStatic] + private static Random rng; + + public static Random Instance + { + get + { + if (rng == null) + { + int seed = Interlocked.Increment(ref seedCounter); + rng = new Random(seed); + } + return rng; + } + } + } +} \ No newline at end of file diff --git a/Lorem.NET/Source.cs b/Lorem.NET/Source.cs new file mode 100644 index 0000000..4b4de81 --- /dev/null +++ b/Lorem.NET/Source.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; +using System.Linq; + +namespace Lorem.NET +{ + internal class Source + { + const string LoremIpsum = @"lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua, ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat, duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur, excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum,"; + + internal static IEnumerable Rearrange(string words) + { + return words.Split(" ").OrderBy(x => RandomHelper.Instance.Next()); + } + + internal static IEnumerable WordList(bool includePuncation) + { + return includePuncation ? Rearrange(LoremIpsum) : Rearrange(LoremIpsum.Remove(",")); + } + } +} diff --git a/Lorem.NET/bin/Debug/Lorem.NET.dll b/Lorem.NET/bin/Debug/Lorem.NET.dll new file mode 100644 index 0000000..d9c3a88 Binary files /dev/null and b/Lorem.NET/bin/Debug/Lorem.NET.dll differ diff --git a/Lorem.NET/bin/Debug/Lorem.NET.pdb b/Lorem.NET/bin/Debug/Lorem.NET.pdb new file mode 100644 index 0000000..0341552 Binary files /dev/null and b/Lorem.NET/bin/Debug/Lorem.NET.pdb differ diff --git a/Lorem.NET/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/Lorem.NET/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache new file mode 100644 index 0000000..119112f Binary files /dev/null and b/Lorem.NET/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/Lorem.NET/obj/Debug/Lorem.NET.csproj.FileListAbsolute.txt b/Lorem.NET/obj/Debug/Lorem.NET.csproj.FileListAbsolute.txt new file mode 100644 index 0000000..e87e7e3 --- /dev/null +++ b/Lorem.NET/obj/Debug/Lorem.NET.csproj.FileListAbsolute.txt @@ -0,0 +1,5 @@ +C:\AJ\Personal\GitHub\Lorem.NET\Lorem.NET\bin\Debug\Lorem.NET.dll +C:\AJ\Personal\GitHub\Lorem.NET\Lorem.NET\bin\Debug\Lorem.NET.pdb +C:\AJ\Personal\GitHub\Lorem.NET\Lorem.NET\obj\Debug\Lorem.NET.csprojResolveAssemblyReference.cache +C:\AJ\Personal\GitHub\Lorem.NET\Lorem.NET\obj\Debug\Lorem.NET.dll +C:\AJ\Personal\GitHub\Lorem.NET\Lorem.NET\obj\Debug\Lorem.NET.pdb diff --git a/Lorem.NET/obj/Debug/Lorem.NET.csprojResolveAssemblyReference.cache b/Lorem.NET/obj/Debug/Lorem.NET.csprojResolveAssemblyReference.cache new file mode 100644 index 0000000..393819d Binary files /dev/null and b/Lorem.NET/obj/Debug/Lorem.NET.csprojResolveAssemblyReference.cache differ diff --git a/Lorem.NET/obj/Debug/Lorem.NET.dll b/Lorem.NET/obj/Debug/Lorem.NET.dll new file mode 100644 index 0000000..d9c3a88 Binary files /dev/null and b/Lorem.NET/obj/Debug/Lorem.NET.dll differ diff --git a/Lorem.NET/obj/Debug/Lorem.NET.pdb b/Lorem.NET/obj/Debug/Lorem.NET.pdb new file mode 100644 index 0000000..0341552 Binary files /dev/null and b/Lorem.NET/obj/Debug/Lorem.NET.pdb differ diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..6d9d0d7 --- /dev/null +++ b/readme.md @@ -0,0 +1,13 @@ +Lorem.NET +=============== + +A .NET library for all things random! + +Usage +--------------- + +### Text Helpers + +```csharp +Lorem.Words(2, 5); +``` \ No newline at end of file