Skip to content

Commit

Permalink
Added ParseUtility, DateTimeUtility.IsDateTime, StringUtility.TakeS.
Browse files Browse the repository at this point in the history
  • Loading branch information
GregaMohorko committed Jun 28, 2020
1 parent 08a1217 commit e1a90a4
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 16 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Gregor Mohorko
Copyright (c) 2020 Gregor Mohorko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 11 additions & 2 deletions src/GM.Utility/GM.Utility/DateTimeUtility.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
MIT License
Copyright (c) 2017 Grega Mohorko
Copyright (c) 2020 Gregor Mohorko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,7 +23,7 @@ MIT License
Project: GM.Utility
Created: 2017-10-27
Author: Grega Mohorko
Author: Gregor Mohorko
*/

using System;
Expand Down Expand Up @@ -117,6 +117,15 @@ public static bool IsCurrentMonth(this DateTime date)
return DateTime.Today.Month == date.Month;
}

/// <summary>
/// Determines whether the provided text represents a valid <see cref="DateTime"/>.
/// </summary>
/// <param name="text">A string, possibly representing a valid date and time.</param>
public static bool IsDateTime(string text)
{
return DateTime.TryParse(text, out _);
}

/// <summary>
/// Determines whether this date is in the future. Ignores time.
/// </summary>
Expand Down
7 changes: 4 additions & 3 deletions src/GM.Utility/GM.Utility/DecimalUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ public static decimal GetDecimals(this decimal value, int decimalCount)
/// </summary>
/// <param name="value">The decimal value.</param>
/// <param name="decimalCount">Number of decimals to get.</param>
[Obsolete("This method is obsolete and will be removed in the next release, please use GetDecimalPart(decimal value, int decimalCount, bool round).", false)]
[Obsolete("This method is obsolete and will be removed in the next releases, please use GetDecimalPart(this decimal, int, bool).", false)]
public static int GetDecimalPart(this decimal value, int decimalCount)
{
// FIXME obsolete 2019-06-15
// FIXME obsolete v1.3.0.0
// 2020-06-28
return GetDecimalPart(value, decimalCount, false);
}

Expand Down Expand Up @@ -137,7 +138,7 @@ public static int GetWholePart(this decimal value)
/// <param name="text">The text.</param>
public static bool IsDecimal(string text)
{
return decimal.TryParse(text, out decimal value);
return decimal.TryParse(text, out _);
}

/// <summary>
Expand Down
9 changes: 5 additions & 4 deletions src/GM.Utility/GM.Utility/DoubleUtility.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
MIT License
Copyright (c) 2017 Grega Mohorko
Copyright (c) 2020 Gregor Mohorko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,7 +23,7 @@ MIT License
Project: GM.Utility
Created: 2017-10-27
Author: Grega Mohorko
Author: Gregor Mohorko
*/

using System;
Expand Down Expand Up @@ -79,10 +79,11 @@ public static double GetDecimals(this double value, int decimalCount)
/// </summary>
/// <param name="value">The double value.</param>
/// <param name="decimalCount">Number of decimals to get.</param>
[Obsolete("This method is obsolete and will be removed in next releases, please use GetDecimalPart(double value, int decimalCount, bool round).", false)]
[Obsolete("This method is obsolete and will be removed in next releases, please use GetDecimalPart(this double, int, bool).", false)]
public static int GetDecimalPart(this double value, int decimalCount)
{
// FIXMe obsolete 2019-06-15
// FIXMe obsolete v1.3.0.0
// 2020-06-28
return GetDecimalPart(value, decimalCount, false);
}

