Skip to content

Commit 2768ff5

Browse files
committed
Added new extensions.
1 parent 90614a7 commit 2768ff5

File tree

5 files changed

+173
-4
lines changed

5 files changed

+173
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace FluentExtensions.Enumerators
2+
{
3+
public enum DigitalStorage
4+
{
5+
KB,
6+
MB,
7+
GB,
8+
TB
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace FluentExtensions.Enumerators
2+
{
3+
public enum Position
4+
{
5+
Start,
6+
End
7+
}
8+
}

src/FluentExtensions/FluentExtensions.csproj

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@
88
<RepositoryUrl>https://github.com/D-Diyare/FluentExtensions</RepositoryUrl>
99
<RepositoryType>git</RepositoryType>
1010
<PackageReadmeFile>README.md</PackageReadmeFile>
11-
<PackageTags>Fluent, Extensions, .NET, DotNet </PackageTags>
12-
<PackageReleaseNotes>Initial Release</PackageReleaseNotes>
11+
<PackageTags>extension, datetime, date, dotnet, extension-methods, fluent, string-matching, stringextensions, fluentextension, fluentextensions, numericxtensions</PackageTags>
12+
<PackageReleaseNotes>
13+
New Extensions Added:
14+
- string AddToEnd(this string text, string textToAdd, bool addSpaceBeforeAddition = true);
15+
- string AddToStart(this string text, string textToAdd, bool addSpaceafterAddition = true);
16+
- string ReplaceMultipleWithOne(this string source, string[] values, string value)
17+
- string Take(this string source, int characters, Position from = Position.Start);
18+
- double ToKB(this int number, DigitalStorage from = DigitalStorage.KB);
19+
- double ToMB(this int number, DigitalStorage from = DigitalStorage.MB);
20+
- double ToGB(this int number, DigitalStorage from = DigitalStorage.GB);
21+
- double ToTB(this int number, DigitalStorage from = DigitalStorage.TB);
22+
</PackageReleaseNotes>
1323
<PackageProjectUrl>https://github.com/D-Diyare/FluentExtensions</PackageProjectUrl>
14-
<Version>1.1</Version>
24+
<Version>1.2</Version>
1525
<Copyright>Copyright © Diyare Abdulla 2021</Copyright>
1626
<Title>FluentExtensions</Title>
1727
<PackageLicenseExpression>MIT</PackageLicenseExpression>

src/FluentExtensions/NumberEx.cs

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using FluentExtensions.Enumerators;
2+
using System;
23
using System.Globalization;
34
using System.Linq;
45
using System.Text;
@@ -7,6 +8,7 @@ namespace FluentExtensions
78
{
89
public static class NumberEx
910
{
11+
1012
/// <summary>
1113
/// Determines whether the given target is even.
1214
/// </summary>
@@ -261,5 +263,91 @@ public static string GenerateRandomString(this int length, bool isUpperCase = fa
261263
return str.ToString();
262264
}
263265

266+
/// <summary>
267+
/// Converts given number to kilobytes.
268+
/// </summary>
269+
/// <param name="number">Number to convert.</param>
270+
/// <returns>Kilobytes based on given number.</returns>
271+
public static double ToKB(this int number, DigitalStorage from = DigitalStorage.KB)
272+
{
273+
switch (from)
274+
{
275+
case DigitalStorage.MB:
276+
return number * 1000;
277+
case DigitalStorage.GB:
278+
return number * 1000000;
279+
case DigitalStorage.TB:
280+
return number * 1000000000;
281+
case DigitalStorage.KB:
282+
default:
283+
return number;
284+
}
285+
}
286+
287+
/// <summary>
288+
/// Converts given number to megabytes.
289+
/// </summary>
290+
/// <param name="number">Number to convert.</param>
291+
/// <param name="from">From type.</param>
292+
/// <returns>Megabytes based on given number and given type to convert from.</returns>
293+
public static double ToMB(this int number, DigitalStorage from = DigitalStorage.MB)
294+
{
295+
switch (from)
296+
{
297+
case DigitalStorage.KB:
298+
return number * 0.001;
299+
case DigitalStorage.GB:
300+
return number * 1000;
301+
case DigitalStorage.TB:
302+
return number * 1000000;
303+
case DigitalStorage.MB:
304+
default:
305+
return number;
306+
}
307+
}
308+
309+
/// <summary>
310+
/// Converts given number to gigabytes.
311+
/// </summary>
312+
/// <param name="number">Number to convert.</param>
313+
/// <param name="from">From type.</param>
314+
/// <returns>Gigabytes based on given number and given type to convert from.</returns>
315+
public static double ToGB(this int number, DigitalStorage from = DigitalStorage.GB)
316+
{
317+
switch (from)
318+
{
319+
case DigitalStorage.KB:
320+
return number.ToMB(DigitalStorage.KB) * 0.001;
321+
case DigitalStorage.MB:
322+
return number * 0.001;
323+
case DigitalStorage.TB:
324+
return number * 1000;
325+
case DigitalStorage.GB:
326+
default:
327+
return number;
328+
}
329+
}
330+
331+
/// <summary>
332+
/// Converts given number to terabytes.
333+
/// </summary>
334+
/// <param name="number">Number to convert.</param>
335+
/// <param name="from">From type.</param>
336+
/// <returns>Gigabytes based on given number and given type to convert from.</returns>
337+
public static double ToTB(this int number, DigitalStorage from = DigitalStorage.TB)
338+
{
339+
switch (from)
340+
{
341+
case DigitalStorage.KB:
342+
return number.ToGB(DigitalStorage.KB) * 0.001;
343+
case DigitalStorage.MB:
344+
return number.ToGB(DigitalStorage.MB) * 0.001;
345+
case DigitalStorage.GB:
346+
return number * 0.001;
347+
case DigitalStorage.TB:
348+
default:
349+
return number;
350+
}
351+
}
264352
}
265353
}

src/FluentExtensions/StringEx.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,60 @@ public static async Task<string> ReadFromDiskAsync(this string path)
301301
return await outputFile.ReadToEndAsync().ConfigureAwait(false);
302302
}
303303

304+
/// <summary>
305+
/// Adds text to end of another text.
306+
/// </summary>
307+
/// <param name="text">Source text.</param>
308+
/// <param name="textToAdd">Text to add.</param>
309+
/// <param name="addSpaceBeforeAddition">Whether to add space before the addition.</param>
310+
/// <returns>A Combined string of the old and new text.</returns>
311+
public static string AddToEnd(this string text, string textToAdd, bool addSpaceBeforeAddition = true)
312+
{
313+
var additionText = addSpaceBeforeAddition ? string.Concat(" ", textToAdd) : textToAdd;
314+
return string.Concat(text, additionText);
315+
}
304316

317+
/// <summary>
318+
/// Adds text to start of another text.
319+
/// </summary>
320+
/// <param name="text">Source text.</param>
321+
/// <param name="textToAdd">Text to add.</param>
322+
/// <param name="addSpaceAfterAddition">Whether to add space after the addition.</param>
323+
/// <returns>A Combined string of the old and new text.</returns>
324+
public static string AddToStart(this string text, string textToAdd, bool addSpaceAfterAddition = true)
325+
{
326+
var additionText = addSpaceAfterAddition ? string.Concat(textToAdd, " ") : textToAdd;
327+
return string.Concat(additionText, text);
328+
}
305329

330+
/// <summary>
331+
/// Gets portion of string based on given characters and position to get.
332+
/// </summary>
333+
/// <param name="source">Text to take the portion from.</param>
334+
/// <param name="characters">Number of characters to get.</param>
335+
/// <param name="from">Whether to take the portion for the start or from the end.</param>
336+
/// <returns>A portion of string based on given characters and position to get.</returns>
337+
public static string Take(this string source, int characters, Position from = Position.Start)
338+
{
339+
var position = from == Position.Start ? 0 : source.Length;
340+
var startIndex = from == Position.End ? (position - characters) : position;
341+
return source.Substring(startIndex, characters);
342+
}
343+
344+
/// <summary>
345+
/// Replaces multiple values inside a string with one value.
346+
/// </summary>
347+
/// <param name="source">Text to apply replaces.</param>
348+
/// <param name="values">Values to replace.</param>
349+
/// <param name="value">value to replace values with.</param>
350+
/// <returns>A Text based on replaced values.</returns>
351+
public static string ReplaceMultipleWithOne(this string source, string[] values, string value)
352+
{
353+
string result = source;
354+
foreach (var val in values)
355+
result = result.Replace(val, value);
356+
357+
return result;
358+
}
306359
}
307360
}

0 commit comments

Comments
 (0)