Skip to content

A library containing common extensions methods that you might need in day to day projects

License

Notifications You must be signed in to change notification settings

mruhul/Bolt.Common.Extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

125 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bolt.Common.Extensions

A library containing common extensions methods. A nuget package also available https://www.nuget.org/packages/Bolt.Common.Extensions/

Guid

  • ToGuid() - Convert a string to nullable guid?

    var id = new Guid("58066027-ED5F-4488-818F-9D93E98EDB4C");
    
    "58066027-ED5F-4488-818F-9D93E98EDB4C".ToGuid().ShouldBe(id);
    
    "d".ToGuid().IsEmpty().ShouldBe(true);
    
    
  • IsEmpty() - Return whether a nullable guid is null or empty

    Guid? id = null;
    Guid? emptyId = Guid.Empty;
    
    id.IsEmpty().ShouldBe(true);
    emptyId.IsEmpty().ShouldBe(true);
    

String

  • NullSafe() - Return a empty string when the string is null

     var name = null;
     name.NullSafe().ShouldBe(string.Empty);
     "hello".NullSafe().ShouldBe("hello");
    
  • IsEmpty() - Return true when string is null or empty or whitespace

     var name = null;
     name.IsEmpty().ShouldBe(true);
     var name = " ";
     name.IsEmpty().ShouldBe(true);
     var name = "";
     name.IsEmpty().ShouldBe(true);
    
  • HasValue() - Return true when string not null and not empty and not white space

     var name = null;
     name.HasValue().ShouldBe(false);
     var name = " ";
     name.HasValue().ShouldBe(false);
     var name = "";
     name.HasValue().ShouldBe(false);
    
  • IsSame(string) - Compare with another string in case insensitive way

var greetingsLowerCase = "hello world";
var greetings = "Hello World";
var name = "Ruhul Amin";

greetings.IsSame(greetingsLowerCase).ShouldBe(true);
grretings.IsSame(name).ShouldBe(false);
  • IsSameAny(params string[] values) - return true when any of the supplied value match with source string. comparison done in case insensitive way
  // match case insesnsitive way
  var sut = "hello";
  var got = sut.IsSameAny("Hello", "test");
  got.ShouldBe(true);

  // no match
  var got = sut.IsSameAny("test", "test2");
  got.ShouldBe(false);

  // null safe
  var sut = null;
  got = sut.IsSameAny("test");
  got.ShouldBe(false);
  • StartsWithIgnoreCase(string value) - check whether a string starts wtih supplied value in case insensitive way
  // return true as source starts with supplied value
  var sut = "hello world!"
  var got = sut.StartsWithIgnoreCase("HELLO");
  got.ShouldBe(true);

  // return false when source is null
  string sut = null;
  sut.StartsWithIgnoreCase("s").ShouldBe(false);
  • EndsWithIgnoreCase(string value) - Check whether a string ends with supplied value in case insensitive way

    var sut = "hello world";
    sut.EndsWithIgnoreCase("WORLD").ShouldBeTrue();
    
  • ContainsIgnoreCase(string value) - check whether a string contains in source string in case insensitive way

    var sut = "Hello World!";
    sut.ContainsIgnoreCase("WORLD").ShouldBeTrue();
    
  • FormatWith(params object[]) - Format string with arguments

var greetingsTemplate = "Hello {0}!";
greetingsTemplate.FormatWith("World").ShouldBe("Hello World!");
  • ToSlug(int,bool) - Generate url friendly version of a string
"Hello World--345".ToSlug().ShouldBe("hello-world-345");

Based on this resource: http://www.danharman.net/2011/07/18/seo-slugification-in-dotnet-aka-unicode-to-ascii-aka-diacritic-stripping/

  • Truncate(int, string?) - Truncate a string to the specified length and optionally adds a suffix
"Hello world".Truncate(4,'...').ShouldBe('Hell...');
"Hello".Truncate(50, ' read more... ').ShouldBe("Hello");
"Hello world".Truncate(4).ShouldBe("Hell");