Expand Down
10 changes: 5 additions & 5 deletions src/GM.Utility/GM.Utility/GM.Utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>GM.StrongNameKey.snk</AssemblyOriginatorKeyFile>
<Version>1.2.9.0</Version>
<Version>1.3.0.0</Version>
<Title>GM.Utility</Title>
<Authors>Gregor Mohorko</Authors>
<Company>Gregor Mohorko</Company>
Expand All @@ -14,11 +14,11 @@
<PackageIcon>icon.png</PackageIcon>
<PackageTags>utility extensions software design pattern</PackageTags>
<Description>Library with various static classes and tools that provide universally useful functions, extensions and utilities.</Description>
<Copyright>Copyright © Gregor Mohorko 2019</Copyright>
<Copyright>Copyright © Gregor Mohorko 2020</Copyright>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageReleaseNotes>Added methods: DictionaryUtility.GetOrCreate; IEnumerableUtility.ForEach; ReflectionUtility.GetProperyValueFromPath; StatisticUtility.CalculateCV, CalculateRSD.</PackageReleaseNotes>
<AssemblyVersion>1.2.9.0</AssemblyVersion>
<FileVersion>1.2.9.0</FileVersion>
<PackageReleaseNotes>Enabled SourceLink. Added utility classes: ExcelUtility, ExceptionUtility, ListUtility, ParseUtility, TaskUtility. Added utility methods: ColorUtility.ScaleLinear; CsvUtility.SurroundWithDoubleQuotes; DateTimeUtility.IsDateTime; DecimalUtility.GetDecimals(decimalCount), ToStringFixedLength, ToStringShortest; DictionaryUtility.AddOrCreate; IEnumerableUtility.AllSame, MaxOrZero, ToObservableCollection; IOUtility.SetAs(Not)ReadOnly; PathUtility.IsNetworkDrive; StatisticUtility.DifferencePercentage, RSD; ReflectionUtility.GetValue, SetValue; StringUtility.ContainsWhitespace, OccurencesOf, TakeS, ToPascalCase, ToSentenceCase, ToSingleLine, ToTitleCase, ToUpperCaseFirstLetterOnly; XMLUtility.(De)Serialize(To/From File).</PackageReleaseNotes>
<AssemblyVersion>1.3.0.0</AssemblyVersion>
<FileVersion>1.3.0.0</FileVersion>
<RepositoryUrl>https://github.com/GregaMohorko/GM.Utility</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
Expand Down
3 changes: 3 additions & 0 deletions src/GM.Utility/GM.Utility/IEnumerableUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static class IEnumerableUtility
{
/// <summary>
/// Determines whether all elements in this collection are equal to each other. Compares using the <see cref="object.Equals(object)"/> method.
/// <para>This method returns true if the collection is empty or it contains only one element.</para>
/// </summary>
/// <typeparam name="T">The type of the elements.</typeparam>
/// <param name="enumerable">The collection.</param>
Expand All @@ -62,6 +63,7 @@ public static bool AllSame<T>(this IEnumerable<T> enumerable)

/// <summary>
/// Determines whether all elements in this collection produce the same value with the provided value selector. Compares using the <see cref="object.Equals(object)"/> method.
/// <para>This method returns true if the collection is empty or it contains only one element.</para>
/// </summary>
/// <typeparam name="T">The type of the elements.</typeparam>
/// <typeparam name="TValue">The type of the values to compare.</typeparam>
Expand All @@ -84,6 +86,7 @@ public static bool AllSame<T>(this IEnumerable<T> enumerable)

/// <summary>
/// Determines whether all elements in this collection produce the same values with the provided value selectors. Compares using the <see cref="object.Equals(object)"/> method.
/// <para>This method returns true if the collection is empty or it contains only one element.</para>
/// </summary>
/// <typeparam name="T">The type of the elements.</typeparam>
/// <typeparam name="TValue1">The type of the first values to compare.</typeparam>
Expand Down
73 changes: 73 additions & 0 deletions src/GM.Utility/GM.Utility/ParseUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
MIT License
Copyright (c) 2020 Gregor Mohorko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Project: GM.Utility
Created: 2020-6-28
Author: Gregor Mohorko
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace GM.Utility
{
/// <summary>
/// Utilities for parsing string.
/// </summary>
public static class ParseUtility
{
/// <summary>
/// Converts the string representation of <typeparamref name="T"/> to its <typeparamref name="T"/> equivalent.
/// <para>Uses <see cref="Convert.ChangeType(object, Type)"/>.</para>
/// </summary>
/// <typeparam name="T">The type of object to return.</typeparam>
/// <param name="value">A string containing the value to convert.</param>
/// <exception cref="InvalidCastException" />
/// <exception cref="FormatException" />
public static T ParseTo<T>(this string value)
{
return (T)Convert.ChangeType(value, typeof(T));
}

/// <summary>
/// Tries to convert the specified string representation of <typeparamref name="T"/> to its <typeparamref name="T"/> equivalent. A return value indicates whether the conversion succeeded or failed.
/// </summary>
/// <typeparam name="T">The type of object to return.</typeparam>
/// <param name="value">A string containing the value to convert.</param>
/// <param name="result">The result of the parse operation.</param>
public static bool TryParseTo<T>(this string value, out T result)
{
try {
result = ParseTo<T>(value);
} catch(Exception e) {
if(e is InvalidCastException || e is FormatException) {
result = default;
return false;
}
throw;
}
return true;
}
}
}
3 changes: 2 additions & 1 deletion src/GM.Utility/GM.Utility/ReflectionUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ public static object GetFieldValue(this object obj, string fieldName)
[Obsolete("This method is obsolete and will be removed in next releases, please use GetValueFromPath.", false)]
public static object GetPropertyValueFromPath(object obj, string propertyPath)
{
// FIXME obsolete 2019-10-08
// FIXME obsolete v1.3.0.0
// 2019-10-08
return GetValueFromPath(obj, propertyPath);
}

Expand Down
40 changes: 40 additions & 0 deletions src/GM.Utility/GM.Utility/StringUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ public static string RemoveFirstOf(this string text, string value)
/// <param name="text">The text.</param>
public static string RemoveWhitespace(this string text)
{
// TESTME
// what is faster?
//return new string(text.Where(c => !char.IsWhiteSpace(c)).ToArray());
return RegexWhitespace.Replace(text, "");
}

Expand Down Expand Up @@ -222,6 +225,20 @@ public static string ShortenWith3Dots(this string text, int maxLength)
return text.Substring(0, maxLength) + "...";
}

