Skip to content

Instantly share code, notes, and snippets.

View liyaodong's full-sized avatar
🏠
Working from home

Yaodong Li liyaodong

🏠
Working from home
View GitHub Profile
@liyaodong
liyaodong / timeDuration.js
Created April 7, 2024 05:41
Calculate duration
"use strict";
const calculateTime = (ranges) => {
const totalTime = ranges.reduce(
(acc, x) => {
const start = x[0];
const end = x[1];
let currentTotal = 0;
// Case E, No overlap
@liyaodong
liyaodong / useLoadingIndicator.tsx
Last active February 7, 2024 06:39
useLoadingIndicator
import { useState } from "react";
type FunctionArgs<T> = T extends (...args: infer Args) => unknown
? Args
: never;
type FunctionReturn<T> = T extends (...args: unknown[]) => infer Return
? Return
: never;
@liyaodong
liyaodong / fp-sample.ts
Created December 22, 2023 04:13
fp-ts sample
import { pipe } from 'fp-ts/function';
import * as O from 'fp-ts/Option';
import * as E from 'fp-ts/Either';
import * as A from 'fp-ts/Array';
import { sequenceT } from 'fp-ts/Apply';
import { flow } from 'fp-ts/function';
import * as t from 'io-ts';
// Example of a complex validator using io-ts
const UserProfile = t.type({
@liyaodong
liyaodong / crop.sh
Created July 19, 2023 15:25
Crop Midjourney Pictures from 4x4 to single image for each item
for file in *.png; do
convert "$file" -crop "2x2@+0+0" -gravity center -repage +0+0 "output_${file%.*}_%d.jpg"
done
@liyaodong
liyaodong / app.tsx
Created July 1, 2022 08:27
auth0-react get token outside of react component
// your app.tsx
const {
getAccessTokenSilently,
loginWithRedirect,
user,
isAuthenticated,
isLoading,
} = useAuth0();
useEffect(() => {
@liyaodong
liyaodong / regionList.json
Created May 19, 2022 03:48
AWS Region list JSON, update time 2022-05-19
[
{
"name": "US East (Ohio)",
"region": "us-east-2"
},
{
"name": "US East (N. Virginia)",
"region": "us-east-1"
},
{
@liyaodong
liyaodong / chunkPromise.js
Last active June 29, 2022 15:02
chunkPromise
const _ = require("lodash");
const sequential = require("promise-sequential");
/**
* Batch execute promises in chunks
*/
const chunkPromise = (promises, chunkSize = 5) => {
const chunkedPromise = _.chunk(promises, chunkSize).map(
(promiseGroup) => () =>
Promise.all(promiseGroup.map((promiseFn) => promiseFn()))
@liyaodong
liyaodong / aws_mysql_pub.sh
Created December 8, 2021 08:07
mysql-to-cloudwatch
#!/bin/sh
max_connections=$(sh /home/bitnami/monitor/mysqlmon.sh max_connections)
max_used_connections=$(sh /home/bitnami/monitor/mysqlmon.sh max_used_connections)
threads_connected=$(sh /home/bitnami/monitor/mysqlmon.sh threads_connected)
cdate=$(date -u +%Y-%m-%dT%H:%M:00.000Z)
@liyaodong
liyaodong / sshConfigSample
Created November 18, 2021 08:01
setup ssh key per project
Host github-my-repo
# The host that has the remote Git repository
Hostname github.com
# Username for remote SSH user (For GitHub, everyone uses the name `git`)
User git
# Path to your private SSH key
IdentityFile /root/.ssh/github-my-repo.id.rsa
change repo ssh config from
@liyaodong
liyaodong / README.md
Created October 26, 2021 08:55
Auth0 with nextjs localhost https issue

The problem

Auth0 require https to view your resources safely. But usually our localhost is http rather than https. Generate a custom cert file may solve this issue but it require changes on nextjs/express level.

If you're facing issue like checks.state argument is missing during auth0 login step please continue to read.

The solution

The https reverse proxy

Caddy is an opensource tool for the local https work, not sure if it's safe enough but it's open source. https://caddyserver.com/