Skip to content

Instantly share code, notes, and snippets.

View Shuumatsu's full-sized avatar

为世人降下祝福 Shuumatsu

View GitHub Profile
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
@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) => {
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 })
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { autobind } from 'core-decorators'
import { path, omit, forEach, compose, curry } from 'ramda'
const themeManagers = new WeakMap()
@autobind
export class ThemeManager {
module ChurchNumberals where
type ChurchNumberals a = (a -> a) -> a -> a
one :: ChurchNumberals a
one f = f
two :: ChurchNumberals a
two f = f . f
module ValidateCCNumbers where
toDigits :: Integer -> [Int]
toDigits 0 = [0]
toDigits n
| n > 0 =
if a > 0
then toDigits a ++ [fromInteger b :: Int]
else [fromInteger b :: Int]
where
module ShiftString where
import Data.Char
data CharacterNumber
= Lower Int
| Upper Int
add' :: CharacterNumber -> Int -> CharacterNumber
add' (Lower a) b = Lower (a + b)
import { propOr, identity } from 'ramda'
const createReducer = (initialState, handlers) =>
(state = initialState, action) =>
propOr(identity, action.type, handlers)(state, action)
export default createReducer
const ensureStartsWith = (start, str) => str.startsWith(start) ? str : start + str
const setSearchParam = searchStr =>
(key, value) => {
if (!value) return searchStr
const searchParams = new URLSearchParams(searchStr)
searchParams.set(key, value)
const newSearchStr =
import {
compose, map, ifElse, filter, isEmpty,
endsWith, init, identity, join
} from 'ramda'
const urljoin = compose(
join('/'),
map(ifElse(endsWith('/'), init, identity)),
filter(isEmpty)
)