/// <summary>
/// Retrieves a substring from this instance. Starts at the beginnning and takes the specified number of characters.
/// <para>If <paramref name="count"/> is bigger than the length of this string, a copy of this instance is returned.</para>
/// </summary>
/// <param name="text">The text.</param>
/// <param name="count">The number of characters to take.</param>
public static string TakeS(this string text, int count)
{
if(text == null) {
throw new ArgumentNullException(nameof(text));
}
return new string(text.Take(count).ToArray());
}

private static readonly Lazy<Regex> REGEX_SENTENCEORTITLE = new Lazy<Regex>(() => new Regex("(^| )[A-z]", RegexOptions.Compiled));

/// <summary>
Expand Down Expand Up @@ -297,6 +314,29 @@ public static string ToTitleCase(this string text)
}
TextInfo textInfo = CultureInfo.InvariantCulture.TextInfo;
return textInfo.ToTitleCase(text);

// old implementation
/*
var sb = new StringBuilder();
var word = new StringBuilder();
foreach(char c in text) {
if(!char.IsWhiteSpace(c)) {
word.Append(c);
continue;
}
if(word.Length > 0) {
string titleCaseWord = word.ToString().ToLowerInvariant().ToUpperFirstLetter();
sb.Append(titleCaseWord);
}
sb.Append(c);
word.Clear();
}
if(word.Length > 0) {
string titleCaseWord = word.ToString().ToLowerInvariant().ToUpperFirstLetter();
sb.Append(titleCaseWord);
}
return sb.ToString();
//*/
}

/// <summary>
Expand Down

0 comments on commit e1a90a4

Please sign in to comment.