Skip to content

Instantly share code, notes, and snippets.

@mfdj
mfdj / make-sure-to-reject-an-Error-object.js
Created Jul 19, 2021
jest await expect(testSubject()).rejects.toThrow expects the rejected value to be an Error object otherwise just use `rejects.toMatch`
View make-sure-to-reject-an-Error-object.js
// test subjects
const rejectsStringValue = () => Promise.reject('I rejected');
const rejectsErrorValue = () => Promise.reject(new Error('I rejected'));
// tests
describe('rejectsStringValue', () => {
it('will throw in try/catch', async () => {
@mfdj
mfdj / why-alphanumeric.md
Last active Feb 15, 2022
Alphabetize your set items and map keys
View why-alphanumeric.md

Alphabetize your set items and map keys

This document looks at certain data-structures and makes the case for alphanumeric ordering to optimize developer experience (DX).

Strategies for organizing code

Authoring code (like any language) is about expressing an idea in an intentional fashion. Obviously it needs to be correct for the computational device, and succeed as a software production, but we also want to make it comprehensible to our fellow humans.

To make code comprehensible I've observed the following stratgeies (I'm sure more could be found):

@mfdj
mfdj / esm-object-export-is-a-shared-reference.md
Last active Jun 7, 2021
ECMAScript modules (ESM): Yes, when you export an object the reference is shared across all usages
View esm-object-export-is-a-shared-reference.md
$ node index.mjs
🌞 module-a pre-export { name: 'module-a 🌞', obj: { a: '🇨🇦', b: 'bee', c: 'see' } }
👻 module-b pre-export { name: 'module-b 👻', obj: { a: '🇨🇦', b: '🐝', c: 'see' } }
{
  moduleA: { name: 'module-a 🌞', obj: { a: '🇨🇦', b: '🐝', c: '👀' } },
  moduleB: { name: 'module-b 👻', obj: { a: '🇨🇦', b: '🐝', c: '👀' } },
  moduleObjectExport: { a: '🇨🇦', b: '🐝', c: '👀' }
}
@mfdj
mfdj / a.mjs
Last active Sep 15, 2020
module level static data
View a.mjs
const STATE = { counter: 0 };
export { STATE };
@mfdj
mfdj / capybara-webkit-install.sh
Created Jul 27, 2020
installing capybara-webkit on Catalina
View capybara-webkit-install.sh
# installing capybara-webkit on Catalina via Monitor wiki
brew update
cd $(brew --prefix)/Homebrew/Library/Taps/homebrew/homebrew-core
git show 9ba3d6ef8891e5c15dbdc9333f857b13711d4e97 > /dev/null || git fetch --unshallow
git checkout 9ba3d6ef8891e5c15dbdc9333f857b13711d4e97 Formula/qt@5.5.rb
type -a gsed || brew install gnu-sed
gsed -i 's/depends_on :macos => :mountain_lion//' Formula/qt@5.5.rb
brew install qt@5.5
# now gem install capybara-webkit -v '1.15.1' will work
@mfdj
mfdj / ghost_dog.js
Last active Jan 17, 2020
mocking-required-code-in-jasmine
View ghost_dog.js
export const log = function() {
return "ghost-dog";
};
export const blob = function() {
return "blee-bloop";
};
/*
@mfdj
mfdj / logReadableStream.js
Last active Jun 19, 2021
General use body-logger to use with browser fetch - riffing on https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/getReader
View logReadableStream.js
/*
fetch('http://example.com')
.then(res => res.ok ? logReadableStream(res.body) : Promise.reject(`Status: ${res.status}`))
.catch(console.log)
*/
let logReadableStream = function (stream) {
const reader = stream.getReader();
let bytesReceived = 0;
let result = new Uint8Array(0);
View suite-v2-visual-tweaks.html
<table>
<thead>
<tr>
<th>Before</th>
<th>After</th>
</tr>
</thead>
<tbody>
<tr>
<td>
@mfdj
mfdj / macos-newrelic-php-extension-install-steps.sh
Last active Aug 30, 2017
Install New Relic PHP extension on macOS
View macos-newrelic-php-extension-install-steps.sh
# Grabbed archive from https://download.newrelic.com/php_agent/release/ and decompressed
gzip -dc newrelic-php*.tar.gz | tar xf -
cd newrelic-php*/
# manually edited newrelic-install to not use sudo because it's contrary to how Homebrew (and thus most of my system) behaves
# Homebrew'd PHP doesn't use PHP's typical extensions folder so I created it manually (note path is dependent on php version)
mkdir -p /usr/local/Cellar/php70/7.0.17_9/lib/php/extensions/no-debug-non-zts-20151012
# run install script with custom paths (since /usr/bin is not writeable because of macOS "system integrity protection")
@mfdj
mfdj / assert.php
Created Aug 13, 2017
prove how to safely validate precision
View assert.php
<?php
set_error_handler(function($severity, $message, $filename, $lineno) {
throw new \Error($message, $severity);
});
function strictAssert($expected, $actual) {
if ($actual !== $expected) {
echo PHP_EOL . '✋' . PHP_EOL;
var_dump($actual, $expected);