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 / build_zdx.sh
Created June 16, 2024 01:25
Build static universal raylib as libraylib.a on macos (libraylib.a)
#! /bin/sh
set -e
function build_raylib_macos {
curr_dir="$(basename $(pwd))"
if [[ "$curr_dir" != "src" ]];
then
@zeusdeux
zeusdeux / hello.s
Last active May 6, 2024 01:13
M1 macOS ARM64 assembly (Darwin Kernel syscalls) — Hello World
.global _start
.align 2
.text
;;; COMPILE and RUN CMD:
;;; as -o hello.o hello.s && ld -macos_version_min 14.0.0 -o hello.bin hello.o -e _start -arch arm64 && ./hello.bin
;;;
;;; EXTRACT FLAT (PURE) BINARY:
;;; 1. otool -l hello.bin and search for sectname __text > offset field (it's in decimal not hex btw)
;;; 1a. Take the offset, convert to hex and verify code starts there in the hexdump view of the compiled binary
@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)
@zeusdeux
zeusdeux / workingHours.ts
Created April 7, 2020 14:32
Intense validations using yupjs
const workingHoursSchema = yup
.object()
.shape(
{
startHour: yup.number().when(["startMinute", "endHour", "endMinute"], {
is: (...args: string[]) => args.reduce((acc, v) => !!v || acc, false as boolean), // really typescript :|
then: yup
.number()
.integer()
.min(0)
@zeusdeux
zeusdeux / hasSequence.js
Created March 16, 2020 18:29
Find if a sequence represented by an array of items in some order is in a dataset that is presented as an array as well
function assertArray(v) {
if (!Array.isArray(v)) {
throw new TypeError(`Expected ${v} to be an array`)
}
return true
}
function hasSequence(data, seq) {
assertArray(data)
assertArray(seq)