Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode a single id. #57

Merged
merged 16 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/Hashids.net/Hashids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,68 @@ public string EncodeLong(long number)
/// <returns>Array of 64-bit integers.</returns>
public long[] DecodeLong(string hash) => GetNumbersFrom(hash);

/// <inheritdoc />
public long DecodeSingleLong(string hash)
{
var numbers = GetNumbersFrom(hash);

if (numbers.Length == 0)
throw new NoResultException("The hash provided yielded no result.");

if (numbers.Length > 1)
throw new MultipleResultsException("The hash provided yielded more than one result.");

return numbers[0];
}

/// <inheritdoc />
public bool TryDecodeSingleLong(string hash, out long id)
{
var numbers = GetNumbersFrom(hash);

if (numbers.Length == 1)
{
id = numbers[0];
return true;
}
else
{
id = 0L;
return false;
}
}

/// <inheritdoc />
public virtual int DecodeSingle(string hash)
{
var numbers = GetNumbersFrom(hash);

if (numbers.Length == 0)
throw new NoResultException("The hash provided yielded no result.");

if (numbers.Length > 1)
throw new MultipleResultsException("The hash provided yielded more than one result.");

return (int)numbers[0];
}

/// <inheritdoc />
public virtual bool TryDecodeSingle(string hash, out int id)
{
var numbers = GetNumbersFrom(hash);

if (numbers.Length == 1)
{
id = (int)numbers[0];
return true;
}
else
{
id = 0;
return false;
}
}

/// <summary>
/// Encodes the provided hex-string into a hash string.
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions src/Hashids.net/IHashids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,49 @@ public interface IHashids
/// <returns>the numbers</returns>
int[] Decode(string hash);

/// <summary>
/// Decodes the provided hashed string.
/// </summary>
/// <param name="hash">the hashed string</param>
/// <exception cref="T:System.OverflowException">if the number in the hash overflows the integer storage</exception>
/// <exception cref="T:HashidsNet.NoResultException">If the decoded hash does not return any value</exception>
/// <exception cref="T:HashidsNet.MultipleResultsException">If the decoded hash returns more than one integer</exception>
/// <returns>the number</returns>
int DecodeSingle(string hash);

/// <summary>
/// Decodes the provided hashed string.
/// </summary>
/// <param name="hash">the hashed string</param>
/// <param name="id">An integer variable to output the result to.</param>
/// <exception cref="T:System.OverflowException">if the number in the hash overflows the integer storage</exception>
/// <returns>the number or 0 if the hash yields more than one result</returns>
bool TryDecodeSingle(string hash, out int id);

/// <summary>
/// Decodes the provided hashed string into longs
/// </summary>
/// <param name="hash">the hashed string</param>
/// <returns>the numbers</returns>
long[] DecodeLong(string hash);

/// <summary>
/// Decodes the provided hashed string into a long
/// </summary>
/// <param name="hash">the hashed string</param>
/// <exception cref="T:HashidsNet.NoResultException">If the decoded hash does not return any value</exception>
/// <exception cref="T:HashidsNet.MultipleResultsException">If the decoded hash returns more than one integer</exception>
/// <returns>the number</returns>
long DecodeSingleLong(string hash);

/// <summary>
/// Decodes the provided hashed string into a long
/// </summary>
/// <param name="hash">the hashed string</param>
/// <param name="id">An 64-bit integer variable to output the result to.</param>
/// <returns>the number or 0 if the hash yields more than one result</returns>
bool TryDecodeSingleLong(string hash, out long id);

/// <summary>
/// Decodes the provided hashed string into a hex string
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/Hashids.net/MultipleResultsException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace HashidsNet
{
public class MultipleResultsException : Exception
{
public MultipleResultsException() { }

public MultipleResultsException(string message) : base(message) { }
}
}
11 changes: 11 additions & 0 deletions src/Hashids.net/NoResultException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace HashidsNet
{
public class NoResultException : Exception
{
public NoResultException() { }

public NoResultException(string message) : base(message) { }
}
}
83 changes: 83 additions & 0 deletions test/Hashids.net.test/GeneralTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,45 @@ public void SingleInt_Encodes()
_hashids.Encode(987654321).Should().Be("oyjYvry");
}

