Skip to content

Instantly share code, notes, and snippets.

View akhrszk's full-sized avatar
🍣
🍶

Akihiro Suzuki akhrszk

🍣
🍶
View GitHub Profile
@akhrszk
akhrszk / useInterval.ts
Last active March 31, 2023 04:37
React Hooks for using setInterval
View useInterval.ts
import { useEffect, useRef, useState } from 'react';
type Control = {
start: () => void;
stop: () => void;
};
type State = 'RUNNING' | 'STOPPED';
type Fn = () => void;
@akhrszk
akhrszk / BinarySearchTree.js
Last active December 11, 2021 12:53
Binary Search Tree in JavaScript
View BinarySearchTree.js
class Node {
constructor(value) {
this.value = value
this.left = undefined
this.right = undefined
}
}
Node.prototype.setLeft = function (node) {
this.left = node
@akhrszk
akhrszk / toMap.ts
Last active December 11, 2021 12:31
convert Array to Map by key
View toMap.ts
const toMap = <T>(arr: T[], byId: (item: T) => string): Map<string, T> => {
const tuple = arr.reduce((acc, curr) => {
acc.push([byId(curr), curr])
return acc
}, [] as [string, T][])
return new Map(tuple)
}
@akhrszk
akhrszk / group.ts
Created December 10, 2021 15:03
splits an array into groups
View group.ts
const group = <T>(arr: T[], size: number): T[][] => {
return arr.reduce(
(acc, curr) => {
if (acc[acc.length - 1].length < size) {
acc[acc.length - 1].push(curr)
} else {
acc.push([curr])
}
return acc
},
@akhrszk
akhrszk / balus.sh
Created January 10, 2021 11:27
バルス
View balus.sh
docker-compose down --rmi all --volumes --remove-orphans
@akhrszk
akhrszk / groupBy.ts
Created November 6, 2020 19:10
Generates array of objects grouped by key.
View groupBy.ts
const groupBy = <K, V>(arr: readonly V[], getKey: (cur: V, idx: number, src: readonly V[]) => K): V[][] =>
arr.reduce((acc, cur, idx, src) => {
const key = getKey(cur, idx, src);
const item = acc.find(([k,]) => k === key);
if (item) {
const [, v] = item;
v.push(cur);
} else {
acc.push([key, [cur]]);
}
@akhrszk
akhrszk / timestamp.go
Created October 21, 2020 11:50
unix timestamp in Golang
View timestamp.go
// marshal/unmarshal to/from unix timestamp
// implement the Scanner and Valuer interfaces of gorm (https://gorm.io/docs/data_types.html)
package custom
import (
"time"
"fmt"
"strconv"
"database/sql/driver"