Skip to content

Instantly share code, notes, and snippets.

@micahriggan
micahriggan / conditional-properties.md
Last active March 28, 2019 17:31
adding conditional properties to objects using ...bool && object

Javascript Property Wizardry

Did you know you can add properties conditionally to an object with spread?

so instead of

function getUser(banned) {
  const userStatus = {banned, banReason: 'Wizard'};
  // only combine the user status if the user is banned
 const user = { name: 'Micah' };
@micahriggan
micahriggan / 3-destructuring-params.js
Created March 26, 2019 03:08
Simple examples of destructuring params
function log(...args) {
console.log(args);
}
function getName({name}) {
console.log(name);
}
function pop([first, ...rest]) {
return rest;
}
function push(array, elem) {
@micahriggan
micahriggan / 2-destructure-object.js
Created March 26, 2019 02:50
Common examples of destructuring objects
const Book = {
title: 'A book title',
pages: 55,
height: 100,
weight: 2,
price: 3.50
};
function test1() {
const {title} = Book;
console.log(title);
@micahriggan
micahriggan / 1-destructure.js
Created March 26, 2019 02:32
Some simple destructuring examples
function getPeople() {
return [{name: "Bob"}, {name: "Sam"}, {name: "Tom"}];
}
function test1() {
const [firstPerson] = getPeople();
console.log(firstPerson);
}
function test2() {
const [firstPerson, ...rest] = getPeople();
console.log(rest);
@micahriggan
micahriggan / 3-generic-table-example.ts
Last active March 26, 2019 01:58
A typesafe example using generics
interface Person {
name: string;
age: number;
}
interface Car {
modelName: string;
year: number;
}
@micahriggan
micahriggan / 2-bad-table-example.ts
Created March 26, 2019 00:41
Showing the errors that can occur from using any instead of a generic
interface Person {
name: string;
age: number;
}
interface Car {
modelName: string;
year: number;
}
@micahriggan
micahriggan / 1-simple-generics.ts
Created March 26, 2019 00:18
Example to show a typesafe way to log values from objects
/* given an object of type T,
* log the value of a property, specified by the argument key
*/
function logValue<T>(obj: T, key: keyof T) {
console.log(obj[key]);
}
function simpleTest() {
const Micah = {name: 'Micah'};
logValue(Micah, "name")
@micahriggan
micahriggan / 3-async-dependencies.js
Last active March 25, 2019 21:02
Asynchronous calls which depend on the result of another asynchronous call
function fetchData(data) {
return new Promise(resolve => setTimeout(() => {
resolve(data)
}, 1000))
}
function getLoggedInUser() {
return fetchData('user1');
}
@micahriggan
micahriggan / data-dependencies.js
Created March 23, 2019 22:24
Adding async values that are fetched in parallel vs serially
function randomNumber() {
const rand = Math.random() * 100;
return new Promise(resolve => setTimeout(() => {
resolve(rand)
}, 1000))
}
async function addExampleSerial() {
console.time('add-serial');
const number1 = await randomNumber();
@micahriggan
micahriggan / serial-promises.js
Created March 23, 2019 21:59
Serial Promises vs Parallel Promises
function wait(waitTime) {
return new Promise(resolve => setTimeout(() => {
console.log(`waited ${waitTime} ms`)
resolve()
}, waitTime));
}
async function serial() {
console.time('serial');
await wait(1000);