Skip to content

Instantly share code, notes, and snippets.

View mmyoji's full-sized avatar
🙃

mmyoji mmyoji

🙃
View GitHub Profile
@mmyoji
mmyoji / create-state-machine.ts
Last active October 9, 2024 02:40
State machine in TypeScript
/**
* @example
* ```ts
* const POST_STATE = {
* DRAFT: 0,
* PUBLISHED: 1,
* // ...
* } as const;
* type PostState = (typeof POST_STATE)[keyof typeof POST_STATE];
*
@mmyoji
mmyoji / express-5-upgrade-guide.md
Last active October 16, 2024 01:03
[WIP] express v5 upgrade guide

express.js official migration guide is not sufficient yet.

Regexp in route path cannot be used

  • Check manually in your RequestHandler
  • Be careful declaration order
// users.router.ts
import { Router } from "express";
@mmyoji
mmyoji / pagination.test.ts
Created May 10, 2024 08:32
Get page objects to implement pagination
import { assertEquals } from "jsr:@std/assert";
import { paginate } from "./pagination.ts";
Deno.test(function paginateTest() {
assertEquals(paginate(1, 1), [1]);
assertEquals(paginate(1, 2), [1, 2]);
assertEquals(paginate(2, 2), [1, 2]);
@mmyoji
mmyoji / multiline-vars-in-bash.sh
Created January 31, 2024 02:29
Multiline variables in bash
#!/bin/bash
function print_multiline() {
local text=$(cat <<'EOF'
```
# Markdown Text
- one
- two
```
@mmyoji
mmyoji / 00_timestamps-on-sequelize-v5.md
Last active December 15, 2023 01:06
timestamps behaviour on Sequelize v5

timestamps: boolean behaviours on Sequelize (v5)

const { Sequelize, DataTypes, literal } = require("sequelize");

const sequelize = new Sequelize({/* ... */});
@mmyoji
mmyoji / range.ts
Created December 11, 2023 09:46
`range()` function for TypeScript
/**
* ref: https://www.freecodecamp.org/news/javascript-range-create-an-array-of-numbers-with-the-from-method/
*
* # Example
*
* ```ts
* import { range } from "./main.ts";
*
* range(1, 5);
* // [1, 2, 3, 4, 5]
@mmyoji
mmyoji / lcm-and-gcd.ts
Last active December 8, 2023 09:16
LCM & GCD in TypeScript
/**
* LCM ... Least Common Multiple (最小公倍数)
* GCD ... Greatest Common Divisor (最大公約数)
*
* LCM = (A * B) / GCD
*/
function _findGCD(a: number, b: number): number {
if (a === 0) return b;
if (b === 0) return a;
@mmyoji
mmyoji / deno-queues-examples.ts
Last active September 28, 2023 00:29
Code examples of Deno Queues
// https://deno.com/blog/queues
function listenQueue<T>(fn: (msg: T) => Promise<void>) {
//
}
// lib/kv/queue/_core.ts
const messageTypes = {
foo: "foo",
@mmyoji
mmyoji / modify-date.ts
Last active August 25, 2023 10:06
[Node.js, Deno] Modify Date function when you can't use Temporal
// see https://deno.land/std/datetime/constants.ts?source
const MILLISECOND = 1;
const SECOND = MILLISECOND * 1e3;
const MINUTE = SECOND * 60;
const HOUR = MINUTE * 60;
const DAY = HOUR * 24;
const WEEK = DAY * 7;
type Unit =
| "milliseconds"
@mmyoji
mmyoji / install-docker-in-wsl2.md
Last active July 25, 2023 11:56
Install Docker directly on WSL2 (Pengwin)

Steps

[Optional] Uninstall Docker Desktop

  1. Start Docker Desktop
  2. Remove all docker resources (e.g., docker container prune)
  3. Stop Docker Desktop
  4. rm -rf ~/.docker

Install Docker in WSL2