Skip to content

Commit

Permalink
Merge pull request #1 from bolorundurowb/develop
Browse files Browse the repository at this point in the history
release: v1.0.2
  • Loading branch information
Bolorunduro Winner-Timothy B authored Jul 5, 2017
2 parents d49b989 + de43d35 commit 8c2ebc3
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 11 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
language: csharp
solution: shortid.sln
mono:
- 4.0.0
- latest
install:
- nuget restore shortid.sln
- cd ../../bolorundurowb/shortid
script:
- xbuild /p:Configuration=Release shortid.sln
- xbuild /p:Configuration=Debug shortid.sln
after_success:
- nuget install NUnit.Runners -Version 3.4.1
- mono ./NUnit.ConsoleRunner.3.4.1/tools/nunit3-console.exe ./shortid.Test/bin/Debug/shortid.Test.dll
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.

## How to use

To make use of the `shortid`, add it to your project via the Nuget package manager UI or console via this command:

```
Expand Down Expand Up @@ -56,4 +57,33 @@ string id = ShortId.Generate(true, false, 12);
// id = VvoCDPazES_w
```

**NOTE: when specifying the desired length, shorter lengths increase the possibility thata duplicate id would be generated**
**NOTE: when specifying the desired length, shorter lengths increase the possibility thata duplicate id would be generated**


## 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:

```csharp
string characters = //whatever you want;
ShortId.SetCharacters(characters);
```

**NOTE: the new character set must number `null`, an empty string or whitespace. Also, all whitespace characters would be removed, finally the character set cannot be less than 20 characters.**

`ShortId` also allows the seed for the random number generator to be set.

To set the seed, run the following:

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

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

```csharp
ShortId.Reset();
```
38 changes: 37 additions & 1 deletion shortid.Test/ShortId.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using NUnit.Framework;

namespace shortid.Test
Expand Down Expand Up @@ -41,5 +42,40 @@ public void GenerateCreatesIdsOfASpecifiedLength()
id = ShortId.Generate(false, true, 8);
Assert.AreEqual(id.Length, 8);
}

[Test]
public void SetSeedThrowsWhenCharacterSetIsEmptyOrNull()
{
string seed = String.Empty;
Assert.Throws<ArgumentException>(delegate
{
ShortId.SetCharacters(seed);
});
}

[Test]
public void SetSeedThrowsWhenCharacterSetIsLessThan20Characters()
{
string seed = "783ujrcuei039kj4";
Assert.Throws<InvalidOperationException>(delegate
{
ShortId.SetCharacters(seed);
});
}

[Test]
public void ResetIsStable()
{
Assert.DoesNotThrow(ShortId.Reset);
}

[Test]
public void SetSeedIsStable()
{
Assert.DoesNotThrow(delegate
{
ShortId.Generate(0);
});
}
}
}
59 changes: 52 additions & 7 deletions shortid/ShortId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ namespace shortid
{
public class ShortId
{
private static readonly Random Random = new Random();
private static Random _random = new Random();
private const string Capitals = "ABCDEFGHIJKLMNOPQRSTUVWXY";
private const string Smalls = "abcdefghjlkmnopqrstuvwxyz";
private const string Numbers = "0123456789";
private const string Specials = "-_";

private static string _pool = $"{Smalls}{Capitals}";

/// <summary>
/// Generates a random string of varying length
/// </summary>
Expand All @@ -18,23 +19,23 @@ public class ShortId
/// <returns>A random string</returns>
public static string Generate(bool useNumbers = false, bool useSpecial = true)
{
int length = Random.Next(7, 15);
int length = _random.Next(7, 15);
return Generate(useNumbers, useSpecial, length);
}

/// <summary>
/// Generates a random string of a specified length
/// Generates a random string of a specified length with the option to add numbers and special characters
/// </summary>
/// <param name="useNumbers">Whether or not numbers are included in the string</param>
/// <param name="useSpecial">Whether or not special characters are included</param>
/// <param name="length">The length of the generated string</param>
/// <returns>A random string</returns>
public static string Generate(bool useNumbers, bool useSpecial, int length)
{
string pool = $"{Capitals}{Smalls}";
string pool = _pool;
if (useNumbers)
{
pool += Numbers;
pool = Numbers + pool;
}
if (useSpecial)
{
Expand All @@ -44,7 +45,7 @@ public static string Generate(bool useNumbers, bool useSpecial, int length)
string output = string.Empty;
for (int i = 0; i < length; i++)
{
int charIndex = Random.Next(0, pool.Length);
int charIndex = _random.Next(0, pool.Length);
output += pool[charIndex];
}
return output;
Expand All @@ -59,5 +60,49 @@ public static string Generate(int length)
{
return Generate(false, true, length);
}

/// <summary>
/// Changes the character set that id's are generated from
/// </summary>
/// <param name="characters">The new character set</param>
/// <exception cref="InvalidOperationException">Thrown when the new character set is less than 20 characters</exception>
public static void SetCharacters(string characters)
{
if (string.IsNullOrWhiteSpace(characters))
{
throw new ArgumentException("The replacement characters must not be null or empty.");
}

characters = characters
.Replace(" ", "")
.Replace("\t", "")
.Replace("\n", "")
.Replace("\r", "");

if (characters.Length < 20)
{
throw new InvalidOperationException(
"The replacement characters must be at least 20 letters in length and without spaces.");
}
_pool = characters;
}

/// <summary>
/// Sets the seed that the random generator works with.
/// </summary>
/// <param name="seed">The seed for the random number generator</param>
public static void SetSeed(int seed)
{
_random = new Random(seed);
}

/// <summary>
/// Resets the random number generator and character set
/// </summary>
public static void Reset()
{
_random = new Random();
_pool = $"{Smalls}{Capitals}";
}
}
}

0 comments on commit 8c2ebc3

Please sign in to comment.