Skip to content

Instantly share code, notes, and snippets.

View isner's full-sized avatar

Matt Isner isner

View GitHub Profile
@isner
isner / index.js
Created August 20, 2018 21:02
Mocking a function within a closure
// import `thirdPartyFunction` as a member of the imported module
import * as myDependency from 'my-dependency'
export default myTestableFunction(arg1) {
// and call `thirdPartyFunction` as a member of the imported module
myDependency.thirdPartyFunction(arg1)
}
@isner
isner / beer-song.js
Last active September 29, 2017 03:38
Beer song code kata
module.exports = function () {
this.verse = (n) => {
const otw = 'on the wall'
const numNow = n === 0 ? 'no more' : n
const itOne = numNow < 2 ? 'it' : 'one'
const numAfter = ((n - 1) < 0) ? 99 : ((n - 1) === 0) ? 'no more' : n - 1
return [
`${cap(numNow)} ${bottle(n)} ${otw}, `,
@isner
isner / toilet-seat.js
Last active August 29, 2015 14:26
Toilet seat touching simulation
var visitCount = 10000;
var touches = 0;
var seatDown = true;
/**
* The "Seat-Down" policy
*
* - Each user places the seat down after conducting
* his or her business.
@isner
isner / scan.js
Created December 10, 2014 16:44
Parallel url-scanner with concurrency limit
var http = require('http');
function async(url, next) {
// Log for demo purposes - remove
console.log('url: ', url);
http.get(url, function (res) {
// setTimeout for demo purposes - remove
setTimeout(function () {
next(res.statusCode);
@isner
isner / gist:9553406
Last active August 29, 2015 13:57
inArray-like check for event.which
var which = event.which; // or whatever defines `which`
var chars = [1, 2, 3, 4];
if (iPressedOneOfThese(chars)) {
// Do my stuff
}
function iPressedOneOfThese(array) {
array.filter(chars, function (char) {
return which === char;
@isner
isner / gist:9486654
Created March 11, 2014 14:19
Working with MySQL query results in PHP
while ($row = $result->fetch_assoc()) {
print_r($row);
echo $row['email'];
}
@isner
isner / gist:8408834
Last active January 3, 2016 04:29
Function that takes your mysqli connection object and a query string and returns a mysql result object
// Performs a database query using PHP's MYSQLI class
// Returns a result object if successful
// Else dies and displays the SQL error and the query string that failed
function db_query($mysqli, $sql) {
$result = $mysqli->query($sql);
if (!$result) {
$message = "SQL error: ".$mysqli->errno." ".$mysqli->error.'<br>';