bookmark_borderHow to get div height to auto-adjust to background size?

Problem: when we set a background image on div the div size doesn’t adjust to background image size.

Solution:

height: calc(imageRatio * 100vw);

Explanation:

First, we need to get the ratio from the background image. We simply divide one dimension by another. Then we get something like for example 66.4%

When we have image ratio we can simply calculate the height of the div by multiplying the ratio by viewport width: calc(0.664 * 100vw)

A height of a div is automatically updated when the window is resized.

See also: my answer on StackOverflow – https://stackoverflow.com/a/61990449/3775079

bookmark_borderHow to update feature branch from master in Git

If you want to update your git feature branch with the new changes from the master branch, you need to:

  1. Update master branch
  2. Merge or rebase the new changes

TL;DR: merging creates additional commit, rebasing rewrites history. Usually the team chooses the way to handle conflicts in repository so ask your colleagues first what your team approach is.

To merge

git checkout master
git pull
git checkout target_feature_branch
git merge master

To rebase

git pull --rebase origin target_feature_branch 
fix conflict and then → git add . 
git rebase --continue 
git push -f

bookmark_borderWhat does question mark and dot operator ?. mean in C# 6.0?

C# 6.0 introduced new operator – null conditional operator ?.

The way it works is very simple. It checks if the left part of the expression is null and if it’s not it returns the right part. Otherwise, it returns null.

Simple example:

var thisWillBeNulll = someObject.NullProperty?.NestedProperty;
var thisWilllBeNestedProperty = someObject.NotNullProperty?.NestedProperty;

Executable example:

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Person
    {
        public string Name { get; set; }
    }
	
    public class Dog 
    {
        public Person Owner { get; set; }
    } 
    
    public class Program
    {
        public static void Main(string[] args)
        {
            Dog dog = new Dog();
            Console.WriteLine(dog.Owner?.Name == null);
            // this will print True
            dog.Owner = new Person() { Name = "Fred" };
            Console.WriteLine(dog.Owner?.Name); 
            // this will print Fred
        }
    }
}

The above example can be executed here: https://rextester.com/TBC19437

Documentation: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators–and-

bookmark_borderHow to delete all documents from a collection in Google Cloud Firestore?

To delete all documents from a Firebase collection in Firestore we need to get the collection, iterate over its elements and delete each one of them:

const db = new Firestore({
  projectId: "projectId",
  keyFilename: "./key.json"
});

db.collection("collectionName")
  .get()
  .then(res => {
    res.forEach(element => {
      element.ref.delete();
    });
  });