[Fact]
public void SingleReturn_Decodes()
{
_hashids.DecodeSingle("NkK9").Should().Be(12345);
_hashids.DecodeSingle("5O8yp5P").Should().Be(666555444);
_hashids.DecodeSingle("Wzo").Should().Be(1337);
_hashids.DecodeSingle("DbE").Should().Be(808);
_hashids.DecodeSingle("yj8").Should().Be(303);

Assert.Throws<NoResultException>(() => _hashids.DecodeSingle(string.Empty));
Assert.Throws<MultipleResultsException>(() => _hashids.DecodeSingle("aBMswoO2UB3Sj"));
}

[Fact]
public void SingleReturnOut_Decodes()
{
int value;

_hashids.TryDecodeSingle("NkK9,NkK9", out value).Should().Be(false);
_hashids.TryDecodeSingle("NkK9", out value).Should().Be(true);
value.Should().Be(12345);

_hashids.TryDecodeSingle("5O8yp5P,5O8yp5P", out value).Should().Be(false);
_hashids.TryDecodeSingle("5O8yp5P", out value).Should().Be(true);
value.Should().Be(666555444);

_hashids.TryDecodeSingle("Wzo,Wzo", out value).Should().Be(false);
_hashids.TryDecodeSingle("Wzo", out value).Should().Be(true);
value.Should().Be(1337);

_hashids.TryDecodeSingle("DbE,DbE", out value).Should().Be(false);
_hashids.TryDecodeSingle("DbE", out value).Should().Be(true);
value.Should().Be(808);

_hashids.TryDecodeSingle("yj8,yj8", out value).Should().Be(false);
_hashids.TryDecodeSingle("yj8", out value).Should().Be(true);
value.Should().Be(303);
}

[Fact]
public void SingleInt_Decodes()
{
Expand Down Expand Up @@ -82,6 +121,50 @@ public void SingleLong_Decode()
_hashids.DecodeLong("jvNx4BjM5KYjv").Should().Equal(new[] { Int64.MaxValue });
}

[Fact]
public void SingleReturnLong_Decode()
{
_hashids.DecodeSingleLong("NV").Should().Be(1L);
_hashids.DecodeSingleLong("21OjjRK").Should().Be(2147483648L);
_hashids.DecodeSingleLong("D54yen6").Should().Be(4294967296L);
_hashids.DecodeSingleLong("KVO9yy1oO5j").Should().Be(666555444333222L);
_hashids.DecodeSingleLong("4bNP1L26r").Should().Be(12345678901112L);
_hashids.DecodeSingleLong("jvNx4BjM5KYjv").Should().Be(Int64.MaxValue);

Assert.Throws<NoResultException>(() => _hashids.DecodeSingleLong(string.Empty));
Assert.Throws<MultipleResultsException>(() => _hashids.DecodeSingleLong("6gH3kPY7MJ9zjM3"));
}

[Fact]
public void SingleReturnOutLong_Decodes()
{
long value;

_hashids.TryDecodeSingleLong("NV,NV", out value).Should().Be(false);
_hashids.TryDecodeSingleLong("NV", out value).Should().Be(true);
value.Should().Be(1L);

_hashids.TryDecodeSingleLong("21OjjRK,21OjjRK", out value).Should().Be(false);
_hashids.TryDecodeSingleLong("21OjjRK", out value).Should().Be(true);
value.Should().Be(2147483648L);

_hashids.TryDecodeSingleLong("D54yen6,D54yen6", out value).Should().Be(false);
_hashids.TryDecodeSingleLong("D54yen6", out value).Should().Be(true);
value.Should().Be(4294967296L);

_hashids.TryDecodeSingleLong("KVO9yy1oO5j,KVO9yy1oO5j", out value).Should().Be(false);
_hashids.TryDecodeSingleLong("KVO9yy1oO5j", out value).Should().Be(true);
value.Should().Be(666555444333222L);

_hashids.TryDecodeSingleLong("4bNP1L26r,4bNP1L26r", out value).Should().Be(false);
_hashids.TryDecodeSingleLong("4bNP1L26r", out value).Should().Be(true);
value.Should().Be(12345678901112L);

_hashids.TryDecodeSingleLong("jvNx4BjM5KYjv,jvNx4BjM5KYjv", out value).Should().Be(false);
_hashids.TryDecodeSingleLong("jvNx4BjM5KYjv", out value).Should().Be(true);
value.Should().Be(Int64.MaxValue);
}

[Fact]
public void ListOfInt_Encodes()
{
Expand Down