Skip to content

Instantly share code, notes, and snippets.

View knjname's full-sized avatar
💭
🐙

knjname knjname

💭
🐙
View GitHub Profile
@knjname
knjname / compare.sh
Last active June 3, 2024 13:25
Compare performance between Go's gzip and Nginx gzip
filteroutput () {
grep Total:
}
echo " min mean[+/-sd] median max"
ab -n 1000 -c 10 http://localhost:8080/plain 2>&1 | filteroutput
ab -n 1000 -c 10 http://localhost:8080/gzipped 2>&1 | filteroutput
const stats = [... new Array(1000)].map(it => 0);
const makeStat = () => {
let result = 0;
for (let i = 0; i < 1000; i++) {
result += Math.random();
}
return result
}
for (let i = 0; i < 100000; i++) {
import React, { useContext } from "react";
const map = new Map<any, any>();
export const createGlobalStore = <T>(
key: any,
generateStore: (prevStore: T | undefined) => T
) => {
const store = generateStore(map.get(key));
map.set(key, store);
@knjname
knjname / useMyStore.ts
Last active February 9, 2021 16:07
A react hook for mobx-state-tree that preserves the last state when reloading
import { getSnapshot, Instance } from "mobx-state-tree";
import { createContext, useEffect, useState } from "react";
import { MyStore } from "./MyStore";
export const useMyStore = () => {
const [state, setState] = useState(() => MyStore.create());
useEffect(() => {
if ((window as any).reentrant) {
const newState = MyStore.create(getSnapshot(state));
@knjname
knjname / useDarkMode.ts
Created November 27, 2019 06:12
A react hook for DarkMode detection.
import { useEffect, useState } from "react";
const darkModeQuery = matchMedia("(prefers-color-scheme: dark)");
export const useDarkMode = () => {
const [isDark, setDark] = useState(darkModeQuery.matches);
useEffect(() => {
const listener = () => {
console.log("Dark mode changed", darkModeQuery.matches);
@knjname
knjname / generate-google-material-icon-names-for-ts.sh
Created November 13, 2019 09:55
generate-google-material-icon-names-for-ts.sh
dest=src/components/atoms/icon/IconTypes.ts
cat > $dest <<EOF
export type IconTypes =
EOF
curl 'https://fonts.google.com/metadata/icons' \
| tail -n +2 \
| jq '.icons[].name' \
| perl -e 'print " | " . join " | ", <>' \
const ldap = require("ldapjs");
const server = ldap.createServer();
server.bind("", (req, res, next) => {
console.log(`Bind attempt with ${req.dn.toString()}`);
res.end();
return next();
});
const formula = [31,28,31,30,31,30,31,31,30,31,30,31].map((e, m) => {
m++;
const otherMonths = [1,2,3,4,5,6,7,8,9,10,11,12].filter(e => e !== m);
return `${e} * (${otherMonths.map(om => `(x-${om})`).join('*')}) / (${otherMonths.map(om => `(${m}-${om})`).join('*')})`
}).join("\n + ")
// 31 * ((x-2)*(x-3)*(x-4)*(x-5)*(x-6)*(x-7)*(x-8)*(x-9)*(x-10)*(x-11)*(x-12)) / ((1-2)*(1-3)*(1-4)*(1-5)*(1-6)*(1-7)*(1-8)*(1-9)*(1-10)*(1-11)*(1-12))
// + 28 * ((x-1)*(x-3)*(x-4)*(x-5)*(x-6)*(x-7)*(x-8)*(x-9)*(x-10)*(x-11)*(x-12)) / ((2-1)*(2-3)*(2-4)*(2-5)*(2-6)*(2-7)*(2-8)*(2-9)*(2-10)*(2-11)*(2-12))
// + 31 * ((x-1)*(x-2)*(x-4)*(x-5)*(x-6)*(x-7)*(x-8)*(x-9)*(x-10)*(x-11)*(x-12)) / ((3-1)*(3-2)*(3-4)*(3-5)*(3-6)*(3-7)*(3-8)*(3-9)*(3-10)*(3-11)*(3-12))
// + 30 * ((x-1)*(x-2)*(x-3)*(x-5)*(x-6)*(x-7)*(x-8)*(x-9)*(x-10)*(x-11)*(x-12)) / ((4-1)*(4-2)*(4-3)*(4-5)*(4-6)*(4-7)*(4-8)*(4-9)*(4-10)*(4-11)*(4-12))
'''
''' A simple unit testing library.
'''
''' @usage
''' Write a test class with the following rules.
'''
''' * A test class name must be ended with `Test` suffix.
''' * A Test method name must be ended with `_Test` suffix.
'''
''' You can see some examples in MonkeyTest class also included in Ariawase.
# Before executing this command, you need to start an empty gitlab instance.
docker-compose exec gitlab /sbin/entrypoint.sh app:rake gitlab:backup:restore