Skip to content

Instantly share code, notes, and snippets.

View lancetw's full-sized avatar
🐈

Hsin-lin Cheng lancetw

🐈
View GitHub Profile
let actions = []
for (let i = 0; i < 10000; i++) {
actions.push(() => Promise.resolve(i))
}
const process = (task) => task.reduce((promised, task) => promised.then(acc => task().then(value => [...acc, value])), Promise.resolve([]))
process(actions)
export const submit = (values, dispatch /*, props */) => {
setSubmitting('loginForm', true)
return login(values)
.then(action => {
dispatch(action)
setSubmitting('loginForm', false)
})
.catch(error => {
dispatch(error)
throw new SubmissionError(error.payload)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pygments import highlight
from pygments.lexers import JsonLexer
from pygments.formatters import TerminalFormatter
from pprint import pformat
import json
def pprint_color(obj, indent=2):
@lancetw
lancetw / filterByProps.js
Created March 1, 2018 18:05
filterByProps
export function filterByProps(obj: any, props: string[]): any {
if (!props) return obj
let ret = {}
Object.keys(obj).forEach(key => {
if (props.some(prop => prop === key)) {
ret[key] = clone(obj[key])
}
})
return ret
}
@lancetw
lancetw / form2obj.ts
Last active September 26, 2018 11:57
let user = new User(filterByProps(form, [
'username',
'displayname'
]))
function filterByProps(obj: any, props: string[]): any {
if (!props) return obj
let ret = {}
Object.keys(obj).forEach(key => {
if (props.some(prop => prop === key)) {
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["dom", "es2015"],
"module": "es2015",
"moduleResolution": "node",
"sourceMap": true,
const delay = (delay) => () => new Promise((resolve, reject) => setTimeout(resolve, delay))
const main = () => f1().then(f2).catch(f3)
const f1 = () => new Promise((resolve, reject) => ret ? resolve() : reject())
const f2 = () => Promise.resolve('f2')
const f3 = () => Promise.resolve('f3')
let ret = true
const next = () => ret = !ret
const alphabetBoardPath = (target) => {
const layout = 5
const hashMap = new Map(Array.from("abcdefghijklmnopqrstuvwxyz")
.map((c, i) => [ c, {x: (i / layout) | 0, y: i % layout | 0}]))
const repeat = (s, n) => (n <= 0 ? '' : s.repeat(n))
let p0 = { x: 0, y: 0 }
const ret = Array.from(target).reduce((ret, t) => {
const p = hashMap.get(t)
const alphabetBoardPath = (target) => {
const layout = 5
const hashMap = new Map(Array.from("abcdefghijklmnopqrstuvwxyz").map((c, i) => [ c, {x: (i / layout) | 0, y: i % layout | 0}]))
const repeat = (s, n) => (n <= 0 ? '' : s.repeat(n))
let p0 = { x: 0, y: 0 }
return Array.from(target).reduce((ret, t) => {
const p = hashMap.get(t)
ret.push(p.y < p0.y ? repeat('L', p0.y - p.y) : '', p.x < p0.x ? repeat('U', p0.x - p.x) : '', p.x > p0.x ? repeat('D', p.x - p0.x) : '', p.y > p0.y ? repeat('R', p.y - p0.y) : '', '!')
p0 = p
return ret
var alphabetBoardPath=function(f){var g=new Map(Array.from("abcdefghijklmnopqrstuvwxyz").map(function(a,c){return[a,{x:c/5|0,y:c%5|0}]})),d=function(a,c){return 0>=c?"":a.repeat(c)},a={x:0,y:0};return Array.from(f).reduce(function(e,c){var b=g.get(c);e.push(b.y<a.y?d("L",a.y-b.y):"",b.x<a.x?d("U",a.x-b.x):"",b.x>a.x?d("D",b.x-a.x):"",b.y>a.y?d("R",b.y-a.y):"","!");a=b;return e},[]).join("")};