Skip to content

Instantly share code, notes, and snippets.

View adeleke5140's full-sized avatar
💭
crafting software

Kenny adeleke5140

💭
crafting software
View GitHub Profile
@adeleke5140
adeleke5140 / index.ts
Created August 28, 2023 05:38
interesting type
interface BannerProps {
type: "base" | "success" | "error";
children: React.ReactNode;
}
{
[key in BannerProps["type"]]: string;
}
@adeleke5140
adeleke5140 / buildIcons.ts
Created August 27, 2023 12:24
Generate svg sprites
import { promises as fs } from "node:fs";
import * as path from "node:path";
import { glob } from "glob";
import { parse } from "node-html-parser";
async function buildIcons() {
const cwd = process.cwd();
const inputDir = path.join(cwd, "svgs");
const inputDirRelative = path.relative(cwd, inputDir);
const outputDir = path.join(cwd, "src", "component", "icons");
@adeleke5140
adeleke5140 / index.tsx
Created August 11, 2023 17:40
Copy to clipboard
import { CopyToClipboard } from "react-copy-to-clipboard";
import { useEffect, useState } from "react";
const index = () => {
const [copiedStatus, setCopiedStatus] = useState(false);
useEffect(() => {
let timeout: number;
if (copiedStatus) {
@adeleke5140
adeleke5140 / Readme.md
Last active July 25, 2023 20:09
2fa-input-react

2fa with React

This is an attempt at creating a set of input element that accept 2fa code.

There is a bug where pressing the backspace key messes with the focused input which I don't have the time to fix right now.

Mine is written in TS but this is the original version in JS: Original

@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 / compose.js
Created April 26, 2023 17:05
function composition
const compose = (...fns) => x => fns.reduceRight((y, f) => f(y), vx)
@adeleke5140
adeleke5140 / flatten.js
Last active April 26, 2023 14:03
Flatten a multidimensional array
function flatten(value){
//do this immutably without modifying the initial arr
value.reduce((acc, cur) => acc.concat(Array.isArray(curr) ? flatten(curr): curr), [])
}
//iteration
function flatten(value){
const res = []
@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