aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-11-05 09:15:30 -0500
committerGitHub <noreply@github.com>2021-11-05 09:15:30 -0500
commit3a38aa8de76d1aa60ea0ccb29eacd7c458958a7b (patch)
tree56ef65a9652b6e68176d5c730dfd734fd1c4d322
parentdfcc7d1968cb78445b3db4cf9a49b194552fabc6 (diff)
parent451c2e743ad85721e1b5aa33d5d390af1665cd37 (diff)
downloadslskd-3a38aa8de76d1aa60ea0ccb29eacd7c458958a7b.tar.gz
Merge branch 'master' into dependabot/npm_and_yarn/src/web/color-string-1.6.0
-rw-r--r--src/slskd/Common/Extensions.cs221
-rw-r--r--src/web/package-lock.json14
-rw-r--r--src/web/package.json2
3 files changed, 124 insertions, 113 deletions
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs
index 4f2cf29f..30b35d14 100644
--- a/src/slskd/Common/Extensions.cs
+++ b/src/slskd/Common/Extensions.cs
@@ -23,6 +23,7 @@ namespace slskd
using System.Linq;
using System.Reflection;
using System.Text.Json;
+ using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
/// <summary>
@@ -31,88 +32,6 @@ namespace slskd
public static class Extensions
{
/// <summary>
- /// Serializes this object to json.
- /// </summary>
- /// <param name="obj">The object to serialize.</param>
- /// <returns>A string containing the serialized object.</returns>
- public static string ToJson(this object obj) => JsonSerializer.Serialize(obj);
-
- /// <summary>
- /// Deserializes this string from json to an object of type <typeparamref name="T"/>.
- /// </summary>
- /// <typeparam name="T">The type to which to deserialize the string.</typeparam>
- /// <param name="str">The string to deserialize.</param>
- /// <returns>The new object deserialzied from the string.</returns>
- public static T ToObject<T>(this string str) => JsonSerializer.Deserialize<T>(str);
-
- /// <summary>
- /// Recursively retrieves all properties.
- /// </summary>
- /// <param name="type">The type from which to retrieve properties.</param>
- /// <returns>The list of properties.</returns>
- public static IEnumerable<PropertyInfo> GetPropertiesRecursively(this Type type)
- {
- var props = new List<PropertyInfo>();
-
- foreach (var prop in type.GetProperties())
- {
- if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsArray || Nullable.GetUnderlyingType(prop.PropertyType) != null || new[] { typeof(string), typeof(decimal) }.Contains(prop.PropertyType))
- {
- props.Add(prop);
- }
- else
- {
- props.AddRange(prop.PropertyType.GetPropertiesRecursively());
- }
- }
-
- return props;
- }
-
- /// <summary>
- /// Replaces the first occurance of <paramref name="phrase"/> in the string with <paramref name="replacement"/>.
- /// </summary>
- /// <param name="str">The string on which to perform the replacement.</param>
- /// <param name="phrase">The phrase or substring to replace.</param>
- /// <param name="replacement">The replacement string.</param>
- /// <returns>The string, with the desired phrase replaced.</returns>
- public static string ReplaceFirst(this string str, string phrase, string replacement)
- {
- int pos = str.IndexOf(phrase);
-
- if (pos < 0)
- {
- return str;
- }
-
- return str.Substring(0, pos) + replacement + str.Substring(pos + phrase.Length);
- }
-
- /// <summary>
- /// Determines whether the string is a valid regular expression.
- /// </summary>
- /// <param name="pattern">The string to validate.</param>
- /// <returns>A value indicating whether the string is a valid regular expression.</returns>
- public static bool IsValidRegex(this string pattern)
- {
- if (string.IsNullOrWhiteSpace(pattern))
- {
- return false;
- }
-
- try
- {
- Regex.Match(string.Empty, pattern);
- }
- catch (ArgumentException)
- {
- return false;
- }
-
- return true;
- }
-
- /// <summary>
/// Deeply compares this object with the specified object and returns a list of properties that are different.
/// </summary>
/// <param name="left">The left side of the comparison.</param>
@@ -171,14 +90,70 @@ namespace slskd
}
/// <summary>
- /// Returns a "pretty" string representation of the provided Type; specifically, corrects the naming of generic Types
- /// and appends the type parameters for the type to the name as it appears in the code editor.
+ /// Recursively retrieves all properties.
/// </summary>
- /// <param name="type">The type for which the colloquial name should be created.</param>
- /// <returns>A "pretty" string representation of the provided Type.</returns>
- public static string ToColloquialString(this Type type)
+ /// <param name="type">The type from which to retrieve properties.</param>
+ /// <returns>The list of properties.</returns>
+ public static IEnumerable<PropertyInfo> GetPropertiesRecursively(this Type type)
{
- return !type.IsGenericType ? type.Name : type.Name.Split('`')[0] + "<" + string.Join(", ", type.GetGenericArguments().Select(a => a.ToColloquialString())) + ">";
+ var props = new List<PropertyInfo>();
+
+ foreach (var prop in type.GetProperties())
+ {
+ if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsArray || Nullable.GetUnderlyingType(prop.PropertyType) != null || new[] { typeof(string), typeof(decimal) }.Contains(prop.PropertyType))
+ {
+ props.Add(prop);
+ }
+ else
+ {
+ props.AddRange(prop.PropertyType.GetPropertiesRecursively());
+ }
+ }
+
+ return props;
+ }
+
+ /// <summary>
+ /// Determines whether the string is a valid regular expression.
+ /// </summary>
+ /// <param name="pattern">The string to validate.</param>
+ /// <returns>A value indicating whether the string is a valid regular expression.</returns>
+ public static bool IsValidRegex(this string pattern)
+ {
+ if (string.IsNullOrWhiteSpace(pattern))
+ {
+ return false;
+ }
+
+ try
+ {
+ Regex.Match(string.Empty, pattern);
+ }
+ catch (ArgumentException)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ /// <summary>
+ /// Replaces the first occurance of <paramref name="phrase"/> in the string with <paramref name="replacement"/>.
+ /// </summary>
+ /// <param name="str">The string on which to perform the replacement.</param>
+ /// <param name="phrase">The phrase or substring to replace.</param>
+ /// <param name="replacement">The replacement string.</param>
+ /// <returns>The string, with the desired phrase replaced.</returns>
+ public static string ReplaceFirst(this string str, string phrase, string replacement)
+ {
+ int pos = str.IndexOf(phrase);
+
+ if (pos < 0)
+ {
+ return str;
+ }
+
+ return str.Substring(0, pos) + replacement + str.Substring(pos + phrase.Length);
}
/// <summary>
@@ -221,6 +196,48 @@ namespace slskd
}
/// <summary>
+ /// Returns a "pretty" string representation of the provided Type; specifically, corrects the naming of generic Types
+ /// and appends the type parameters for the type to the name as it appears in the code editor.
+ /// </summary>
+ /// <param name="type">The type for which the colloquial name should be created.</param>
+ /// <returns>A "pretty" string representation of the provided Type.</returns>
+ public static string ToColloquialString(this Type type)
+ {
+ return !type.IsGenericType ? type.Name : type.Name.Split('`')[0] + "<" + string.Join(", ", type.GetGenericArguments().Select(a => a.ToColloquialString())) + ">";
+ }
+
+ /// <summary>
+ /// Serializes this object to json.
+ /// </summary>
+ /// <param name="obj">The object to serialize.</param>
+ /// <returns>A string containing the serialized object.</returns>
+ public static string ToJson(this object obj) => JsonSerializer.Serialize(obj, GetJsonSerializerOptions());
+
+ /// <summary>
+ /// Converts a fully qualified remote filename to a local filename based in the provided
+ /// <paramref name="baseDirectory"/>, swapping directory characters for those specific to the local OS, removing any
+ /// characters that are invalid for the local OS, and making the path relative to the remote store (including the
+ /// filename and the parent folder).
+ /// </summary>
+ /// <param name="remoteFilename">The fully qualified remote filename to convert.</param>
+ /// <param name="baseDirectory">The base directory for the local filename.</param>
+ /// <returns>The converted filename.</returns>
+ public static string ToLocalFilename(this string remoteFilename, string baseDirectory)
+ {
+ return Path.Combine(baseDirectory, remoteFilename.ToLocalRelativeFilename());
+ }
+
+ /// <summary>
+ /// Converts the given path to the local format (normalizes path separators).
+ /// </summary>
+ /// <param name="path">The path to convert.</param>
+ /// <returns>The converted path.</returns>
+ public static string ToLocalOSPath(this string path)
+ {
+ return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar);
+ }
+
+ /// <summary>
/// Converts a fully qualified remote filename to a local filename, swapping directory characters for those specific
/// to the local OS, removing any characters that are invalid for the local OS, and making the path relative to the
/// remote store (including the filename and the parent folder).
@@ -243,26 +260,20 @@ namespace slskd
}
/// <summary>
- /// Converts a fully qualified remote filename to a local filename based in the provided <paramref name="baseDirectory"/>, swapping directory characters for those specific
- /// to the local OS, removing any characters that are invalid for the local OS, and making the path relative to the
- /// remote store (including the filename and the parent folder).
+ /// Deserializes this string from json to an object of type <typeparamref name="T"/>.
/// </summary>
- /// <param name="remoteFilename">The fully qualified remote filename to convert.</param>
- /// <param name="baseDirectory">The base directory for the local filename.</param>
- /// <returns>The converted filename.</returns>
- public static string ToLocalFilename(this string remoteFilename, string baseDirectory)
- {
- return Path.Combine(baseDirectory, remoteFilename.ToLocalRelativeFilename());
- }
+ /// <typeparam name="T">The type to which to deserialize the string.</typeparam>
+ /// <param name="str">The string to deserialize.</param>
+ /// <returns>The new object deserialzied from the string.</returns>
+ public static T ToObject<T>(this string str) => JsonSerializer.Deserialize<T>(str, GetJsonSerializerOptions());
- /// <summary>
- /// Converts the given path to the local format (normalizes path separators).
- /// </summary>
- /// <param name="path">The path to convert.</param>
- /// <returns>The converted path.</returns>
- public static string ToLocalOSPath(this string path)
+ private static JsonSerializerOptions GetJsonSerializerOptions()
{
- return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar);
+ var options = new JsonSerializerOptions();
+ options.Converters.Add(new IPAddressConverter());
+ options.Converters.Add(new JsonStringEnumConverter());
+ options.IgnoreNullValues = true;
+ return options;
}
}
} \ No newline at end of file
diff --git a/src/web/package-lock.json b/src/web/package-lock.json
index b7233ee3..c16d6881 100644
--- a/src/web/package-lock.json
+++ b/src/web/package-lock.json
@@ -3298,17 +3298,17 @@
"dev": true
},
"axios": {
- "version": "0.21.1",
- "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
- "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
+ "version": "0.21.2",
+ "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.2.tgz",
+ "integrity": "sha512-87otirqUw3e8CzHTMO+/9kh/FSgXt/eVDvipijwDtEuwbkySWZ9SBm6VEubmJ/kLKEoLQV/POhxXFb66bfekfg==",
"requires": {
- "follow-redirects": "^1.10.0"
+ "follow-redirects": "^1.14.0"
},
"dependencies": {
"follow-redirects": {
- "version": "1.14.2",
- "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.2.tgz",
- "integrity": "sha512-yLR6WaE2lbF0x4K2qE2p9PEXKLDjUjnR/xmjS3wHAYxtlsI9MLLBJUZirAHKzUZDGLxje7w/cXR49WOUo4rbsA=="
+ "version": "1.14.5",
+ "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.5.tgz",
+ "integrity": "sha512-wtphSXy7d4/OR+MvIFbCVBDzZ5520qV8XfPklSN5QtxuMUJZ+b0Wnst1e1lCDocfzuCkHqj8k0FpZqO+UIaKNA=="
}
}
},
diff --git a/src/web/package.json b/src/web/package.json
index 067ebfd2..d20edaab 100644
--- a/src/web/package.json
+++ b/src/web/package.json
@@ -5,7 +5,7 @@
"homepage": ".",
"dependencies": {
"@microsoft/signalr": "^5.0.10",
- "axios": "^0.21.1",
+ "axios": "^0.21.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.0",