From fe519364dca2c7c5c4b94a5d346cf77819f46b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Havl=C3=AD=C4=8Dek?= Date: Sat, 16 Sep 2023 08:24:12 +0200 Subject: [PATCH] Add support for `Dictionary(TKey key, out TValue value) Remove` (#93) --- api_list.include.md | 5 +++ readme.md | 5 +++ src/Polyfill/PolyfillExtensions_Dictionary.cs | 35 +++++++++++++++++++ .../PolyfillExtensionsTests_Dictionary.cs | 27 ++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 src/Polyfill/PolyfillExtensions_Dictionary.cs create mode 100644 src/Tests/PolyfillExtensionsTests_Dictionary.cs diff --git a/api_list.include.md b/api_list.include.md index 99cd4422..c8ca2f07 100644 --- a/api_list.include.md +++ b/api_list.include.md @@ -54,6 +54,11 @@ * `Boolean TryFormat(Span, Int32&, ReadOnlySpan, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.tryformat) +### Dictionary + + * `Boolean Remove(TKey, out TValue)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove) + + ### Double * `Boolean TryFormat(Span, Int32&, ReadOnlySpan, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat) diff --git a/readme.md b/readme.md index 985e8172..efdd4a94 100644 --- a/readme.md +++ b/readme.md @@ -407,6 +407,11 @@ The class `PolyfillExtensions` includes the following extension methods: * `Boolean TryFormat(Span, Int32&, ReadOnlySpan, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.tryformat) +### Dictionary + + * `Boolean Remove(TKey, out TValue)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove) + + ### Double * `Boolean TryFormat(Span, Int32&, ReadOnlySpan, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat) diff --git a/src/Polyfill/PolyfillExtensions_Dictionary.cs b/src/Polyfill/PolyfillExtensions_Dictionary.cs new file mode 100644 index 00000000..a3d9b67e --- /dev/null +++ b/src/Polyfill/PolyfillExtensions_Dictionary.cs @@ -0,0 +1,35 @@ +// + +#pragma warning disable + +#if NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2X + +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using Link = System.ComponentModel.DescriptionAttribute; + +static partial class PolyfillExtensions +{ + /// + /// Removes the value with the specified key from the , and copies the element + /// to the value parameter. + /// + /// A dictionary with keys of type TKey and values of type TValue. + /// The key of the element to remove. + /// The removed element. + /// The type of the keys in the dictionary. + /// The type of the values in the dictionary. + /// true if the element is successfully found and removed; otherwise, false. + /// is null. + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove")] + public static bool Remove( + this Dictionary target, + TKey key, + [MaybeNullWhen(false)] out TValue value) + { + target.TryGetValue(key, out value); + return target.Remove(key); + } +} +#endif \ No newline at end of file diff --git a/src/Tests/PolyfillExtensionsTests_Dictionary.cs b/src/Tests/PolyfillExtensionsTests_Dictionary.cs new file mode 100644 index 00000000..6df1930d --- /dev/null +++ b/src/Tests/PolyfillExtensionsTests_Dictionary.cs @@ -0,0 +1,27 @@ +partial class PolyfillExtensionsTests +{ + [Test] + public void Dictionary_Remove() + { + var dictionary = new Dictionary { {"key", "value"} }; + + Assert.True(dictionary.Remove("key", out var value)); + Assert.AreEqual("value", value); + } + + [Test] + public void Dictionary_Remove_DoesntThrowOnMissingKey() + { + var dictionary = new Dictionary(); + + Assert.False(dictionary.Remove("non-existent key", out var value)); + Assert.AreEqual(default, value); + } + + [Test] + public void Dictionary_Remove_ThrowsOnNull() + { + var dictionary = new Dictionary(); + Assert.Throws(() => dictionary.Remove(null!, out _)); + } +}