Skip to content

Commit ff5e80b

Browse files
Add convenience methods.
1 parent 5ea9f3a commit ff5e80b

2 files changed

Lines changed: 91 additions & 7 deletions

File tree

src/ImageSharp.Web/Commands/CommandCollection.cs

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,12 @@ public IEnumerable<string> Keys
5555
{
5656
get
5757
{
58-
if (this.TryGetValue(key, out KeyValuePair<string, string> item))
58+
if (!this.TryGetValue(key, out string value))
5959
{
60-
return item.Value;
60+
ThrowKeyNotFound();
6161
}
6262

63-
ThrowKeyNotFound();
64-
return default;
63+
return value;
6564
}
6665

6766
set
@@ -72,11 +71,55 @@ public IEnumerable<string> Keys
7271
}
7372
else
7473
{
75-
this.Add(new(key, value));
74+
this.Add(key, value);
7675
}
7776
}
7877
}
7978

79+
/// <summary>
80+
/// Adds an element with the provided key and value to the <see cref="CommandCollection"/>.
81+
/// </summary>
82+
/// <param name="key">The <see cref="string"/> to use as the key of the element to add.</param>
83+
/// <param name="value">The <see cref="string"/> to use as the value of the element to add.</param>
84+
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
85+
public void Add(string key, string value) => this.Add(new(key, value));
86+
87+
/// <summary>
88+
/// Inserts an element into the <see cref="CommandCollection"/> at the
89+
/// specified index.
90+
/// </summary>
91+
/// <param name="index">The zero-based index at which item should be inserted.</param>
92+
/// <param name="key">The <see cref="string"/> to use as the key of the element to insert.</param>
93+
/// <param name="value">The <see cref="string"/> to use as the value of the element to insert.</param>
94+
/// <exception cref="ArgumentOutOfRangeException">index is less than zero. -or- index is greater than <see cref="P:CommandCollection.Count"/>.</exception>
95+
public void Insert(int index, string key, string value) => this.Insert(index, new(key, value));
96+
97+
/// <summary>
98+
/// Gets the value associated with the specified key.
99+
/// </summary>
100+
/// <param name="key">The key whose value to get.</param>
101+
/// <param name="value">
102+
/// When this method returns, the value associated with the specified key, if the
103+
/// key is found; otherwise, the default value for the type of the value parameter.
104+
/// This parameter is passed uninitialized.
105+
/// </param>
106+
/// <returns>
107+
/// <see langword="true"/> if the object that implements <see cref="CommandCollection"/> contains
108+
/// an element with the specified key; otherwise, <see langword="false"/>.
109+
/// </returns>
110+
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
111+
public bool TryGetValue(string key, out string value)
112+
{
113+
if (this.TryGetValue(key, out KeyValuePair<string, string> keyValue))
114+
{
115+
value = keyValue.Value;
116+
return true;
117+
}
118+
119+
value = default;
120+
return false;
121+
}
122+
80123
/// <summary>
81124
/// Searches for an element that matches the conditions defined by the specified
82125
/// predicate, and returns the zero-based index of the first occurrence within the
@@ -109,6 +152,25 @@ public int FindIndex(Predicate<string> match)
109152
return -1;
110153
}
111154

155+
/// <summary>
156+
/// Searches for the specified key and returns the zero-based index of the first
157+
/// occurrence within the entire <see cref="CommandCollection"/>.
158+
/// </summary>
159+
/// <param name="key">The key to locate in the <see cref="CommandCollection"/>.</param>
160+
/// <returns>
161+
/// The zero-based index of the first occurrence of key within the entire <see cref="CommandCollection"/>,
162+
/// if found; otherwise, -1.
163+
/// </returns>
164+
public int IndexOf(string key)
165+
{
166+
if (this.TryGetValue(key, out KeyValuePair<string, string> item))
167+
{
168+
return this.IndexOf(item);
169+
}
170+
171+
return -1;
172+
}
173+
112174
/// <inheritdoc/>
113175
[MethodImpl(MethodImplOptions.AggressiveInlining)]
114176
protected override string GetKeyForItem(KeyValuePair<string, string> item) => item.Key;

tests/ImageSharp.Web.Tests/Commands/CommandCollectionTests.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ public void CanInsertCommands()
4848
Assert.Equal(0, collection.IndexOf(kv2));
4949
}
5050

51+
[Fact]
52+
public void CanInsertCommandsViaKey()
53+
{
54+
KeyValuePair<string, string> kv1 = new("a", "b");
55+
KeyValuePair<string, string> kv2 = new("c", "d");
56+
57+
CommandCollection collection = new();
58+
collection.Add(kv1);
59+
Assert.Single(collection);
60+
61+
collection.Insert(0, kv2.Key, kv2.Value);
62+
Assert.Equal(2, collection.Count);
63+
64+
Assert.Equal(kv1.Value, collection[kv1.Key]);
65+
Assert.Equal(kv2.Value, collection[kv2.Key]);
66+
67+
Assert.Equal(1, collection.IndexOf(kv1));
68+
Assert.Equal(0, collection.IndexOf(kv2));
69+
}
70+
5171
[Fact]
5272
public void CanSetCommandsViaIndex()
5373
{
@@ -148,10 +168,12 @@ public void KeysAreOrdered()
148168
collection.Insert(0, new(key, null));
149169
}
150170

151-
int counter = keys.Length - 1;
171+
int index = 0;
172+
int offset = keys.Length - 1;
152173
foreach (string key in collection.Keys)
153174
{
154-
Assert.Equal(keys[counter--], key);
175+
Assert.Equal(index++, collection.IndexOf(key));
176+
Assert.Equal(keys[offset--], key);
155177
}
156178
}
157179

0 commit comments

Comments
 (0)