Common Extensions
ArrayExtension, StringExtension, DateTimeExtension, TimeSpanUtility, TypeExtension, UriExtension common extensions.
Zongsoft.Common provides a common set of extension methods for day-to-day processing of arrays, strings, times, types, and URIs.
Type Relationship
ArrayExtension
Array null value detection and creation of empty arrays by runtime type.
StringExtension
Character removal, string pruning, number detection, desensitization, fragmentation analysis.
DateTimeExtension
Time reset, time-consuming calculation.
TimeSpanUtility
Time intervals are parsed and limited to the specified range.
TypeExtension
Judgment of type compatibility, generic definition, collection type, scalar type, default value, etc.
UriExtension
Reads the specified key from a URL query string.
String Expansion
Source: framework/Zongsoft.Core/test/Common/StringExtensionTest.cs (excerpt; see source for context).
public void TestSlice()
{
var parts = StringExtension.Slice("a - b -- c ", '-').ToArray();
Assert.NotEmpty(parts);
Assert.Equal(3, parts.Length);
Assert.Equal("a", parts[0]);
Assert.Equal("b", parts[1]);
Assert.Equal("c", parts[2]);
var hasColon = false;
parts = StringExtension.Slice("issue-100-park:10001-1", chr => hasColon ? false : !(hasColon = chr == ':') && chr == '-').ToArray();
Assert.NotEmpty(parts);
Assert.Equal(3, parts.Length);
Assert.Equal("issue", parts[0]);
Assert.Equal("100", parts[1]);
Assert.Equal("park:10001-1", parts[2]);
}The above directly quotes Core's sharding test, covering whitespace, continuous separators, and stateful separation judgments. The actual input and assertions of RemoveAny, Trim, and IsDigits are also in the same file. Slice ignores whitespace fragments and is suitable for parsing simple delimited text.
Time Expansion
TimeSpanUtility supports ms, s, m, h, d and other abbreviated formats, and also supports standard time formats.
Source: framework/Zongsoft.Core/test/Common/TimeSpanTest.cs (excerpt; see source for context).
DateTimeExtension.GetElapsed can calculate the elapsed time from a certain point in time to the present. Discussions' file upload controller generates a file name based on the number of days since the millennium era plus a random string; the following is a snippet of the UploadAsync callback parameter, and the complete method can be found in the source code link. Use a wall clock for this date difference, and a specialized timing tool should be used to measure code execution time.
Source: src/api/Controllers/FileController.cs (excerpt; see source for context).
Type Extension
TypeExtension provides more flexible generic judgment capabilities than Type.IsAssignableFrom.
Source: framework/Zongsoft.Core/test/Common/TypeExtensionTest.cs (excerpt; see source for context).
The above is from TypeExtensionTest, and all collection types used are test inputs; the full test can be reused to check generic matching behavior. It can also determine sets, lists, dictionaries, hash sets, numeric types, nullable types, and scalar types.
URI Extension
No call case for this extension has been found in Discussions and the framework project. Implementation of UriExtension splits Query by & and =, and ignores the case of key names; it does not have a complete URI decoding process, nor does it actively remove the question mark from the Query header. Web requests should preferentially use the framework controller's request query set and avoid treating this tool as a full URL parameter resolver.
Related Resources
Last updated