Skip to content

Instantly share code, notes, and snippets.

@aderaaij
aderaaij / es6-tagged-template.js
Last active November 12, 2017 20:11
With tagged templates we can modify a template string with a function tagged in front of it. str += `${string} <span class='hl'>${values[i] || ''}</span>`;
/* The highlight function we tagged on to our template string
Arguments: An array of strings in between variables and the variables
We could use `strings, age, name` as arguments but using the spread operator
to get all the variables is better scalable. */
function highlight(strings, ...values) {
let str = '';
/* For each string we add both string and value to the let value. If value doesn't exist,
add empty string. */
strings.forEach((string, i) => {
str += `${string} <span class='hl'>${values[i] || ''}</span>`;
@aderaaij
aderaaij / es6-html-render-function.js
Last active November 12, 2017 21:33
A function to render html inside a template string. From es6.io
const beer = {
name: 'Belgian Wit',
brewery: 'Steam Whistle Brewery',
keywords: ['pale', 'cloudy', 'spiced', 'crisp']
};
function renderKeywords(keywords) {
return `
<ul>
${keywords.map(keyword => `<li>keywords: ${keyword}</li>`).join('')}
@aderaaij
aderaaij / es6-template-string-ternary-operator.js
Last active November 12, 2017 15:13
In this template string we use a ternary conditional operator to only show the 'featured' part when it's available. If not available, the last part is omitted. From es6.io
const song = {
name: 'Dying to live',
artist: 'Tupac',
featuring: 'Biggie Smalls'
};
const markup = `
<div class="song">
<p>
${song.name} — ${song.artist}
@aderaaij
aderaaij / es6-template-string-loop.js
Last active November 12, 2017 15:13
A way to open a template string and loop through arrays or objects to create another template string within. From es6.io
const dogs = [
{ name: 'Snickers', age: 2 },
{ name: 'Hugo', age: 8 },
{ name: 'Sunny', age: 1 }
];
const markup = `
<ul class="dogs">
${dogs.map(dog => `
<li>
@aderaaij
aderaaij / es6-default-arguments.js
Last active November 12, 2017 15:14
An example of how to add default arguments to a named function in es6.
function calculateBill(total, tax = 0.10, tip = 0.15) {
return total + (total * tax) + (total * tip);
}
console.log(calculateBill(100));
// Calculate bill with custom tip value and default tax value. Pass undefined
// expliclitely to use a default value that's not the first or last function argument
const bill = caluculateBill(100, undefined, 0.20);
console.log(bill);
@aderaaij
aderaaij / es6-arrow-functions
Created November 11, 2017 13:34
Simle Arrow Function examples
const names = ['michelangelo', 'donatello', 'leonardo'];
const fullNames = names.map(name => `${name} turtle`);
console.log(fullNames);
const sayMyName = name => alert(`hi ${name}`)
sayMyName('Raphaello');
@aderaaij
aderaaij / arrow-function-examples
Last active November 12, 2017 15:32
Arrow function examples for return and filter, from https://es6.io. 1. Return an object with winners, the race they participate in and their finishing place based on the order of the array. Note that the second argument is shorthand, when both the name and value are equal. Also note the braces around the curly brackets to immediately invoke a re…
const race = '100m Dash';
const winners = ['Hunter Gath', 'Singa Song', 'Imda Bos'];
// Return an object with winners, the race they participate in and their
// finishing place based on the order of the array
// Note that the second argument is shorthand, when both the name and value
// are equal
const win = winners.map((winner, i) => ({ name: winner, race, place: i+1}));
console.log(win);
@aderaaij
aderaaij / es6-switch-variables
Created November 11, 2017 13:03
Switch out variables by destructuring in es6
// Switch out variables by destructuring in es6
let first = 'first';
let second = 'second';
console.log(first, second);
[first, second] = [second, first];
console.log(first, second);
@aderaaij
aderaaij / browser-sync-start-directory
Last active December 22, 2017 17:58
Start a browsersync server in underlaying directory https://browsersync.io/docs/command-line
browser-sync start -s --directory "/"
browser-sync start -s --directory "/" --files "**/*.html"
@aderaaij
aderaaij / times.js
Last active November 12, 2017 15:14
https://stackoverflow.com/a/30452949/4474075 The code below is written using ES6 syntaxes but could just as easily be written in ES5 or even less. ES6 is not a requirement to create a "mechanism to loop x times" If you don't need the iterator in the callback, this is the most simple implementation
// A function to repeat whatever you feed it an x amount of time
// https://stackoverflow.com/a/30452949/4474075
const times = x => f => {
if (x > 0) {
f()
times (x - 1) (f)
}
}
// use it