Skip to content

Instantly share code, notes, and snippets.

View kazuma1989's full-sized avatar
🦐

Kazuma Ebina kazuma1989

🦐
View GitHub Profile
@kazuma1989
kazuma1989 / Path.ts
Created November 24, 2020 02:16
補完の効くSPA内遷移先一覧を定義する方法
const paths = [
'/settings/:id/:tab',
'/profile/:tab(foo)',
'/profile/',
'/users/:userId',
'/',
] as const;
/**
* SPA 内遷移のパス一覧
@kazuma1989
kazuma1989 / brand-pattern.ts
Last active April 2, 2020 08:47
Mixin example
// 型に別名を付けても
type LooseID = string
// 元の型のまま代入できてしまう
const id1: LooseID = 'wrong value'
// こうすると
type StrictID = string & { readonly brand: unique symbol }
// 防げる
@kazuma1989
kazuma1989 / .prettierrc
Last active April 5, 2020 09:13
よく使うprettierrc, tsconfig
{
"endOfLine": "lf",
"semi": false,
"singleQuote": true,
"trailingComma": "all"
}
@kazuma1989
kazuma1989 / App.tsx
Last active March 11, 2020 06:20
MobX と hooks でプレーンな書き味の React コンポーネントを書く (https://qiita.com/kazuma1989/items/16f68cf835031b03fb61)
import React, { useEffect } from 'react'
import { Section, Title, Loading, Todo } from './components'
import useTodosStore from './useTodosStore'
export default function App() {
// このファイルにべた書きされた store インスタンスではなく、コンテキスト経由の store を使うことで、
// テスト時や Storybook を使うときにモックしやすくなる。
const [todos, loading, toggle, fetchTodos] = useTodosStore(store => [
store.todos,
store.loading,
@kazuma1989
kazuma1989 / nonNull.ts
Created February 27, 2020 02:49
Non-null filter
/**
* 値が null または undefined のときは false, それ以外の値のときは true を返す。
* Array.prototype.filter と組み合わせて null を除去するのに使える。
*
* @param value null チェックしたい値
* @example
* [0, null, {}].filter(nonNull) // [0, {}]
*/
export function nonNull<T>(value: T | null | undefined): value is T {
return value !== null && value !== undefined
@kazuma1989
kazuma1989 / throttle-trailing.ts
Last active February 11, 2020 06:35
Simple throttle
export function throttle<A extends unknown[]>(
func: (...args: A) => unknown,
wait = 0,
): (...args: A) => void {
// Fire the first call immediately
let prevTime = Date.now() - wait
// Fire trailing calls after wait
let trailingCall: ReturnType<typeof setTimeout>
return function throttled(...args: A) {
@kazuma1989
kazuma1989 / cli.js
Created November 17, 2019 04:42
Scaffold
#!/usr/bin/env node
const tar = require("tar");
async function scaffold(file) {
await tar.extract({
strip: 1,
file
});
@kazuma1989
kazuma1989 / cli.js
Last active November 17, 2019 04:40
hello-npm-gist
#!/usr/bin/env node
console.log("Hello from Gist!");
@kazuma1989
kazuma1989 / setup-cloud9.sh
Created February 28, 2018 13:05
Set up Cloud9 for Headless Chrome
# Node.js Sample for AWS Cloud9 - AWS Cloud9
# https://docs.aws.amazon.com/cloud9/latest/user-guide/sample-nodejs.html#sample-nodejs-install
nvm install node
nvm alias default v9.5.0
# Installation | Yarn
# https://yarnpkg.com/en/docs/install#linux-tab
# CentOS / Fedora / RHEL
sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
sudo yum install -y yarn
@kazuma1989
kazuma1989 / package.json
Last active January 22, 2018 23:11
Write HTML with Handlebars and dev-server watching files
{
"name": "raw-hbs",
"version": "1.0.0",
"main": "index.js",
"author": "kazuma1989 <funifuni.1204@gmail.com>",
"license": "MIT",
"scripts": {
"start": "webpack-dev-server",
"build": "cross-env NODE_ENV=production webpack"
},