Skip to content

Commit e825427

Browse files
committed
feat: Support more on ienumerable
1 parent abd2645 commit e825427

File tree

3 files changed

+66
-25
lines changed

3 files changed

+66
-25
lines changed

Assets/JCSUnity/Scripts/Util/JCS_Enum.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,19 @@ public static class JCS_Enum
2929
/// </summary>
3030
public static IEnumerable<T> GetValues<T>()
3131
{
32-
return Enum.GetValues(typeof(T)).Cast<T>();
32+
return GetValues<T, T>();
33+
}
34+
public static IEnumerable<U> GetValues<T, U>()
35+
{
36+
return Enum.GetValues(typeof(T)).Cast<U>();
37+
}
38+
39+
/// <summary>
40+
/// Return enum in true form.
41+
/// </summary>
42+
public static IEnumerable<Enum> GetValuesE<T>()
43+
{
44+
return GetValues<T, Enum>();
3345
}
3446

3547
/// <summary>

Assets/JCSUnity/Scripts/Util/JCS_Random.cs

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -229,46 +229,74 @@ public static T ChooseOneE<T>(params T[] args) // Ellipsis
229229
{
230230
return ChooseOne(args);
231231
}
232-
public static T ChooseOne<T>(ICollection<T> lst)
232+
public static T ChooseOne<T>(IEnumerable<T> args)
233233
{
234-
if (lst == null || lst.Count == 0)
234+
return ChooseOne(args.ToList());
235+
}
236+
public static T ChooseOne<T>(ICollection<T> args)
237+
{
238+
if (args == null || args.Count == 0)
235239
return default;
236240

237-
int index = Range(0, lst.Count);
241+
int index = Range(0, args.Count);
238242

239-
return lst.ElementAt(index);
243+
return args.ElementAt(index);
240244
}
241-
public static List<T> Choose<T>(int size, ICollection<T> lst)
245+
public static KeyValuePair<T, K> ChooseOneE<T, K>(params KeyValuePair<T, K>[] args) // Ellipsis
246+
{
247+
return ChooseOne(args);
248+
}
249+
public static KeyValuePair<T, K> ChooseOne<T, K>(IEnumerable<KeyValuePair<T, K>> args) // Ellipsis
250+
{
251+
return ChooseOne(args.ToList());
252+
}
253+
public static KeyValuePair<T, K> ChooseOne<T, K>(ICollection<KeyValuePair<T, K>> args)
254+
{
255+
if (args == null || args.Count == 0)
256+
return default;
257+
258+
int index = Range(0, args.Count);
259+
260+
return args.ElementAt(index);
261+
}
262+
263+
/// <summary>
264+
/// Choose multiple.
265+
/// </summary>
266+
public static List<T> ChooseE<T>(int size, params T[] args) // Ellipsis
267+
{
268+
return Choose(size, args);
269+
}
270+
public static List<T> Choose<T>(int size, IEnumerable<T> args)
271+
{
272+
return Choose(size, args.ToList());
273+
}
274+
public static List<T> Choose<T>(int size, ICollection<T> args)
242275
{
243276
List<T> chosen = new();
244277

245278
JCS_Loop.Times(size, () =>
246279
{
247-
chosen.Add(ChooseOne(lst));
280+
chosen.Add(ChooseOne(args));
248281
});
249282

250283
return chosen;
251284
}
252-
public static KeyValuePair<T, K> ChooseOneE<T, K>(params KeyValuePair<T, K>[] args) // Ellipsis
285+
public static List<KeyValuePair<T, K>> ChooseE<T, K>(int size, params KeyValuePair<T, K>[] args)
253286
{
254-
return ChooseOne(args);
287+
return Choose(size, args);
255288
}
256-
public static KeyValuePair<T, K> ChooseOne<T, K>(ICollection<KeyValuePair<T, K>> dict)
289+
public static List<KeyValuePair<T, K>> Choose<T, K>(int size, IEnumerable<KeyValuePair<T, K>> args)
257290
{
258-
if (dict == null || dict.Count == 0)
259-
return default;
260-
261-
int index = Range(0, dict.Count);
262-
263-
return dict.ElementAt(index);
291+
return Choose(size, args.ToList());
264292
}
265-
public static List<KeyValuePair<T, K>> Choose<T, K>(int size, ICollection<KeyValuePair<T, K>> dict)
293+
public static List<KeyValuePair<T, K>> Choose<T, K>(int size, ICollection<KeyValuePair<T, K>> args)
266294
{
267295
List<KeyValuePair<T, K>> chosen = new();
268296

269297
JCS_Loop.Times(size, () =>
270298
{
271-
chosen.Add(ChooseOne(dict));
299+
chosen.Add(ChooseOne(args));
272300
});
273301

274302
return chosen;

docs/ScriptReference/Util/JCS_Enum.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ Enum utilities.
44

55
## Functions
66

7-
| Name | Description |
8-
|:----------|:-----------------------------------------------------------------------------|
9-
| GetValues | Get the value for each enum, use to loop through the enum. |
10-
| ToArray | Convert enum values to array. |
11-
| ToList | Convert enum values to list. |
12-
| GetNames | Retrieves an array of the names of the constants in a specified enumeration. |
13-
| Size | Returns the length of an enumerator. |
7+
| Name | Description |
8+
|:-----------|:-----------------------------------------------------------------------------|
9+
| GetValues | Get the value for each enum, use to loop through the enum. |
10+
| GetValuesE | Get the value for each enum, in the type of `Enum`. |
11+
| ToArray | Convert enum values to array. |
12+
| ToList | Convert enum values to list. |
13+
| GetNames | Retrieves an array of the names of the constants in a specified enumeration. |
14+
| Size | Returns the length of an enumerator. |

0 commit comments

Comments
 (0)