Skip to content

Instantly share code, notes, and snippets.

View JoshuaKGoldberg's full-sized avatar
🌴
Mostly on vacation through 5/20. Expect little.

Josh Goldberg ✨ JoshuaKGoldberg

🌴
Mostly on vacation through 5/20. Expect little.
View GitHub Profile
@JoshuaKGoldberg
JoshuaKGoldberg / sinon-stub-return.ts
Created January 31, 2017 20:16
Sinon stub with a return value
attempt_CallsOnFail_IfMyApiFails() {
// Arrange
const onFail = sinon.stub();
const onSucceed = sinon.stub();
const externalApi = sinon.stub().returns(false);
const actor = new Actor({ externalApi, onFail, onSucceed });
// Act
actor.attempt(); // internally calls externalApi
@JoshuaKGoldberg
JoshuaKGoldberg / lolex-clock.ts
Created January 31, 2017 20:17
Lolex clock for simulating delays
actAfterDelay_CallsAction_AfterDelay() {
// Arrange
const action = sinon.stub();
const clock = lolex.createClock<lolex.BrowserClock>();
const delay = 100;
const actor = new Actor({ action, clock, delay });
// Act
actor.actAfterDelay();
clock.tick(delay);
@JoshuaKGoldberg
JoshuaKGoldberg / component.ts
Created December 30, 2017 21:41
Ridiculously tiny IoC with almost no features
import { IComponentListing } from "./listings";
import { getFunctionName } from "../utils";
export const component = (componentFunction: any, initializer?: Function) => {
const componentFunctionName = getFunctionName(componentFunction);
return initializer === undefined
? createComponentClass(componentFunction, componentFunctionName)
: createComponentInitializer(componentFunction, componentFunctionName, initializer);
};
@JoshuaKGoldberg
JoshuaKGoldberg / npm-debug.log
Created January 17, 2018 21:38
Errors from npm i with an empty package-lock.json
0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli 'C:\\Users\\jogol\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'i' ]
2 info using npm@5.5.1
3 info using node@v8.8.1
4 verbose npm-session c1cc1bbe74c6487e
5 silly install runPreinstallTopLevelLifecycles
6 silly preinstall electric-boogaloo@0.1.0
7 info lifecycle electric-boogaloo@0.1.0~preinstall: electric-boogaloo@0.1.0
@JoshuaKGoldberg
JoshuaKGoldberg / CreateRouteDetails.ts
Last active May 25, 2018 14:02
🃏 Unit Tests: SOLID, Modern, Electric Boogaloo 🃏
export const createRouteDetails = (lookupId?: string): IRouteDetails => {
if (lookupId === undefined) {
return {
pageMode: PageMode.MySways,
};
}
return {
lookupId,
pageMode: PageMode.Document,
@JoshuaKGoldberg
JoshuaKGoldberg / test-event-emitting.ts
Created June 1, 2018 03:10
Sample of event listening with selenium-webdriver and squee
import { createEventEmitter, IEventReceiver } from "squee";
import * as Selenium from "selenium-webdriver";
// Put whatever you'd like in this...
interface IPostMessageData {
identifier: string;
}
declare const window: {
__MY_IS_EVENT_DATA_VALID__: (data: any) => data is IPostMessageData;
@JoshuaKGoldberg
JoshuaKGoldberg / clipboard-polyfill.d.ts
Last active May 2, 2019 12:56
Removing an incorrect @types definition
// webpack/assets/javascripts/clipboard-polyfill.d.ts
// This file should be in that path 👆 but Gists don't allow subpaths...
// Overrides incorrect types in node_modules/clipboard-polyfill/build/clipboard-polyfill.d.ts
// See https://github.com/lgarron/clipboard-polyfill/issues/106
@JoshuaKGoldberg
JoshuaKGoldberg / esri.d.ts
Last active May 2, 2019 12:59
Replacing an incorrect @types definition
// webpack/assets/javascripts/esri.d.ts
// This file should be in that path 👆 but Gists don't allow subpaths...
declare module "esri" {
const wat: string;
export wat;
}
@JoshuaKGoldberg
JoshuaKGoldberg / my-esri.js
Created May 2, 2019 12:59
Wrapping and exporting an ambient @types module
// webpack/assets/javascripts/my-esri
// This file should be in that path 👆 but Gists don't allow subpaths...
// I'm not super sure this works the way I want it to
// In theory, this /// include should only import the types into *this* file's context, not the ambient context
// 🤷
/// <reference path="../../../node_modules/@types/esri/index.d.ts" />
export const Esri = window.Esri;
@JoshuaKGoldberg
JoshuaKGoldberg / binary.ts
Last active August 6, 2019 21:51
Silly Binary Logic in TypeScript's Type System
type Bit = 0 | 1;
type BitFlip<A> = A extends 0
? 1
: 0;
type BitOr<A, B> = [A, B] extends [0, 0]
? 0
: 1;