aboutsummaryrefslogtreecommitdiffstats
path: root/src/IOL.Helpers/StringHelpers.cs
blob: 783d23808fe672835bfc1a82956fb2f85589ae39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Configuration;

namespace IOL.Helpers;

public static class StringHelpers
{
	public static bool IsNullOrWhiteSpace(this string value) {
		return string.IsNullOrWhiteSpace(value);
	}

	public static string Slugified(this string input) {
		return Slug.Generate(true, input);
	}

	public static bool HasValue(this string value) {
		return !value.IsNullOrWhiteSpace();
	}

	public static string UnicornFormat(this string input, IDictionary<string, string> values) {
		if (string.IsNullOrWhiteSpace(input)) return default;
		return values.Count == 0 ? default : values.Aggregate(input, (current, value1) => current.Replace("{" + value1.Key + "}", value1.Value));
	}


	public static string UnicornFormatWithEnvironment(this string input, IConfiguration configuration) {
		if (string.IsNullOrWhiteSpace(input)) return default;
		var matchList = Regex.Matches(input, "{[a-z|_]*}", RegexOptions.IgnoreCase);
		foreach (var key in matchList.Select(match => match.Value)) {
			var value = configuration.GetValue<string>(key.Substring(1, key.Length - 2));
			if (string.IsNullOrWhiteSpace(value)) continue;
			input = input.Replace(key, value);
		}

		return input;
	}

	public static Guid ToGuid(this string value) {
		return !Guid.TryParse(value, out var res) ? default : res;
	}

	public static string Base64Encode(this string text) {
		return Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
	}

	public static string ExtractFileName(this string value) {
		if (value.IsNullOrWhiteSpace()) return default;
		var lastIndex = value.LastIndexOf('.');
		return lastIndex <= 0 ? default : value[..lastIndex];
	}

	public static string ExtractExtension(this string value) {
		if (value.IsNullOrWhiteSpace()) return default;
		var lastIndex = value.LastIndexOf('.');
		return lastIndex <= 0 ? default : value.Substring(lastIndex);
	}

	public static string Capitalize(this string input) {
		return input.IsNullOrWhiteSpace()
				? input
				: Regex.Replace(input, @"\b(\w)", m => m.Value.ToUpper(), RegexOptions.None);
	}


	/// <summary>
	/// Check if the given MIME is a JSON MIME.
	/// </summary>
	/// <param name="mime">MIME</param>
	/// <returns>Returns true if MIME type is json.</returns>
	public static bool IsJsonMime(this string mime) {
		var jsonRegex = new Regex("(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$");
		return mime != null && (jsonRegex.IsMatch(mime) || mime.Equals("application/json-patch+json"));
	}

	public static string Obfuscate(this string value) {
		var last4Chars = "****";
		if (value.HasValue() && value.Length > 4) {
			last4Chars = value.Substring(value.Length - 4);
		}

		return "****" + last4Chars;
	}
}