Skip to content

Instantly share code, notes, and snippets.

View hmmhmmhm's full-sized avatar
๐Ÿงฏ
BURNING!!

<hmmhmmhm/> hmmhmmhm

๐Ÿงฏ
BURNING!!
View GitHub Profile
@hmmhmmhm
hmmhmmhm / modulo.ts
Created December 15, 2021 06:34
Modulo in javascript (and typescript).
// ts
const mod = (n: number, m: number) => {
let r = n % m;
return Math.floor(r >= 0 ? r : r + m);
};
// js
const mod = (n, m) => {
let r = n % m;
return Math.floor(r >= 0 ? r : r + m);
@hmmhmmhm
hmmhmmhm / README.md
Created November 25, 2021 10:57
CodeWatch Personal information processing policy.

Codewatch is a watch theme that does not collect or store personal information. If you have any related questions, please contact us in the email below. pm2@kakao.com

@hmmhmmhm
hmmhmmhm / download.js
Last active October 16, 2021 21:19
virtual file download
const url = `/component.js`;
const a = document.createElement("a");
a.href = url;
a.download = url.split("/").pop()!;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
@hmmhmmhm
hmmhmmhm / packing.ts
Last active November 1, 2021 07:21
typescript grid item sort & align algorithm
export interface PackNode {
w: number
h: number
x?: number
y?: number
used?: boolean
right?: PackNode
down?: PackNode
fit?: PackNode
}
@hmmhmmhm
hmmhmmhm / fileInput.ts
Last active October 12, 2021 09:01
Virtual File Input
type FileCallbackType = (
event:
| {
file: File
thumbnail: string | ArrayBuffer | null
getFormData: (name: string) => FormData
}[]
| void
) => unknown
@hmmhmmhm
hmmhmmhm / buffer.ts
Created October 12, 2021 04:40
typescript ab2str & str2ab
/**
* Converts an ArrayBuffer to a String.
*
* @param buffer - Buffer to convert.
* @returns String.
*/
export const arrayBufferToString = (buffer: ArrayBuffer) =>
String.fromCharCode.apply(null, Array.from(new Uint16Array(buffer)))
/**
import Router from 'next/router'
import { useEffect } from 'react'
export default function StaticRoute() {
useEffect(() => {
Router.replace(window.location.pathname, window.location.pathname, {
shallow: true
})
}, [])
return null
@hmmhmmhm
hmmhmmhm / foundExample.ts
Created September 17, 2021 02:50
Check Map For Some
const list = [{ text: "a" }, { text: "b" }, { text: "c" }, { text: "d" }];
// Map & Filter
const i1 = list
.map((item, index) => (item.text === "c" ? index : null))
.filter((item) => item !== null)[0]!;
// For
let i2 = 0;
for (const item of list) {
@hmmhmmhm
hmmhmmhm / rand.ts
Created September 13, 2021 08:01
A method of generating random values that are not duplicated...
// if you want better performance, use fpe
// https://gist.github.com/hmmhmmhm/89e200e3501b4d155901baee3cae0f74
const rand = (max) => ~~(Math.random() * max)
const items = []
while(items.length === 100){
const item = rand(100)
if(!items.includes(item))
items.push(item)
@hmmhmmhm
hmmhmmhm / random.ts
Created August 30, 2021 09:59
How to create a random number that can be scoped briefly
// ~~(min + Math.random() * max)
// 0 ~ 100
~~(Math.random() * 100)
// 1 ~ 5
~~(1 + Math.random() * 5)