From b387e741c332db8a82194e510f9cf7d9cbfb9b07 Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Tue, 25 Aug 2020 15:38:37 +0100 Subject: [PATCH] ft: update readme --- README.md | 58 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 77715ed..5801a27 100644 --- a/README.md +++ b/README.md @@ -4,61 +4,79 @@ ## About ShortId -A CSharp library to generate completely random short id's. They can be used as primary keys or unique identifiers. This library is different in that you can specify the length of the id's generated. I have tested the application generating 180000 id's without duplicates. +A CSharp library to generate completely random short id's. They can be used as primary keys or unique identifiers. This library is different in that you can specify the length of the ids to be generated. This library is thread-safe and can generate millions of unique ids across multiple threads. -## How to use +## Getting Started To make use of the `shortid`, add it to your project via the Nuget package manager UI or console via this command: +#### Package Manager + ``` Install-Package shortid ``` +#### .NET CLI +``` +> dotnet add package shortid +``` + +#### PackageReference +```csharp + +``` + +## Usage + Add the following using command to the top of your csharp code file: ```csharp using shortid; +using shortid.Configuration; ``` This gives your code access the classes and methods of the `shortid` namespace. -To generate a unique id of any length between 7 and 14, you call the `Generate` method without parameters. +To generate a unique id of any length between 8 and 14, you call the `Generate` method without parameters. ```csharp string id = ShortId.Generate(); // id = KXTR_VzGVUoOY ``` -If you want to include numbers in the generated id, then you call the `Generate` method with a boolean indicating your preference. +If you want to include numbers in the generated id, then you call the `Generate` method with options indicating your preference. ```csharp -string id = ShortId.Generate(true); +var options = new GenerationOptions +{ + UseNumbers = true +}; +string id = ShortId.Generate(options); // id = O_bBY-YUkJg ``` -If you do not want special characters *i.e _ and -* in your generated id, then call the `Generate` method with two boolean parameters, the first indicating whether or not you want numbers and the second indicating whether or not you want special characeters. +If you do not want special characters *i.e _ and -* in your generated id, then call the `Generate` method with options indicating your preferences. ```csharp -string id = ShortId.Generate(true, false); +var options = new GenerationOptions +{ + UseSpecialCharacters = false +}; +string id = ShortId.Generate(options); // id = waBfk3z ``` -If you want to specify the length of the generated id, call the `Generate` method with an integer parameter which is the desired length. - -```csharp -string id = ShortId.Generate(8); -// id = M-snXzBk -``` - -If you want to control the type of id generated by specifying whether you want numbers, special characters and the length, call the `Generate` method and pass three parameters, the first a boolean stating whether you want numbers, the second a boolean stating whether you want special characters, the last a number indicating your length preference. +If you want to specify the length of the generated id, call the `Generate` method with options indicating your preferences. ```csharp -string id = ShortId.Generate(true, false, 12); -// id = VvoCDPazES_w +var options = new GenerationOptions +{ + Length = 9 +}; +string id = ShortId.Generate(options); +// id = M-snXzBkj ``` -**NOTE: v2.0.0 introduced a change that prevents lengths of less than 7** - ## Customize ShortId @@ -71,7 +89,7 @@ string characters = //whatever you want; ShortId.SetCharacters(characters); ``` -**NOTE: the new character set must not be `null`, an empty string or whitespace. Also, all whitespace characters would be removed, finally the character set cannot be less than 20 characters.** +**NOTE: the new character set must not be `null`, an empty string or whitespace. Also, all whitespace and duplicate characters would be removed, finally the character set cannot be less than 50 characters.** `ShortId` also allows the seed for the random number generator to be set.