Skip to content

Instantly share code, notes, and snippets.

View Shuumatsu's full-sized avatar

为世人降下祝福 Shuumatsu

View GitHub Profile
export const eventName = 'in view'
export const tagName = 'inview-observer'
const io = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
const target = entry.target
target.dispatchEvent(new Event('in view'))
})
}, { threshold: 0.25 })
@Shuumatsu
Shuumatsu / async_generator.js
Created November 10, 2017 07:39
用 generator 实现 async/await
const gFoo = function* () {
const a = yield Promise.resolve(1)
const b = yield Promise.resolve(2)
const c = yield Promise.resolve(3)
const d = yield Promise.resolve(4)
return [a, b, c, d]
}
const co = genF => (...args) => {
const seenKeys = new Set()
const MULTIPLIER = Math.pow(2, 24)
const generateRandomKey = () => {
const key = Math.floor(Math.random() * MULTIPLIER).toString(32)
if (seenKeys.has(key) || !isNaN(+key))
return generateRandomKey()
seenKeys.add(key)
return key
// 常规的递归方法
const flatten1 = arr => {
const result = []
for (const v of arr) {
Array.isArray(v) ?
result.push(...flatten(v)) :
result.push(v)
}
return result
const cache = new WeakMap()
export default async fileblob => new Promise((resolve, reject) => {
if (cache.has(fileblob))
resolve(cache.get(fileblob))
const reader = new FileReader()
reader.addEventListener('load', e => {
const src = e.target.result
cache.set(fileblob, src)
const cache = new WeakMap()
export default src => new Promise((resolve, reject) => {
if (cache.has(src))
resolve(cache.get(src))
const audio = document.createElement('audio')
audio.addEventListener('loadedmetadata', e => {
const duration = audio.duration
cache.set(src, duration)
http://www.ics.uci.edu/~eppstein/161/960109.html
const cache = new Map()
export default src =>
new Promise((resolve, reject) => {
if (cache.has(src)) resolve(cache.get(src))
const audio = document.createElement('audio')
audio.addEventListener('loadedmetadata', () => {
const duration = audio.duration
export const sync = syncFn => (data, cb) => data |> syncFn |> cb;
export const compose = (taskList, cb) =>
reduceRight((next, task) => data => task(data, next), cb, taskList);
export const withErrorHandler = handler =>
map(task => (data, next) =>
data instanceof Error ? handler(data) : task(data, next)
);
import React from 'react'
import styled, { keyframes } from 'styled-components'
const Svg = styled.svg.attrs({
version: '1.1',
xmlns: 'http://www.w3.org/2000/svg',
xmlnsXlink: 'http://www.w3.org/1999/xlink',
'xmlns:sketch': 'http://www.bohemiancoding.com/sketch/ns'
})`
position: relative;