Skip to content

Instantly share code, notes, and snippets.

View mendes5's full-sized avatar
๐Ÿ“‰
[object Object]

mendes5

๐Ÿ“‰
[object Object]
View GitHub Profile
@mendes5
mendes5 / traits.rs
Created February 7, 2023 13:58
Traits, sub-traits and dyn Trait
use std::{rc::Rc, vec};
trait Unsafe {
// where Self: Sized allows us to hold
// values implementing this in arrays
// and pass it as function arguments as &dyn Unsafe
// or Box<dyn Unsafe> without not object-safe errors.
fn create() -> Self where Self: Sized;
}
@mendes5
mendes5 / let-him-cook.rs
Created February 2, 2023 04:04
Can reactive declarative programming in rust be easier?
#![feature(local_key_cell_methods)]
use std::{cell::RefCell, collections::HashMap};
// Core idea: Rust lets you do this:
fn create_closure(initial: u32) -> Box<dyn FnMut()> {
let mut captured = initial;
Box::new(move || {
captured += 1;
fn mount<T, P, B: FnMut(&mut P)>(
component: fn(this: Option<T>, props: P, block: B) -> T,
props: P,
block: B,
) {
component(None, props, block);
}
struct Props<T: FnMut()> {
on_click: T,
@mendes5
mendes5 / LinkedHeightContainer.js
Created September 14, 2022 18:01
A container that can have many elements but will not influence its container height, to do that, it should be linked to another element inside the same container to get its height from.
const LinkedHeightContainer = ({ children, link }) => {
const ref = useRef(null);
useEffect(() => {
const observer = new ResizeObserver((entries) => {
const entry = entries[entries.length - 1];
const height = `${entry.contentRect.height}px`;
ref.current.style.height = height;
});
@mendes5
mendes5 / s.js
Created September 9, 2022 19:36
Poor mans version of S.js
const effects = [];
let isCollectingEffects = false;
const data = (init) => {
const deps = [];
let oldValue = init;
return (value) => {
@mendes5
mendes5 / subtract.ts
Created August 10, 2022 21:35
Subtract one from a number inside a string in typescript
type SubtractOne<T> =
T extends '9' ? '8'
: T extends '8' ? '7'
: T extends '7' ? '6'
: T extends '6' ? '5'
: T extends '5' ? '4'
: T extends '4' ? '3'
: T extends '3' ? '2'
: T extends '2' ? '1'
: T extends '1' ? '0'
Math.max(NaN, 100) === NaN
@mendes5
mendes5 / debug-rerender.ts
Created June 28, 2022 22:25
Helps debugging react hooks re-render reasons
const useDebug = (deps: Record<string, any>) => {
Object.entries(deps).forEach(([key, value]) => {
useEffect(() => console.log(`${key} Changed`), [value]);
});
};
@mendes5
mendes5 / events.js
Created June 26, 2022 04:50
Pretty print a micromark events list
console.log(self.events.map(x => [x[0], x[1].type, x[2].sliceSerialize(x[1], true)]).map(x => x.join(' | ')).reduce((prev, curr) => {
if (curr.startsWith('enter')) {
prev = { i: prev.i, t: prev.t + '\n' + ' '.repeat(prev.i) + curr };
prev.i+=2
return prev
} else {
prev.i-=2
return { i: prev.i, t: prev.t + '\n' + ' '.repeat(prev.i) + curr }
}
}, { i: 0, t: '' }).t)
@mendes5
mendes5 / ocs.json
Created March 9, 2022 04:04
Deep Rock Galatic overclocks by guns by characters, from karl.gg
{
"Engineer": {
"\"Warthog\" Auto 210": [
{
"id": 25,
"character_id": 1,
"gun_id": 1,
"overclock_type": "Clean",
"overclock_index": 1,
"overclock_name": "Stunner",