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

Add ListExtensions.cs #70

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Assets/BetterExtensions/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;
}
}
}

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

2 changes: 1 addition & 1 deletion Assets/BetterExtensions/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
Loading