Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #70 from techno-dwarf-works/dev
Browse files Browse the repository at this point in the history
Add ListExtensions.cs
  • Loading branch information
uurha committed Mar 8, 2024
1 parent 3022470 commit cd17c1d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions Editor/BetterExtensions.Editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "BetterExtensions.Editor",
"rootNamespace": "Better.Extensions.EditorAddons",
"references": [
"GUID:6adbd2a12d24f5d429f7ea1b1086e835",
"GUID:01df13aca8d01e24a911bcc3e8277031",
"GUID:28da8d3b12e3efa47928e0c9070f853d"
],
Expand Down
75 changes: 75 additions & 0 deletions Runtime/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;

namespace Better.Extensions.Runtime
{
public static class ListExtensions
{
public static bool RemoveRange<T>(this List<T> self, IEnumerable<T> range)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return false;
}

if (range == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(range));
return false;
}

var anyRemoved = false;
foreach (var item in range)
{
if (self.Remove(item))
{
anyRemoved = true;
}
}

return anyRemoved;
}

public static void AddRangeDistinct<T>(this List<T> self, IEnumerable<T> range)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return;
}

if (range == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(range));
return;
}

foreach (var item in range)
{
if (self.Contains(item)) continue;
self.Add(item);
}
}

public static bool TrimToCount<T>(this List<T> self, int count)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return false;
}

count = Math.Max(count, 0);

if (self.Count > count)
{
var difference = self.Count - count;
self.RemoveRange(count, difference);
return true;
}

return false;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Extensions/ListExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.uurha.betterextensions",
"displayName": "Better Extensions",
"version": "1.5.95",
"version": "1.5.96",
"unity": "2021.3",
"description": "Unity extensions, serialize extension, async extension, string extension and UI extensions",
"dependencies": {
Expand Down

0 comments on commit cd17c1d

Please sign in to comment.