Skip to content

Instantly share code, notes, and snippets.

@hanayashiki
hanayashiki / Setup eslint.md
Created August 9, 2022 12:58
Setup eslint.md
pnpm install -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-prettier prettier eslint-plugin-import

.eslintrc.cjs

module.exports = {
  ignorePatterns: ['**/vendors/**'],
  plugins: ['import', '@typescript-eslint', 'prettier'],
  parser: '@typescript-eslint/parser',
@hanayashiki
hanayashiki / benchmarks.mjs
Created November 16, 2021 04:27
module resolution benchmarks
import { build } from 'esbuild'
import typescript from '@rollup/plugin-typescript'
export async function esbuildResolve(id, dir) {
let result
await build({
stdin: {
contents: `import ${JSON.stringify(id)}`,
@hanayashiki
hanayashiki / ios-screen-size.json
Last active January 20, 2023 16:35
A dictionary of different iPhone screen sizes (until iPhone 12)
{
"iPhone 6": {
"height": 667,
"width": 375
},
"iPhone 7": {
"height": 667,
"width": 375
},
"iPhone 8": {
@hanayashiki
hanayashiki / podforceupdate.sh
Created July 10, 2020 11:19 — forked from mbinna/podforceupdate.sh
Clear CocoaPods cache, re-download and re-install all pods
#!/usr/bin/env bash
rm -rf "${HOME}/Library/Caches/CocoaPods"
rm -rf "`pwd`/Pods/"
pod update
@hanayashiki
hanayashiki / EventEmitter.swift
Last active July 9, 2020 21:37 — forked from brennanMKE/EventEmitter.swift
React Native Event Emitter for RCTEventEmitter in Objective-C and Swift
class EventEmitter {
/// Shared Instance.
public static var sharedInstance = EventEmitter()
// ReactNativeEventEmitter is instantiated by React Native with the bridge.
private static var eventEmitter: ReactNativeEventEmitter!
private init() {}
@hanayashiki
hanayashiki / sliding_window.py
Created December 28, 2019 07:39
2d sliding window implementation for numpy
import numpy as np
from numpy.lib.stride_tricks import as_strided
from typing import *
def sliding_window_2d(a: np.array, wnd_size: Tuple[int, int]):
# a: an nd-array of (height, width, ...rest)
# wnd_size: a tuple of (wnd_width, wnd_height)
# returns an nd-array of (height - wnd_height + 1, width - wnd_width + 1, wnd_height, wnd_width, ...rest)
height, width, *rest = a.shape
s0, s1, *rest_strides = a.strides
class CallbackSequence extends Function {
constructor() {
super();
this.callbacks = {};
return new Proxy(this, {
apply: (target, thisArg, argumentsList) => {
this.__call__();
}
});
}
@hanayashiki
hanayashiki / patch-arrayBuffer.js
Created December 16, 2019 04:08
Safari 13.0.4: Blob.arrayBuffer is not a function
(function () {
File.prototype.arrayBuffer = File.prototype.arrayBuffer || myArrayBuffer;
Blob.prototype.arrayBuffer = Blob.prototype.arrayBuffer || myArrayBuffer;
function myArrayBuffer() {
// this: File or Blob
return new Promise((resolve) => {
let fr = new FileReader();
fr.onload = () => {
resolve(fr.result);
import socket
def extract_request_line(request):
first_line = request.split("\r\n")[0]
method, resource, ver = first_line.split(" ")
return method, resource, ver
def respond(method, resource):
if method == "GET" and resource == "/A":
return b"<h1>Resource A from my server!</h1>", 200
@hanayashiki
hanayashiki / json_visualizer.py
Created July 18, 2019 04:10
Truncate long json objects so that it is human readable
import sys
import json
max_item = 3
def visualize(obj: dict):
if isinstance(obj, list):
if len(obj) > max_item:
return [visualize(o) for o in obj[0:max_item] + ["(%d items in total)" % len(obj)]]
else: