Skip to content

Instantly share code, notes, and snippets.

@mike-pete
mike-pete / tagsAndProxies.js
Last active February 20, 2024 17:52
Aaaah!
// higher-order function that returns a tag function (closure) that replaces variables in a template literal with the value of "word"
const wordReplacer = (word) => (strings, ...values) => {
return strings.join(word)
}
// proxy handler that will intercept "get" and return a tag function
const proxyHandler = {
get(_, prop) {
return wordReplacer(prop)
},
@mike-pete
mike-pete / pubSub.ts
Last active January 26, 2024 05:12
Pub/Sub
const pubSub = <UpdateType>() => {
type Subscriber = ({ update, unsubscribe }: { update: UpdateType; unsubscribe: () => void }) => void
const subscribers = new Set<Subscriber>()
const subscribe = (sub: Subscriber) => subscribers.add(sub)
const publish = (update: UpdateType) => {
subscribers.forEach((sub) => {
const unsubscribe = () => subscribers.delete(sub)
sub({ update, unsubscribe })
@mike-pete
mike-pete / createExposedPromise.ts
Last active January 29, 2024 00:18
Create Exposed Promise
const createExposedPromise = <T>() => {
let resolve: (value: T | PromiseLike<T>) => void
let reject: (reason: any) => void
const promise = new Promise<T>((res, rej) => {
resolve = res
reject = rej
})
return {
@mike-pete
mike-pete / Dialog.jsx
Created September 14, 2023 18:04
React Modal using Dialog
import { memo, useEffect, useRef } from "react"
const Dialog: React.FC<{ open: boolean, className?:string, children:React.ReactElement }> = ({ open, className, children }) => {
const dialogRef = useRef<HTMLDialogElement>(null)
useEffect(() => {
if (open) {
if(dialogRef.current?.open){
dialogRef.current?.close()
}
const useRecords = () => {
const [record, setRecord] = useState<Record<string, string>>({
'record 1': 'foo',
'record 2': 'foo',
'record 3': 'bar',
})
return {
get uniqueRecords() {
const uniqueRecords = new Set(Object.values(record))
@mike-pete
mike-pete / 0patterns.md
Last active February 28, 2023 23:54
Design Patterns

Terms

Composition

  • Delegate work to helper objects.
  • Use objects from different concrete classes to get different implementations for helper methods.
  • strategy, state, etc.

Aggregation

  • (from refactoring.guru) object A contains objects B; B can live without A.
@mike-pete
mike-pete / recursion.py
Created July 27, 2022 15:47
example of recursion
def addAllNumbersFromZeroToN(n):
if n > 0:
return n + addAllNumbersFromZeroToN(n-1)
return n
print(addAllNumbersFromZeroToN(3)) #6
print(addAllNumbersFromZeroToN(2)) #3
print(addAllNumbersFromZeroToN(5)) #15
const x = {
a:{
b:{
c:'neat'
}
}
}
const loop = (object, path) => {
let index = 0
class Solution:
def intToRoman(self, num: int) -> str:
numerals = {
1: 'I',
4: 'IV',
5: 'V',
9: 'IX',
10: 'X',
40: 'XL',
50: 'L',
class Solution:
def romanToInt(self, s: str) -> int:
numerals = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000