Skip to content

Instantly share code, notes, and snippets.

type FnMapperR<T> = {
[K in keyof T]: (...args: any[]) => T[K]
}
type FnMapper2<T, U, R> = {
[K in keyof U]: (t: T, u: U[K]) => R
}
declare function module <S, G, A, M> (module: {
namespaced?: boolean,
@ktsn
ktsn / undo_redo.js
Last active October 14, 2016 15:07
export const ({ shouldRecord, invertAction, isUndo, isRedo }) => {
const history = []
return store => next => action => {
if (isUndo(action)) {
const invert = invertAction(lastUndoable(history))
history.push(invert)
next(invert)
return
}
@ktsn
ktsn / mouse-position.js
Created August 23, 2016 01:46
特定の要素上でマウスイベントが発生した時に、その要素におけるマウスの座標を取得する
el.addEventListener('mousemove', event => {
const rect = event.currentTarget.getBoundingClientRect()
const pos = {
left: event.clientX - rect.left,
top: event.clientY - rect.top
}
})
export function traverse<T, U>(obj: Dictionary<T>, f: (t: T) => Array<U>) : Array<Dictionary<U>> {
return sequence(
mapValues(obj, t => {
return f(t)
})
)
}
export function sequence<T>(obj: Dictionary<Array<T>>) : Array<Dictionary<T>> {
function loop<T>(acc: Array<Dictionary<T>>, obj: Dictionary<Array<T>>) : Array<Dictionary<T>> {
@ktsn
ktsn / action.ts
Last active July 15, 2016 10:35
Vuex action helper
import {
INIT_SOME_DATA,
ADD_SOME_DATA,
MOVE_SOME_DATA,
REMOVE_SOME_DATA,
UPDATE_SOME_DATA
} from '../mutation_types'
import { ISomeData } from '../model/some_data'
@ktsn
ktsn / module.js
Created July 14, 2016 10:57
[Vuex] Suppress prefix or suffix in the module definition
// use shorter name in local
const ADD = `ADD_TOAST_MESSAGE`
const REMOVE = `REMOVE_TOAST_MESSAGE`
// export as longer name with suffix
export {
ADD as ADD_TOAST_MESSAGE,
REMOVE as REMOVE_TOAST_MESSAGE
}
type Subscriber<T> = (val: T) => void
type Unsubscriber = () => void
export default class Observable<T> {
private subscribers: Subscriber<T>[] = []
private unsubscriber: Unsubscriber
private _value: T
constructor(source?: (cb: Subscriber<T>) => Unsubscriber) {
if (source) {