Skip to content

Commit

Permalink
replaced SmartDictionaryOf with Tuple version of DictionaryOf
Browse files Browse the repository at this point in the history
  • Loading branch information
X1nto committed Jan 14, 2022
1 parent e5ae9e6 commit a119ae3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
42 changes: 23 additions & 19 deletions Sharplin/Dictionary/Builders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,39 @@
/// </example>
public static class Builders
{
/// <returns>A dictionary containing the provided <paramref name="pairs"/></returns>
/// <summary>Constructs a <see cref="Dictionary{TKey,TValue}"/> from the provided <
/// paramref name="pairs"/>
/// </summary>
/// <example>
/// <code>
/// var dic = DictionaryOf(
/// PairOf("number", 1),
/// PairOf("position": 2)
/// );
/// </code>
/// </example>
/// <returns>A <see cref="Dictionary{TKey,TValue}"/> containing the provided <paramref name="pairs"/></returns>

public static Dictionary<TKey, TValue> DictionaryOf<TKey, TValue>(params KeyValuePair<TKey, TValue>[] pairs)
where TKey : notnull =>
pairs.ToDictionary(pair => pair.Key, pair => pair.Value);

/// <summary>
/// Constructs a <see cref="Dictionary{TKey,TValue}"/> from the provided <paramref name="pairs"/>
/// <summary>Constructs a <see cref="Dictionary{TKey,TValue}"/> from the provided
/// <paramref name="pairs"/>
/// </summary>
/// <example>
/// <code>
/// var dic = SmartDictionaryOf&lt;string, int&gt;(
/// "number", 1,
/// "position": 2
/// var dic = DictionaryOf(
/// ("number", 1),
/// ("position": 2)
/// );
/// </code>
/// </example>
/// <returns>A <see cref="Dictionary{TKey,TValue}"/> of keys and values extracted from <paramref name="pairs"/></returns>
/// <remarks>Use this at the cost of your sanity.</remarks>
///
public static Dictionary<TKey, TValue> SmartDictionaryOf<TKey, TValue>(params object[] pairs)
where TKey : notnull
{
var dic = new Dictionary<TKey, TValue>();
for (int i = 0; i < pairs.Length; i += 2) {
dic.Add((TKey) pairs[i], (TValue) pairs[i + 1]);
}
return dic;
}

/// <returns>A <see cref="Dictionary{TKey,TValue}"/> containing the provided <paramref name="pairs"/></returns>
public static Dictionary<TKey, TValue> DictionaryOf<TKey, TValue>(params (TKey key, TValue value)[] pairs)
where TKey : notnull =>
pairs.ToDictionary(pair => pair.Item1, pair => pair.Item2);

/// <returns>
/// A <see cref="KeyValuePair{TKey,TValue}"/> of the provided <paramref name="key"/> and
/// <paramref name="value"/>
Expand Down
9 changes: 4 additions & 5 deletions Tests/Dictionary/Builders.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Tests.Dictionary;

using System;
using System.Collections.Generic;
using NUnit.Framework;
using Sharplin.Dictionary;
Expand All @@ -14,11 +13,11 @@ public class Builders
};

[Test]
public void Test_SmartDictionaryOf()
public void Test_DictionaryOf_Tuple()
{
var actual = SmartDictionaryOf<string, int>(
"number", 1,
"money", 50
var actual = DictionaryOf(
("number", 1),
("money", 50)
);
Assert.AreEqual(Expected, actual);
}
Expand Down

0 comments on commit a119ae3

Please sign in to comment.