Skip to content

Commit 904f791

Browse files
committed
Added new extensions.
1 parent 419a227 commit 904f791

File tree

3 files changed

+112
-12
lines changed

3 files changed

+112
-12
lines changed

src/FluentExtensions/BooleanEx.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
3+
namespace FluentExtensions
4+
{
5+
public static class BooleanEx
6+
{
7+
/// <summary>
8+
/// Converts boolean value to either "yes" or "No".
9+
/// </summary>
10+
/// <param name="source">Boolean to check.</param>
11+
/// <returns>Either "Yes" or "No" based on given boolean value.</returns>
12+
public static string ToYesNo(this bool source)
13+
{
14+
return source ? "Yes" : "No";
15+
}
16+
17+
/// <summary>
18+
/// Executes an action while the given boolean value is equal to target value.
19+
/// </summary>
20+
/// <param name="source">boolean value to check against.</param>
21+
/// <param name="result">target value to check into.</param>
22+
/// <param name="execute">The Action to execute when the condition is met.</param>
23+
public static void DoOn(this bool source, bool result, Action execute)
24+
{
25+
if (source != result)
26+
return;
27+
28+
execute.Invoke();
29+
}
30+
}
31+
}

src/FluentExtensions/ListEx.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data;
34
using System.Linq;
45

56
namespace FluentExtensions
@@ -17,7 +18,8 @@ public static IEnumerable<T> ToImmutableList<T>(this IEnumerable<T> sourceList)
1718
{
1819
if (sourceList == null)
1920
throw new ArgumentNullException(nameof(sourceList));
20-
var list = sourceList.ToList();
21+
22+
var list = sourceList.AsList();
2123
return list.AsReadOnly();
2224
}
2325

@@ -33,13 +35,13 @@ public static IEnumerable<T> ReplaceWith<T>(this IList<T> sourceList, IEnumerabl
3335
{
3436
if (sourceList == null)
3537
throw new ArgumentNullException(nameof(sourceList));
36-
37-
if(sourceList.Any())
38+
39+
if (sourceList.Any())
3840
sourceList.Clear();
39-
41+
4042
foreach (var item in targetList)
4143
sourceList.Add(item);
42-
44+
4345
return sourceList;
4446
}
4547

@@ -54,13 +56,26 @@ public static T PickRandomItem<T>(this IList<T> sourceList)
5456
{
5557
if (sourceList == null)
5658
throw new ArgumentNullException(nameof(sourceList));
57-
59+
5860
if (!sourceList.Any())
5961
throw new ArgumentNullException($"{nameof(sourceList)} is empty");
60-
62+
6163
var randomize = new Random();
6264
return sourceList[randomize.Next(1, sourceList.Count)];
6365
}
64-
66+
67+
/// <summary>
68+
/// Converts IEnumerable to list, if it is already a List it returns the list itself.
69+
/// </summary>
70+
/// <param name="sourceList">IEnumerable to convert.</param>
71+
/// <returns>List from IEnumerable.</returns>
72+
public static IList<T> AsList<T>(IEnumerable<T> sourceList)
73+
{
74+
if (sourceList == null)
75+
throw new ArgumentNullException(nameof(sourceList));
76+
77+
return sourceList is List<T> ? sourceList : sourceList.ToList();
78+
}
79+
6580
}
6681
}

src/FluentExtensions/StringEx.cs

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using FluentExtensions.Enumerators;
2-
using System;
1+
using System;
2+
using System.Collections.Generic;
33
using System.Globalization;
44
using System.IO;
55
using System.Linq;
66
using System.Text;
77
using System.Threading.Tasks;
8+
using FluentExtensions.Enumerators;
89

910
namespace FluentExtensions
1011
{
@@ -370,9 +371,10 @@ public static string DriveFreeSpace(this string driveLetter)
370371
{
371372
if (drive.Name.ToLower().Replace(@":\", "") != driveLetter.ToLower())
372373
continue;
373-
var kb = (int)(drive.AvailableFreeSpace * 0.001);
374+
var kb = (int) (drive.AvailableFreeSpace * 0.001);
374375
freeSpace = kb.ToGB(DigitalStorage.KB);
375376
}
377+
376378
return Math.Round(freeSpace, 3) + " GB";
377379
}
378380

@@ -389,9 +391,10 @@ public static string DriveTotalSize(this string driveLetter)
389391
{
390392
if (drive.Name.ToLower().Replace(@":\", "") != driveLetter.ToLower())
391393
continue;
392-
var kb = (int)(drive.TotalSize * 0.001);
394+
var kb = (int) (drive.TotalSize * 0.001);
393395
freeSpace = kb.ToGB(DigitalStorage.KB);
394396
}
397+
395398
return Math.Round(freeSpace, 3) + " GB";
396399
}
397400

@@ -410,6 +413,7 @@ public static string DriveFormat(this string driveLetter)
410413
continue;
411414
driveFormat = drive.DriveFormat;
412415
}
416+
413417
return driveFormat;
414418
}
415419

@@ -428,6 +432,7 @@ public static DriveType DriveType(this string driveLetter)
428432
continue;
429433
type = drive.DriveType;
430434
}
435+
431436
return type;
432437
}
433438

@@ -539,5 +544,54 @@ public static double FileSizeInMB(this string filePath)
539544
return Math.Round(fileSizeInMB, 3, MidpointRounding.AwayFromZero);
540545
}
541546

547+
/// <summary>
548+
/// Joins string array into a single string separated via delimiter.
549+
/// </summary>
550+
/// <param name="texts">string array to join.</param>
551+
/// <param name="delimiter">delimiter to join strings with (eg: , ).</param>
552+
/// <returns>A Single string based on given array.</returns>
553+
public static string Join(this IEnumerable<string> texts, string delimiter)
554+
{
555+
var str = new StringBuilder();
556+
foreach (var text in texts)
557+
{
558+
str.Append($"{text}{delimiter}");
559+
}
560+
561+
return str.ToString();
562+
}
563+
564+
/// <summary>
565+
/// Reads the file content as array of bytes.
566+
/// </summary>
567+
/// <param name="filePath">Path of the file to read.</param>
568+
/// <returns>Byte array of the given file content.</returns>
569+
public static byte[] ReadBytesFromDisk(this string filePath)
570+
{
571+
if (filePath.IsEmpty())
572+
throw new Exception("The given path is empty");
573+
574+
return File.ReadAllBytes(filePath);
575+
}
576+
577+
/// <summary>
578+
/// Reads the file content as array of bytes asynchronously.
579+
/// </summary>
580+
/// <param name="filePath">Path of the file to read.</param>
581+
/// <returns>Byte array of the given file content.</returns>
582+
public static async Task<byte[]> ReadBytesFromDiskAsync(this string filePath)
583+
{
584+
if (filePath.IsEmpty())
585+
throw new Exception("The given path is empty");
586+
587+
byte[] outputFile;
588+
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
589+
{
590+
outputFile = new byte[fs.Length];
591+
await fs.ReadAsync(outputFile, 0, (int) fs.Length);
592+
}
593+
594+
return outputFile;
595+
}
542596
}
543597
}

0 commit comments

Comments
 (0)