DateTime

  • ToDateTime() - Convert a string to nullable datetime

    "2012-04-05".ToDateTime().ShouldBe(new DateTime(2015,04,05));
    "dsfsd".ToDateTime().ShouldBe(null);
    
  • ToUtcDateTime() - Convert a string to UTC datetime. The method assumes the string is formatted as RoundTripKind (o). If the converted datetime not UTC kind then transform to universaltime before return.

    var utcDateFromUtcDateString = "2021-07-10T12:40:21.3389002Z".ToUtcDateTime();
    var utcDateFromLocalDateString = "2021-07-10T22:40:21.3389002+10:00".ToUtcDateTime();
    
    utcDateFromUtcDateString.ShouldBe(utcDateFromLocalString);
    
    
  • FormatAsUtc() - Format a datetime to utc format RoundTripKind (o). If the supplied datetime kind is not UTC then this function convert the datetime to universal time and then format to stirng

    var utcDateString = DateTime.UtcNow.FormatAsUtc(); // should print date as format similar to `2021-07-10T12:40:21.3389002Z`
    var localDateString = DateTime.Now.FormatAsUtc(); // should convert the time to Unversal time and then format similar to `2021-07-10T12:40:21.3389002Z`
    
  • ToUnixTimeSeconds() - Returns the number of seconds that have elapsed since 1970-01-01T00:00:00Z.

  • ToUnixTimeMilliseconds() - Returns the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z.

Enumerable

  • NullSafe() - Return an Enumerable.Empty<T> when collection is null

    IEnumerable<string> values = null;
    values.NullSafe().Count().ShouldBe(0);
    
  • IsEmpty() - Return whether an enumerable is null or empty

    
    IEnumerable<string> values = null;
    var colors = new List<string>();
    
    values.IsEmpty().ShouldBe(true);
    colors.IsEmpty().ShouldBe(true);
    
    
  • HasItem() - Reverse method of IsEmpty()

    
    IEnumerable<string> values = null;
    var colors = new List<string>();
    
    values.HasItem().ShouldBe(false);
    colors.HasItem().ShouldBe(false);
    
    
  • ForEach<T>(Action) - Do something with each element of an collection

    
    IEnumerable<string> values = null;
    var colors = new []{"Red","Green"};
    
    values.NullSafe().ForEach(x => Console.WriteLine(x)); // do nothing
    colors.ForEach(color => Console.WriteLine(color)); // print our all colors in collection
    
  • Join(IEnumerable<string>, string) - Join a collection of string with seperator

  var colors = new []{ "Red", "Green", "Blue" };
  colors.Join(",").ShouldBe("Red,Green,Blue");
  • JoinNonEmptyValues(IEnumerable<string>?, string?) - Truncate a string to the specified length and optionally adds a suffix
