Skip to content

Instantly share code, notes, and snippets.

View zeusdeux's full-sized avatar
👾
i wear my sunglasses at night so i can — so i can justify the money spent on 'em

Mudit zeusdeux

👾
i wear my sunglasses at night so i can — so i can justify the money spent on 'em
View GitHub Profile
@zeusdeux
zeusdeux / Either.ts
Last active February 19, 2024 19:20
Another Maybe and Either implementation in Typescript without making the value constructors (Just, Nothing, Left and Right) types themeselves as in the previous attempt
// Either monad using discriminated unions
export type Either<L, R> =
| { type: "left"; value: L }
| { type: "right"; value: R };
export function Left<L, R>(a: L): Either<L, R> {
return {
type: "left",
value: a
};
@zeusdeux
zeusdeux / ES5vsES6.js
Last active September 6, 2023 15:03
ES5 inheritance vs ES6 inheritance
/* ES6/ES2015 classes */
class A {
constructor() {
this.a = 10
}
print() {
console.log(this.a, this.b)
}
}
@zeusdeux
zeusdeux / Flamegraph_osx.md
Last active June 15, 2023 20:33
Node.js flamegraphs on osx using instruments.app, node and http://thlorenz.github.io/flamegraph/web/

Flamegraphs for your node processes on OS X

This document will help you generate flamegraphs for your node processes on OS X.

You can read about the various types of flamegraphs and how they are useful
in Brendan Gregg's wonderful write up here.

By the end of this document, you should have a flamegraph for you node app to play with.

@zeusdeux
zeusdeux / have_i_been_pwned.sh
Last active June 7, 2023 08:08
Check if any passwords have been compromised using HIBP's password API (https://haveibeenpwned.com/API/v2#PwnedPasswords). Your password never leaves your local system!
#!/usr/bin/env bash
# enable when debugging
# set -o errexit
# set -o errtrace
# set -o xtrace
# set -o nounset
# set -o pipefail
plsno () {
@zeusdeux
zeusdeux / click-outside.ts
Last active May 26, 2023 15:07
Non-💩 and dirt simple click-outside directive for Vue 3
import type { Directive } from "vue";
type ClickOutsideHandler = ($event: MouseEvent | TouchEvent) => void;
type ClickOutsideHandlerMap = Map<HTMLElement, ClickOutsideHandler> & {
initialized?: boolean;
};
const clickOutsideHandlersSymbol: symbol = Symbol.for("v-click-outside-handlers");
// @ts-ignore
@zeusdeux
zeusdeux / fail-on-select-console-calls.ts
Last active May 24, 2023 17:05
Cleaner console for jest test runs + fail on certain console messages
/**
* Fail jest test run if certain messages are logged to console (e.g., `[vuex warn] unknown getter: ....`)
* Conditionally (on CI or not) add this file and the file below to `setupFilesAfterEnv` in jest config.
*
* Example config:
*
* import type { Config } from "@jest/types"
*
* const config: Config.InitialOptions = {
* ...
@zeusdeux
zeusdeux / timed.ts
Created April 21, 2023 20:15
A function that runs async functions and prints how long they take to stderr with a given string as the prefix message
export async function timed<T>(
cb: () => Promise<T>,
message: string,
debug = (...args: any[]) => console.warn(...args),
): ReturnType<typeof cb> {
const start = performance.now();
const result = await cb();
const end = performance.now();
let timeSpent = end - start;
@zeusdeux
zeusdeux / filter-utility.ts
Last active October 17, 2022 15:23
Filtering keys from an object type based on type of value for the keys
type MixedObject = {
someFn(x: number): string
someProp: symbol
}
// using key remapping that was added in TS 4.1 wherein
// if we return `never` as a value for key in the remapping logic
// the key is dropped from the resulting type
// More: https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#key-remapping-via-as
type Filter<InputType, AllowedValueTypes> = {
@zeusdeux
zeusdeux / doSet.js
Last active May 29, 2022 22:43
Persistent set operation
function assert(cond, msg) {
if (!cond) throw new Error(msg)
}
function doSet(source, pathToSet, newValue) {
assert(Array.isArray(pathToSet), 'path being set needs to be given as an array of prop names in order of access')
const prop = pathToSet.shift()
if (typeof prop === 'undefined') {
@zeusdeux
zeusdeux / recursiveProxy.js
Created May 29, 2022 00:09
Build a recursive proxy for any provided object
function createProxyHandler() {
const handler = {
get(target, prop, receiver) {
console.log('get>', target, prop, receiver)
if (prop === '_isProxy') {
return true
}
const value = Reflect.get(target, prop, receiver)