Skip to content

Instantly share code, notes, and snippets.

View adeleke5140's full-sized avatar
💭
crafting software

Kehinde adeleke5140

💭
crafting software
View GitHub Profile
@adeleke5140
adeleke5140 / debounce.js
Last active April 27, 2023 00:16
A higher order function that returns a debounced func
function debounce(func, wait){
let timer;
let debouncedFunc = function(...args){
clearTimeout(timer)
timer = setTimeout(() => {
func.apply(this, args)
}, wait)
}
@adeleke5140
adeleke5140 / addProperty.ts
Last active April 17, 2023 19:09
Conditionally addCode in a Typesafe way.
//conditionally add properties in a typesafe way
declare let hasMiddle: boolean
const fullName = { first: 'Kehinde', last: 'Adeleke'}
//the issue
const developer2 = {...fullName}
//if you tried adding a new key,
//typescript would scream that middle does not exist on type { first: string, last: string}
@adeleke5140
adeleke5140 / readme.md
Created April 17, 2023 15:20
Explicit tag to type narrow

Type Narrowing

A way to help typescript narrow your types is by explicitly adding a tag to them. This helps in narrowing the type when necessary.

const _cache: {[url: string]:string} = {}
async function fetchWithCache(url:string){
if(url in cache){
return _cache[url]
}
const response = await fetch(url)
const text = response.text()
_cache[url] = text
@adeleke5140
adeleke5140 / backdrop.css
Created March 28, 2023 16:48
Styles for creating a backdrop element that fills the screen
.backdrop {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: #000000e1;
display: flex;
align-items: center;
justify-content: center;
@adeleke5140
adeleke5140 / form.js
Created March 23, 2023 20:58
use Form with JS disabled
<form
action="/api/search"
method="post"
onSubmit={event => {
event.preventDefault();
fetch(
'/api/search',
{
method: 'POST',
@adeleke5140
adeleke5140 / App.js
Last active March 18, 2023 18:02
Build a modular and extensible Grid with ReactJS
import Grid from './Grid'
function App(){
return (
<Grid numRows={2} numCols={4}/>
)
}
export default App
@adeleke5140
adeleke5140 / useLocalStorage.ts
Created February 7, 2023 13:27
a hook to useLocalStorage in NextJS
import { useState, useEffect } from "react";
const useLocalStorage = (key: string, initialValue: any) => {
const isServer = typeof window === "undefined";
const [value, setValue] = useState(() => {
if (isServer) {
return initialValue;
}
@adeleke5140
adeleke5140 / cons.js
Created January 27, 2023 23:22 — forked from abiodun0/cons.js
For next blog post con cdr car
const cons = (x, y) => (m) => m(x, y)
const car = (z) => z((p, q) => p)
const cdr = (z) => z((p, q) => q)
const someLinkedList = cons(1, cons(2, cons(3 , null)))
// iterating
@adeleke5140
adeleke5140 / custom-plugin.tsx
Created January 6, 2023 13:56
My version of unpkgPathPlugin
import * as esbuild from 'esbuild-wasm'
import axios from 'axios'
export const unpkgPathPlugin = () => {
return {
name: 'unpkg-path-plugin',
setup(build: esbuild.PluginBuild) {
build.onResolve({ filter: /.*/ }, async (args) => {
console.log('onResolve', args)
if(args.path === 'index.js'){