Skip to content

Instantly share code, notes, and snippets.

View JustinDFuller's full-sized avatar
🧐

Justin Fuller JustinDFuller

🧐
View GitHub Profile
@JustinDFuller
JustinDFuller / snippets.js
Last active October 8, 2018 18:14
Snippet for Ava tests
{
"Ava Test Snippet": {
"prefix": "ava",
"body": [
"import test from 'ava'",
"",
"import ${1} from '${2}'",
"",
"test('It exports a function', function (t) {",
" t.is(typeof ${3}, 'function')",
@JustinDFuller
JustinDFuller / test.js
Created October 6, 2018 00:38
untested code
/* source code file "myFunction.js" */
export default myFunction(callback) {
return callback(10)
}
/* test file */
import assert from 'assert'
import test from 'my-favorite-test-library'
@JustinDFuller
JustinDFuller / test.js
Created September 19, 2018 11:20
Exports a function
import assert from 'assert'
import test from 'my-favorite-test-library'
import functionality from './myFunctionality'
test('It exports a function', function () {
assert.strictEqual(typeof functionality, 'function')
})
@JustinDFuller
JustinDFuller / Promises.js
Created August 12, 2018 12:43
Using Promises with promise-funnel
const { cork, uncork, wrap } = promiseFunnel()
const wrapped = wrap(function () {
return 'done!'
})
cork()
// An unresolved promise is returned
const promise = wrapped().then(function (res) {
@JustinDFuller
JustinDFuller / bluebird.js
Created August 12, 2018 12:31
Using Bluebird with promise-funnel
import Promise from 'bluebird'
import createFunnel from 'promise-funnel'
const funnel = createFunnel({
Promise, // promise-funnel will use Bluebird promises now
})
// The usage is exactly the same as before after this!
@JustinDFuller
JustinDFuller / Authentication.js
Created August 12, 2018 01:48
Using promise-funnel for authentication
import createFunnel from 'promise-funnel'
const funnel = createFunnel()
function fetchData(...options) {
// This will fail if the user is unauthenticated
return fetch(...options)
}
function login() {
@JustinDFuller
JustinDFuller / WithDatabase.js
Created August 12, 2018 01:36
Using promise-funnel with a database
import mysql from 'mysql'
import createFunnel from 'promise-funnel'
const funnel = createFunnel()
funnel.cork()
const connection = mysql.createConnection({
/* options here */
})
import test from 'ava';
function doubleSay (str) {
return str + ", " + str;
}
function capitalize (str) {
return str[0].toUpperCase() + str.substring(1);
}
@JustinDFuller
JustinDFuller / nullish-coalescing.test.js
Last active March 31, 2018 13:06
Testing nullish coalescing
import test from 'ava';
test('Nullish coalescing defaults null', (t) => {
t.is(null ?? 'default', 'default');
});
test('Nullish coalescing defaults undefined', (t) => {
t.is(undefined ?? 'default', 'default');
});
@JustinDFuller
JustinDFuller / optional-chaining.test.js
Created March 28, 2018 19:19
Short circuiting with optional chaining
let neighborCount = 0;
function getNextNeighbor(neighbors) {
return neighbors?.[++neighborCount];
}
test('It short circuits expressions', (t) => {
const neighbors = getNeighbors(data);
t.is(getNextNeighbor(neighbors), 'jane doe');
t.is(getNextNeighbor(undefined), undefined);