using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace IOL.Helpers;
public static class StringHelpers
{
///
/// Check wheter or not the given string has a value
///
///
/// False if the input string is null or only whitespace, otherwise it returns true
public static bool IsNullOrWhiteSpace(this string input) {
return string.IsNullOrWhiteSpace(input);
}
///
/// Convert a string to snake_casing
///
/// The input string to convert
/// A snake cased string representation of the input string
public static string AsSnakeCasedString(this string input) {
if (input.IsNullOrWhiteSpace()) return default;
input = input.ToLower().Trim();
return input.Replace(" ", "_");
}
///
/// Creatas a url safe string (slug) of the input
///
/// The string to convert into a slug
/// A string (slug) representation of the input
public static string AsSlug(this string input) {
return Slug.Generate(true, input);
}
///
/// Check wheter or not the given string has a value
///
///
/// False if the input string is null or only whitespace, otherwise it returns true
public static bool HasValue(this string input) {
return !input.IsNullOrWhiteSpace();
}
///
/// Performs a unicorn format on the input.
///
///
/// const string input = "Hello, {name}";
/// var result = input.UnicornFormat(new Dictionary {
/// {
/// "name", "World"
/// }
/// });
/// // result -> "Hello, World"
///
///
///
///
public static string UnicornFormat(this string input, IDictionary values) {
if (string.IsNullOrWhiteSpace(input)) return default;
if (values.Count == 0) return input;
return values.Aggregate(input, (current, value1) => current.Replace("{" + value1.Key + "}", value1.Value));
}
///
/// Converts a string input to it's Guid representation, if the string does not conform to the guid specification a FormatException will be thrown
///
/// The string to interpret as a guid
/// The input string as a guid
public static Guid AsGuid(this string input) {
return new Guid(input);
}
///
/// Converts a string input to it's Guid representation, if the string does not conform to the guid specification, a default input will be returned
///
/// The string to interpret as a guid
/// The string as a guid or the default input for guids
public static Guid AsGuidOrDefault(this string input) {
return !Guid.TryParse(input, out var res) ? default : res;
}
///
/// Encodes a string input to it's base 64 equalient
///
/// The input to encode
/// The base 64 encoded string
public static string AsBase64EncodedString(this string input) {
return input.IsNullOrWhiteSpace() ? default : Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
}
///
/// Decodes a string input to it's original input
///
/// The input to decode
/// The decoded string input of the base 64 string
public static string AsBase64DecodedString(this string input) {
return input.IsNullOrWhiteSpace() ? default : Encoding.UTF8.GetString(Convert.FromBase64String(input));
}
///
/// Capitalize the input
///
/// The string input to capitalize
///
public static string Capitalize(this string input, bool onlyFirstChar = false) {
if (input.IsNullOrWhiteSpace()) return default;
input = input.Trim();
if (!onlyFirstChar) {
return Regex.Replace(input, @"\b(\w)", m => m.Value.ToUpper(), RegexOptions.None);
}
input = char.ToUpper(input[0]) + input.Substring(1, input.Length - 1);
return input;
}
///
/// Obfucates the input string
///
///
///
public static string Obfuscate(this string input) {
if (input.IsNullOrWhiteSpace()) return default;
var last4Chars = "****";
if (input.HasValue() && input.Length > 4) {
last4Chars = input[^4..];
}
return "****" + last4Chars;
}
}