Internet Explorer 11 bugs

Add a class if the browser is Internet Explorer 11 or lower

if ('ActiveXObject' in window) {
    document.documentElement.className = 'is-ie';
}

Fixing childen of a flexbox container exceeding their parent width

Just add width: 100% to the elements, or if you’re really lazy:

.container > * {
    width: 100%;
}

SASS

Getting both value and index from a SASS list

$colors: red, blue, yellow, green;
@for $index from 1 through length($colors) {
    li:nth-child(#{length($colors)}n + #{$index}):before {
        background-image: url('../img/bullet-#{nth($colors, $index)}.svg');
    }
}

Javascript

Is there a quicker way to assign a default value if the result is undefined?

Instead of this:

const bar = foo.bar ? foo.bar : 'bar';

Or this:

let bar;

if (foo.bar) {
  bar = foo.bar;
} else {
  bar = 'bar';
}

Try this:

const bar = foo.bar || 'bar';

Promises

// Transforming jQuery's $.get to a Promise
function get(url) {
    return new Promise(function(resolve, reject) {
        $.ajax({
            method : "GET",
            url : url,
            success : function(data) {
                resolve(data);
            },
            error : function() {
                reject('Something went wrong');
            }
        }
    });
}

get('http://example.com').then(function(data) {
    console.log(data);
});

async/await

const URL = 'https://api.github.com/gists/a09e180c6064906b5271';

async function getGist() {
    try {
        const res = await fetch(URL);
        const data = await res.json();
        return data;
    } catch (err) {
        console.log(err);
    }
}

getGist().then(gist => console.log(gist.description));

Lodash: Transform an array with objects to an object with keys and values

_.mapValues(_.keyBy(data, 'key'), 'value');

Transform this:

[
    { key : 'foo', value : 'bar'},
    { key : 'baz', value : 'qux'}
]

Into this:

{
    'foo' : 'bar',
    'baz' : 'qux'
}

Chrome

Remove URLS from autocomplete

Highlight the item using keyboard arrows and press Shift and Delete keys.