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 4 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
74 changes: 74 additions & 0 deletions src/Hashids.net/Hashids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,80 @@ public string EncodeLong(long number)
/// <returns>Array of 64-bit integers.</returns>
public long[] DecodeLong(string hash) => GetNumbersFrom(hash);

/// <summary>
/// Decodes the provided hash into numbers.
Cereal-Killa marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="hash">Hash string to decode.</param>
/// <returns>A 64-bit integer.</returns>
/// <exception cref="T:HashidsNet.MultipleResultsException">If the decoded hash returns more than one long</exception>
public long DecodeSingleLong(string hash)
{
var numbers = GetNumbersFrom(hash);

if (numbers.Length == 1) return numbers[0];
else throw new MultipleResultsException("The hash provided yielded more than one result.");
Cereal-Killa marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Decodes the provided hash into numbers.
/// </summary>
/// <param name="hash">Hash string to decode.</param>
/// <param name="id">A 64-bit integer variable to output the result to.</param>
/// <returns>A 64-bit integer.</returns>
public bool DecodeSingleLong(string hash, out long id)
Cereal-Killa marked this conversation as resolved.
Show resolved Hide resolved
{
var numbers = GetNumbersFrom(hash);

if (numbers.Length == 1)
{
id = numbers[0];
return true;
}
else
{
id = -1;
Cereal-Killa marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}

/// <summary>
/// Decodes the provided hash into a number.
/// </summary>
/// <param name="hash">Hash string to decode.</param>
/// <returns>An integer.</returns>
/// <exception cref="T:System.OverflowException">If the decoded number overflows integer.</exception>
/// <exception cref="T:HashidsNet.MultipleResultsException">If the decoded hash returns more than one integer</exception>
Cereal-Killa marked this conversation as resolved.
Show resolved Hide resolved
public virtual int DecodeSingle(string hash)
{
var numbers = GetNumbersFrom(hash);

if (numbers.Length == 1) return (int)numbers[0];
else throw new MultipleResultsException("The hash provided yielded more than one result.");
Cereal-Killa marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Decodes the provided hash into a number.
/// </summary>
/// <param name="hash">Hash string to decode.</param>
/// <param name="id">An integer variable to output the result to.</param>
/// <returns>An integer greater or equal to zero or -1 if the hash conversion yields more than one result.</returns>
/// <exception cref="T:System.OverflowException">If the decoded number overflows integer.</exception>
public virtual bool DecodeSingle(string hash, out int id)
Cereal-Killa marked this conversation as resolved.
Show resolved Hide resolved
{
var numbers = GetNumbersFrom(hash);

if (numbers.Length == 1)
{
id = (int)numbers[0];
return true;
}
else
{
id = -1;
Cereal-Killa marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}

/// <summary>
/// Encodes the provided hex-string into a hash string.
/// </summary>
Expand Down
33 changes: 33 additions & 0 deletions src/Hashids.net/IHashids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,46 @@ 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.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 -1 if the hash yields more than one result</returns>
bool DecodeSingle(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>
/// <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 -1 if the hash yields more than one result</returns>
bool DecodeSingleLong(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) { }
}
}
90 changes: 90 additions & 0 deletions test/Hashids.net.test/GeneralTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,48 @@ 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<MultipleResultsException>(() => _hashids.DecodeSingle("NkK9,NkK9").Should().Be(12345));
Assert.Throws<MultipleResultsException>(() => _hashids.DecodeSingle("5O8yp5P,5O8yp5P").Should().Be(12345));
Assert.Throws<MultipleResultsException>(() => _hashids.DecodeSingle("Wzo,Wzo").Should().Be(12345));
Assert.Throws<MultipleResultsException>(() => _hashids.DecodeSingle("DbE,DbE").Should().Be(12345));
Assert.Throws<MultipleResultsException>(() => _hashids.DecodeSingle("yj8,yj8").Should().Be(12345));
}

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

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

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

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

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

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

[Fact]
public void SingleInt_Decodes()
{
Expand Down Expand Up @@ -82,6 +124,54 @@ 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<MultipleResultsException>(() => _hashids.DecodeSingleLong("NV,NV").Should().Be(1L));
Assert.Throws<MultipleResultsException>(() => _hashids.DecodeSingleLong("21OjjRK,21OjjRK").Should().Be(2147483648L));
Assert.Throws<MultipleResultsException>(() => _hashids.DecodeSingleLong("D54yen6,D54yen6").Should().Be(4294967296L));
Assert.Throws<MultipleResultsException>(() => _hashids.DecodeSingleLong("KVO9yy1oO5j,KVO9yy1oO5j").Should().Be(666555444333222L));
Assert.Throws<MultipleResultsException>(() => _hashids.DecodeSingleLong("4bNP1L26r,4bNP1L26r").Should().Be(12345678901112L));
Assert.Throws<MultipleResultsException>(() => _hashids.DecodeSingleLong("jvNx4BjM5KYjv,jvNx4BjM5KYjv").Should().Be(Int64.MaxValue));
}

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

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

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

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

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

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

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

[Fact]
public void ListOfInt_Encodes()
{
Expand Down