Skip to content

Instantly share code, notes, and snippets.

View osbornm's full-sized avatar

Matthew Osborn osbornm

View GitHub Profile
@osbornm
osbornm / useInterval.test.ts
Created April 7, 2021 15:45
React: useInterval
import { renderHook } from '@testing-library/react-hooks';
import useInterval from './useInterval';
describe('useInterval', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers();
});
it('should run the callback after the delay', () => {
@osbornm
osbornm / useQueryStringState.test.ts
Last active April 1, 2021 22:32
React: useQueryStringState
import {
useHistory,
useLocation
} from 'react-router-dom';
import queryString from 'query-string';
import { renderHook } from '@testing-library/react-hooks';
import useQueryStringState from './useQueryStringState';
const mockHistory = { push: jest.fn() };
@osbornm
osbornm / AsyncTime.js
Created March 8, 2020 20:11
Async Timeout Function (JavaScript)
function timeout(ms = 1000) {
return new Promise(resolve => setTimeout(resolve, ms));
}
### Keybase proof
I hereby claim:
* I am osbornm on github.
* I am osbornm (https://keybase.io/osbornm) on keybase.
* I have a public key whose fingerprint is 8BD9 6E8E FDFC 2468 BE9C E904 71E8 E463 8BA3 B85E
To claim this, I am signing this object:
@osbornm
osbornm / no-array-from.js
Created March 24, 2017 15:42
eslint array.from
/**
* @fileoverview Disallow the use of Array.From()
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
@osbornm
osbornm / Helpers.js
Created September 4, 2015 21:02
uppercut.js Announcement
// asObservable or asObservableArray: if passed an observable use that instance otherwise wrap it in a new observable
var myModel = function(options){
var self = this;
self.observable = ko.asObservable(options.possibleObservable);
self.observableArray = ko.asObservableArray(options.possibleObservableArray);
return self;
}
// Trackable Observables: the ability to revert to a "good" value
var myForm = function(){
@osbornm
osbornm / javascript.js
Created August 26, 2015 17:28
JS vs TS
var things = [
new Thing1(),
new Thing1(),
new Thing2()
];
var OnlyThingOnes = things.filter(function(t){return t.thingOneOnlyProperty !== undefined});
$cd = Add-Type -memberDefinition @"
[DllImport("winmm.dll", CharSet = CharSet.Ansi)] public static extern int mciSendStringA(string lpstrCommand, string lpstrReturnString, int uReturnLength, IntPtr hwndCallback);
"@ -passthru -name mciSendString\
# Where F an G are the Drive letters
$cd::mciSendStringA("set cdaudio!F door closed wait", $null, 1, 1);
$cd::mciSendStringA("set cdaudio!G door closed wait", $null, 1, 1);
@osbornm
osbornm / bindings.js
Created December 1, 2014 19:40
KnockoutJs href binding
ko.bindingHandlers.href = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
return ko.bindingHandlers['attr']['update'](element, function () {
return { href: valueAccessor() };
}, allBindings, viewModel, bindingContext);
}
}
@osbornm
osbornm / anyEmptyBindings.js
Last active August 29, 2015 14:00
Any & Empty Bindings
function hasItems(data) {
var value = ko.unwrap(data),
result = false;
if (value.length && value.length > 0)
result = true;
return result;
}