-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.cs
54 lines (48 loc) · 1.88 KB
/
Helpers.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Random = UnityEngine.Random;
namespace VeryUsualDay
{
public static class Helpers
{
public static TimeSpan ConvertToTimeSpan(string timeSpan)
{
var l = timeSpan.Length - 1;
var value = timeSpan.Substring(0, l);
var type = timeSpan.Substring(l, 1);
switch (type)
{
case "d": return TimeSpan.FromDays(double.Parse(value));
case "h": return TimeSpan.FromHours(double.Parse(value));
case "m": return TimeSpan.FromMinutes(double.Parse(value));
case "s": return TimeSpan.FromSeconds(double.Parse(value));
default: return TimeSpan.FromSeconds(double.Parse(value));
}
}
private static string GetCustomDescription(object objEnum)
{
var fi = objEnum.GetType().GetField(objEnum.ToString());
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : objEnum.ToString();
}
public static string Description(this Enum value)
{
return GetCustomDescription(value);
}
public static bool In<T>(this T val, params T[] vals) => vals.Contains(val);
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> enumerable)
{
var enumerable1 = enumerable.ToList();
var newEnum = Enumerable.Empty<T>();
while (enumerable1.Count != 0)
{
var index = Random.Range(0, enumerable1.Count);
newEnum = newEnum.Append(enumerable1[index]);
enumerable1.RemoveAt(index);
}
return newEnum;
}
}
}