Skip to content

Instantly share code, notes, and snippets.

View JoshuaKGoldberg's full-sized avatar
💖

Josh Goldberg ✨ JoshuaKGoldberg

💖
View GitHub Profile
@JoshuaKGoldberg
JoshuaKGoldberg / createFunctions.js
Created April 21, 2014 23:28
Dynamic Function Factory in JavaScript
/**
* Given a sketch of how a set of classes should look and inherit, this
* returns a set of functions to be used as constructors for those
* classes.
*
* @param {Object} inheritance A JSON representation of the classes'
* inheritance, with each parent object
* containing the objects of its children,
* all keyed by name.
@JoshuaKGoldberg
JoshuaKGoldberg / RemoveUnusedVariablesGulpTslint.js
Created June 2, 2016 00:00
Remove unused variables from gulp tslint
// Folder prefix for file names if running from a different path than gulp
const fileNamePrefix = "";
// Relies on an "input.txt" to exist in the same directory
// That "input.txt" should be a listing of just the tslint failures
const inputFileName = "input.txt";
const fs = require("fs");
const lines = fs
console.log("Hello my darling");
console.log("Hello my ragtime gaaaaaaal...");
<!DOCTYPE HTML>
<html>
<head>
<title>Test embed bug: Gists</title>
</head>
<body>
<iframe src="https://gist.github.com/jammur/97282dd0ff3696cc10e6a14a3b809cdd.pibb" style="width:700px;height:490px;" />
</body>
</html>
@JoshuaKGoldberg
JoshuaKGoldberg / nprogress-requirejs-interval.js
Created December 18, 2016 05:48
NProgress and RequireJS with an interval
// Start showing a progress bar
NProgress.start();
// Make the user think we're continuously making progress
const timer = setInterval(() => NProgress.inc(), 100);
// Send a network request, and when it's done, hide the bar
sendNetworkRequest().then(() => {
clearInterval(timer);
NProgress.stop();
@JoshuaKGoldberg
JoshuaKGoldberg / nprogress-requirejs-onresourceload.js
Created December 18, 2016 05:49
NProgress and RequireJS with onResourceLoad
// Start showing a progress bar
NProgress.start();
// Tell the user whenever we make progress
requirejs.onResourceLoad = () => NProgress.inc());
// Send a network request, and when it's done, hide the bar
sendNetworkRequest().then(() => {
NProgress.stop();
delete requirejs.onResourceLoad;
@JoshuaKGoldberg
JoshuaKGoldberg / multiply-string-numbers.js
Created December 26, 2016 18:31
Multiply all numbers in a string by a constant
function multiplyNumbersInString(source, constant) {
return source.replace(/((\d\.\d)|(\.\d)|\d)+/g, digit => digit * constant);
}
@JoshuaKGoldberg
JoshuaKGoldberg / nprogress-requirejs-context.js
Last active December 28, 2016 19:13
NProgress and RequireJS with onResourceLoad and contexts._.defined
// Start showing a progress bar
NProgress.start();
// Remember how many resources have been requested
let completed = 0;
let oldPercentage = 0;
// Delay calculations so that if the first of a few resources has many dependencies,
// we don't immediately jump far in the progress bar before starting their loads
requirejs.onResourceLoad = context => {
@JoshuaKGoldberg
JoshuaKGoldberg / Uncaught errors with chai-as-promised.js
Created January 18, 2017 22:48
Demonstrates how chai-as-promised will correctly handle uncaught errors
// Setup: npm install mocha chai chai-as-promised
// Run: node node_modules/mocha/bin/mocha
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
const mocha = require("mocha");
chai.use(chaiAsPromised);
const expect = chai.expect;
@JoshuaKGoldberg
JoshuaKGoldberg / sinon-stub-simple.ts
Created January 31, 2017 20:16
Simple use case for a Sinon stub
succeed_CallsOnSuccess() {
// Arrange
const spy = sinon.stub();
const actor = new Actor(spy);
// Act
actor.succeed();
// Assert
UTF.Assert.True("onSuccess should have been called", spy.calledOnce);