["First", null, "", "Last".JoinNonEmptyValues(",").ShouldBe("First,Last");
["First", null, "", "Last".JoinNonEmptyValues(',').ShouldBe("First,Last");
["First", null, "", "Last".JoinNonEmptyValues().ShouldBe("FirstLast");
  • Append<T>(params T[] items) - add items at end of enumerable
  var colors = new []{"Red", "Green"};
  colors.Append("Blue"); // colors collection will now have ["Red","Green","Blue"]

  • Prepend<T>(params T[] items) - add items at beginning of enumerable
  var colors = new []{"Green", "Blue"};
  colors.Append("Red"); // colors collection will now have ["Red","Green","Blue"]

Dictionary

  • NullSafe() - Return a new instance of dictionary if supplied source is null

    Dictionary<string,string> source = null;
    source.NullSafe().Count.ShouldBe(0);
    
  • GetValueOrDefault(TKey key, [TValue defaultValue]) - Return value of the key if exists otherwise return the default(TValue) or supplied default value.

    var source = new Dictionary<string, string> {{"name", "ruhul"}};
    
    source.TryGetValueOrDefault("name").ShouldBe("ruhul");
    source.TryGetValueOrDefault("postcode").ShouldBe(null);
    source.TryGetValueOrDefault("postcode", "3000").ShouldBe("3000");
    
  • Merge(IDictionary<TKey,TValue>) - Merge a dictionary with another dictionary and return a new dictionary instance

    var source = new Dictionary<string,string>{
      ["one"] = "1"
    }
    
    var other = new Dictionary<string,string>{
      ["two"] = "2"
    }
    
    var got = source.Merge(other);
    got.Count.ShouldBe(2);
    got["one"].ShouldBe("1");
    got["two"].ShouldBe("2");
    
  • Append(IDictionary<TKey,TValue>) - Same as merge, difference is Append alter the supplied source dictionary and add new items in source dictionary. Merge method doesn't do any change in source dictionary

    var source = new Dictionary<string,string>{
      ["one"] = "1"
    }
    
    var other = new Dictionary<string,string>{
      ["two"] = "2"
    }
    
    _ = source.Append(other);
    source.Count.ShouldBe(2);
    source["one"].ShouldBe("1");
    source["two"].ShouldBe("2");
    

Enum

  • ToEnum<TEnum> - Convert a string to nulllable enum

    enum Color
    {
      None,
      Red
    }
    
    "Red".ToEnum<Color>().ShouldBe(Color.Red);
    "Unknown".ToEnum<Color>().ShouldBe(null);
    "Unknown".ToEnum<Color>().NullSafe().ShouldBe(Color.None);
    
  • Description() - Return the description attribute value of an enum

private enum Color
{
   [System.ComponentModel.Description("Red Color")]
   Red,
   Green,
   Blue
}

var redColor = Color.Red;
var greenColor = Color.Green;

redColor.Description().ShouldBe("Red Color");
greenColor.Description().ShouldBe("Green");

Numeric

  • ToInt() - Convert a string to nullable int?

    "123".ToInt().ShouldBe(123);
    "Hello".ToInt().ShouldBe(null);
    "Hello".ToInt().NullSafe().ShouldBe(0);
    
  • ToDecimal() - Convert a string to nullable decimal?

    "123.56".ToDecimal().ShouldBe(123.56);
    "Hello".ToDecimal().ShouldBe(null);
    "Hello".ToDecimal().NullSafe().ShouldBe(0);
    
  • ToDouble() - Convert a string to nullable double?

    "123.56".ToDouble().ShouldBe(123.56);
    "Hello".ToDouble().ShouldBe(null);
    "Hello".ToDouble().NullSafe().ShouldBe(0);
    
  • ToLong() - Convert a string to nullable long?

    "123".ToLong().ShouldBe(123);
    "Hello".ToLong().ShouldBe(null);
    "Hello".ToLong().NullSafe().ShouldBe(0);
    
  • ToFloat() - Convert a string to nullable float?

    "1.56".ToFloat().ShouldBe(1.56);
    "Hello".ToFloat().ShouldBe(null);
    "Hello".ToFloat().NullSafe().ShouldBe(0);
    

Null Related

  • NullSafe() - Return a defult(T) of any struct is null

    Color? value = null;
    value.NullSafe().ShouldBe(Color.None);
    
    int? value = null;
    value.NullSafe().ShouldBe(0);
    
  • NullSafe() - Return Enumerable.Empty when a enumerable is null. Handy for chaining methods

    string[] values = null;
    values.NullSafe().Count().ShouldBe(0);
    
  • NullSafeGet<T> - Execute an func of a object and return the value. When the object is null return default(T)

    Person p = null;
    p.NullSafeGet(x => x.Name).ShouldBe(null);
    
    Person p = new Person { Name = "Test Name" };
    p.NullSafeGet(x => x.Name).ShouldBe("Test Name");
    
  • NullSafeDo<T> - Execute an action related to an object. When the object is null do nothing

    Person p = null;
    p.NullSafeDo(x => x.Name = "Mr. {0}".FormatWith(x.Name));
    p.ShouldBeNull();
    
    Person p = new Person { Name = "Test Name" };
    p.NullSafeDo(x => x.Name = "Mr. {0}".FormatWith(x.Name));
    p.Name.ShouldBe("Mr. Test Name");
    

Task related

  • WrapInTask - Fluently convert any object to a Complated Task

    public class InMemoryStockProvider : IStockProvider
    {
      public Task<Stock> GetAsync(string id)
      {
        var stock = GetAssetFromMemory(id);
    
        // instead of writing as
        // Task.FromResult(stock);
        return stock.WrapInTask();
      }
    }
    

About

A library containing common extensions methods that you might need in day to day projects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •