Skip to content

Instantly share code, notes, and snippets.

View sidouglas's full-sized avatar

sidouglas sidouglas

  • Sydney, Australia
View GitHub Profile
@sidouglas
sidouglas / createFetch.ts
Last active May 6, 2024 13:50
Using Fetch with Zod in a 2 step request Factory
import type { ZodTypeAny, z } from 'zod';
//@see https://zod.dev/?id=writing-generic-functions
export const createFetch = <Output extends ZodTypeAny>(
zodSchema: Output
): ((input: RequestInfo | URL, init?: RequestInit) => Promise<z.infer<Output>>) => {
return (input, init) =>
fetch(input, init).then((res) => {
if (!res.ok) {
throw new Error(res.statusText);
@sidouglas
sidouglas / script.applescript
Last active December 24, 2022 02:11
Activate All Brave Windows containing substring title
tell application "Brave Browser"
activate
repeat with w in (windows)
set j to 0
repeat with t in (tabs of w)
set j to j + 1
if title of t contains "YOUR_NAME" then
set (active tab index of w) to j
set index of w to 1
tell application "System Events" to tell process "Brave Browser"
@sidouglas
sidouglas / htmlDump.ts
Last active December 16, 2022 04:27
React Testing Library Dump Component html to a file
// dump to your desktop the contents of a component at a point in time
// htmlDump(component);
function htmlDump(component: RenderResult) {
const fs = require('fs');
const path = require('path');
const os = require('os');
const rtl = require('react-testing-library');
const dir = os.homedir() + '/Desktop/htmlDump';
if (!fs.existsSync(dir)) {
@sidouglas
sidouglas / moment
Created October 4, 2022 10:10
Mock Moment + return Moment that is static
jest.mock('moment', () => {
const mock = (timeStamp: string = '2022-09-29T00:00:00Z') =>
jest.requireActual('moment')(timeStamp);
return Object.assign(mock, jest.requireActual('moment'));
});
@sidouglas
sidouglas / object-to-form-data.js
Last active May 13, 2022 02:57
ObjectToForm Data
/**
* @param {Object} object
* @param {'GET'|'POST'|'PUT'|'PATCH'|'DELETE'} verb
* @returns {FormData}
*/
export function objectToFormData (object, verb = 'POST') {
const formData = Object.keys(object).reduce((formData, key) => {
const value = typeof object[key] === 'boolean'
? Number(object[key])
: object[key]
@sidouglas
sidouglas / jest-spy-on-all.js
Last active April 26, 2022 01:43
Jest spy on everything
export default (interrogate, mockImplementations = {}) => {
const spies = {}
interrogate.prototype && Object.getOwnPropertyNames(interrogate.prototype).forEach((name) => {
if (name !== 'constructor') {
spies[`${name}Spy`] = createSpy(interrogate.prototype, mockImplementations, name)
}
// AFAIK, you can't spy on a constructor
// top answer is here: https://stackoverflow.com/a/48486214/1090606
// For now, mock the entire module to assert that the constructor was called correctly.
@sidouglas
sidouglas / mediaStream.js
Created March 23, 2021 04:45
MediaStream to MediaStream JSON Mock
// run this in the browser console.
function buildJSON(streams) {
clear();
var mock = []
var mockTrack = (stream) => {
const output = stream.reduce((acc, next) => {
const clone = {}
for (var i in next) {
if (typeof next[i] === 'string' || typeof next[i] === 'boolean') {
@sidouglas
sidouglas / your.spec.js
Last active February 19, 2021 10:15
Live Template for JetBrains (Velocity) for a new test case
#set($pascalCaseName = ${StringUtils.removeAndHump($NAME, '-')})
#set($titleCaseName = ${pascalCaseName.replaceAll("([A-Z])", " $1").trim()})
import { mountFunction } from 'tests/unit/mount'
import $pascalCaseName from '@/components/#if($folder != '')$folder/#end$pascalCaseName'
import { getLastEvent, getLastItemFromArray } from 'tests/helpers'
describe('$titleCaseName', () => {
beforeEach(() => {
})
@sidouglas
sidouglas / prefill.js
Created February 6, 2021 11:58
Prefilling an Array with Values
Array.from({length: 5}, (_,index) => ++index) // length of 5, and a callback to fill a value [1,2,3,4,5]
@sidouglas
sidouglas / provider.example.js
Last active February 3, 2021 09:12
Vue 2 Injector / Provider
export function createProvider (vm) {
const provider = {}
return (properties) => {
properties.forEach((property) => {
const [[key, alias = null]] = typeof property === 'string'
? [[property]]
: Object.entries(property)
Object.defineProperty(provider, key, {
enumerable: true,
get: () => vm[alias || key]