Giter VIP home page Giter VIP logo

shortid's Introduction

shortid

Build Status License: MIT NuGet Badge Coverage Status

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 ids to be generated. This library is thread-safe and can generate millions of unique ids across multiple threads.

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

<PackageReference Include="shortid" />

Usage

Add the following using command to the top of your csharp code file:

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 8 and 15, you call the Generate method without parameters.

string id = ShortId.Generate();
// id = KXTR_VzGVUoOY

If you want to include numbers in the generated id, then you call the Generate method with options indicating your preference.

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 options indicating your preferences.

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 options indicating your preferences.

var options = new GenerationOptions(length: 9);
string id = ShortId.Generate(options);
// id = M-snXzBkj

Customize ShortId

ShortId has several features that help with customizing the ids generated. Characters sets can be introduced and the random number generator can be seeded.

To change the character set in use, run the following:

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 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.

To set the seed, run the following:

int seed = 1939048828;
ShortId.SetSeed(seed);

Finally, ShortId allows for all customizations to be reset using the following:

ShortId.Reset();

shortid's People

Contributors

bolorundurovj avatar vanillajonathan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

shortid's Issues

Are you trying to port dylang's shortid to C#? Problems with approach...

I'm toying with the idea of creating my own port, and I saw your library and thought "maybe I won't need to". Unfortunately, I don't think I can use this as-is -- there are a few issues with your approach compared to node's "shortid" library:

  1. It's stateless. Node's version keeps track of how many times you've asked for a unique ID in a very short period of time. This stateful behavior helps ensure the uniqueness of IDs.
  2. Length is random between 7-14. Node's version only increases the length of the unique ID if you need to, to guarantee uniqueness. The more times you ask for an ID within the same second, the longer the ID becomes.
  3. No support for clusters. Node's version allows up to 16 machines in a cluster to generate unique IDs without the possibility of collisions.

Thoughts?

Emits only "aaaaaa" after some time due to wrong use of Math.Random

I've used your code in a multithreaded server environment, but after some time only "aaaaaaa" is emitted. This is probably due to the fact that you're using the same Math.Random instance across multiple threads. You shouldn't do that: https://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396#ThreadSafety

Especially (bold by me):

Instead of instantiating individual Random objects, we recommend that you create a single Random instance to generate all the random numbers needed by your app. However, Random objects are not thread safe. If your app calls Random methods from multiple threads, you must use a synchronization object to ensure that only one thread can access the random number generator at a time. If you don't ensure that the Random object is accessed in a thread-safe way, calls to methods that return random numbers return 0.

Missing letters

Hi, I am curious why did you skip O and Z letters in Bigs?

private const string Bigs = "ABCDEFGHIJKLMNPQRSTUVWXY";

API proposal

Replace the method overloads with a options class.

public class ShortIdOptions
{
    public bool IncludeNumbers { get; set; }
    public bool IncludeSpecialCharacters { get; set; }
    public int Length { get; set; }
}

Signature:

public static string Generate(ShortIdOptions options)

Usage:

var options = new ShortIdOptions
{
    IncludeNumbers = true;
    IncludeSpecialCharacters = true;
    Length = 12;
};
string id = ShortId.Generate(options);

Move GenerationOptions

Move /Configuration/GenerationOptions.cs to /GenerationOptions.cs. Moving it away from a separate directory into the source root eases discoverability of the class.

Change the namespace from "shortid.Configuration" to "shortid".

URL safe

Hi,

Please can you confirm if the shortid is url safe?
Or need to make them URL safe?

Please advice & suggest.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.