Skip to content

Instantly share code, notes, and snippets.

View shqld's full-sized avatar
🦭
shqld

shqld

🦭
shqld
View GitHub Profile
@shqld
shqld / dict-symbol.js
Created December 13, 2017 13:18
Abstract dictionaries in ES2015 with immutability and method chaining
const repository = Symbol('repository')
class Dict {
constructor(props) {
if (typeof props !== 'object') {
throw new Error(
`Props must be an object, you passed ${typeof props} ${props}.`
)
}
Object.freeze(props)
@shqld
shqld / withDynamicAttributes.jsx
Last active August 28, 2018 01:14
Setting element attributes dynamically
/* withAttr.jsx */
import React from 'react'
const withAttr = (Component) => ({attrs, ...props}) => <Component {...attrs} {...props} />
export default withAttr
/* example.jsx */
import React from 'react'
@shqld
shqld / workbox.d.ts
Last active December 23, 2018 03:18
Typings for workbox (most part is copied from @types/workbox-sw)
// Type definitions for workbox-sw 3.2
// Project: https://github.com/GoogleChrome/workbox
// Definitions by: Frederik Wessberg <https://github.com/wessberg>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.6
/**
* ===== BroadcastCacheUpdate =====
*/
@shqld
shqld / my-lazyloader.html
Created April 27, 2019 06:58
A toy LazyLoader built with WebComponents/CustomElement
<style>
.container {
display: flex;
flex-direction: row;
height: 1000px;
}
.item {
flex: 1;
}
</style>
@shqld
shqld / useStore.tsx
Last active May 5, 2019 13:44
`useStore` hook for reading global state without binding actions
import { useState, useEffect } from 'preact/hooks'
// or
import { useState, useEffect } from 'react'
import { Store } from 'unistore'
// or
import { Store } from 'redux'
const extract = (state: any, key: any) => {
if (!key) return state
@shqld
shqld / colored_console.ts
Last active August 19, 2019 07:29
Colored console with chalk
import chalk from 'chalk';
export const initChalk = ({ error = chalk.redBright, warn = chalk.yellowBright } = {}) => {
// disable coloring e.g. when piped like `2> err.log`
if (typeof process !== 'undefined' && !process.stderr.isTTY) {
return;
}
if (error) {
const defaultError = console.error.bind(console);
@shqld
shqld / Terser & Babel の好きなところ.md
Last active December 20, 2023 15:27
個人的にTerser & Babelの好きなところ

だれ?

(credit: @tomo_e さん)

  • 新卒 2 年目
  • ウェブチーム
@shqld
shqld / get-v8-optimization-status.js
Created September 23, 2019 16:13
Get V8 optimization status
// https://github.com/v8/v8/blob/master/test/mjsunit/mjsunit.js#L162-L178
const statuses = {
IsFunction: 1 << 0,
NeverOptimize: 1 << 1,
AlwaysOptimize: 1 << 2,
MaybeDeopted: 1 << 3,
Optimized: 1 << 4,
TurboFanned: 1 << 5,
Interpreted: 1 << 6,
@shqld
shqld / async-operations-server.js
Last active November 29, 2019 19:18
Async handling for sync operations on server-side, leveraging EventLoop
const express = require('express')
const sleep = (ms) => new Promise((res, rej) => setTimeout(res, ms))
const sleepSync = (ms) => {
const start = new Date()
while (new Date() - start < ms) {}
}
const server = express()
@shqld
shqld / friendly-inifinite-loop.js
Created April 1, 2020 00:55
A bit friendly infinite loop
const run = () => console.log("loop") || requestIdleCallback(run)
run()