diff --git a/Core/Collection/List/ListExtensions.cs b/Core/Collection/List/ListExtensions.cs index e567ec1..2389718 100644 --- a/Core/Collection/List/ListExtensions.cs +++ b/Core/Collection/List/ListExtensions.cs @@ -17,6 +17,34 @@ namespace Trivial.Collection; /// public static partial class ListExtensions { + /// + /// Tries to get the string value. + /// + /// The source dictionary. + /// The key. + /// The value output. + /// true if exist and not empty; otherwise, false. + /// dict or key was null. + public static bool TryGetNotEmptyValue(this IDictionary dict, string key, out string value) + { + if (dict == null) throw new ArgumentNullException(nameof(dict), "dict should not be null."); + value = dict.TryGetValue(key, out var v) && !string.IsNullOrEmpty(v) ? v : null; + return v != null; + } + + /// + /// Tries to get the string value. + /// + /// The source dictionary. + /// The key. + /// The value; or null if not exist or empty. + /// dict or key was null. + public static string TryGetNotEmptyValue(this IDictionary dict, string key) + { + if (dict == null) throw new ArgumentNullException(nameof(dict), "dict should not be null."); + return dict.TryGetValue(key, out var v) && !string.IsNullOrEmpty(v) ? v : null; + } + /// /// Adds a key and a value to the end of the key value pairs. /// diff --git a/Core/Text/Json/ObjectNode.cs b/Core/Text/Json/ObjectNode.cs index 6e81171..bc1cb10 100644 --- a/Core/Text/Json/ObjectNode.cs +++ b/Core/Text/Json/ObjectNode.cs @@ -1080,6 +1080,21 @@ public IJsonDataNode GetValue(string key) return store[key] ?? JsonValues.Null; } + /// + /// Gets the value of the specific property. + /// + /// The property key. + /// true if return undefined when the property does not exist; otherwise, false. + /// The value. + /// The property key should not be null, empty, or consists only of white-space characters. + /// The property does not exist and argument undefined is false. + public IJsonDataNode GetValue(string key, bool undefined) + { + AssertKey(key); + if (!undefined) return store[key] ?? JsonValues.Null; + return store.TryGetValue(key, out var v) ? (v ?? JsonValues.Null) : JsonValues.Undefined; + } + /// /// Gets the value of the specific property. ///