This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
const testSrc = 'https://download.samplelib.com/mp3/sample-3s.mp3'; | |
const audioEventTypes = [ | |
'audioprocess', | |
'canplay', | |
'canplaythrough', | |
'complete', | |
'durationchange', | |
'emptied', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
openapi: 3.0.0 | |
info: | |
title: My microservice | |
# The double quotes are necessary to prevent version from being | |
# interpreted as a floating point number. | |
version: "1.0" | |
paths: | |
'/api/users/{userId}': | |
post: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Imports types from the implementation. | |
import { User } from './api/models/user'; | |
// ... others | |
// Exports types used in the REST layer - a request/response/both per API operation. | |
// This naming convention ensures that the name in OpenAPI won't change even if the | |
// underlying type does. | |
export type IUpdateUserRequestBody = Partial<User>; | |
export type IUpdateUserResponseBody = Omit<User, 'Password'>; | |
// ... others |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// much better | |
interface IGenericRequest { | |
requestId: string; | |
} | |
interface IGetUserRequest extends IGenericRequest { | |
username: string; | |
} | |
function mustBeUserRequest(request: IGenericRequest): asserts request is IGetUserRequest { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// not great | |
interface IGenericRequest { | |
requestId: string; | |
} | |
interface IGetUserRequest extends IGenericRequest { | |
username: string; | |
} | |
function handler(request: IGenericRequest) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// much better | |
interface IGenericRequest { | |
requestId: string; | |
} | |
interface IGetUserRequest extends IGenericRequest { | |
username: string; | |
} | |
function isUserRequest(request: IGenericRequest): request is IGetUserRequest { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const list: Array<number | string> = [1, 'two', 3, 'four']; | |
// the implicit type of both of these consts is Array<string | number> -- not helpful! | |
const numbers = list.filter(thing => typeof thing === 'number'); | |
const strings = list.filter(thing => typeof thing === 'string'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Demonstrates a method for creating bubbling behavior through EventEmitter | |
* composition. With this method a parent class (ConsumerClass) is composed | |
* of instances of helper classes (HelpfulClass), and wants to bubble all of | |
* their events up to itself. The goal is that client code can listen at the | |
* parent level (to receive all events) or at the instance level. | |
* | |
* Limitations without a little more scaffolding: | |
* - no context for determining where an event originated from. | |
* - this implementation assumes that HelpfulClass.emitter will not have |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Awful - hard to parse the grammar with your eyes, easier to mix up parentheses | |
navigator.geolocation.getCurrentPosition((loc) => this.located(loc.coords.latitude, loc.coords.longitude), () => this.located(lat, lng)) | |
// Better - easy to parse out what is happening, but not the meaning... what are the arrow functions for? | |
navigator.geolocation.getCurrentPosition( | |
(loc) => this.located(loc.coords.latitude, loc.coords.longitude), | |
() => this.located(lat, lng) | |
) | |
// Good - I can tell exactly what's going on now. |