This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { OutputBundle, OutputChunk, OutputOptions, Plugin } from 'rollup'; | |
const rollupPluginUpdate = (update: (code: string) => string): Plugin => { | |
function generateBundle(_: OutputOptions, bundle: OutputBundle) { | |
for (const fileName in bundle) { | |
const entryFile = bundle[fileName] as OutputChunk; | |
if (entryFile.isEntry) { | |
entryFile.code = update(entryFile.code); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { SetStateAction, useCallback, useReducer, useRef } from 'react'; | |
// useControlValue | |
// 同时处理受控和非受控组件 | |
// based on https://github.com/alibaba/hooks/blob/master/packages/hooks/src/useControllableValue/index.ts | |
function useControlValue<V = any>(props: Record<string, any> = {}) { | |
const isControlled = 'value' in props; | |
// value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useDarkMode = (): [boolean, () => void] => { | |
const [isDark, setIsDark] = useState(false); | |
const setDark = (newDark: boolean) => { | |
document.documentElement.classList[newDark ? 'add' : 'remove']('dark'); | |
localStorage.setItem('dark', `${newDark}`); | |
setIsDark(newDark); | |
}; | |
useEffect(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { SetStateAction, useCallback, useReducer, useRef } from 'react'; | |
// useControlState | |
// 同时处理受控和非受控组件,但与 useControlValue 不同 | |
// 本 hook 用于此种情况:内部 state 与 onChange 时传出 value 不同步 (如内部 state 情况复杂,超前于 value) | |
// 此时需同步 value -> state (若 props.value 变化),同时同步 state -> value (若 state 变化且需要传出) | |
function useControlState<S = any, V = any>({ | |
props = {}, | |
valueToState, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyPromise { | |
constructor(fn) { | |
this.status = 'PENDING'; | |
const resolve = (data) => { | |
if (this.status !== 'PENDING') return; | |
this.status = 'FULLFILLED'; | |
this.value = data; | |
this.onChange?.(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Scheduler { | |
constructor(limit) { | |
this.count = 0; | |
this.limit = limit; | |
this.list = []; | |
} | |
add(fn) { | |
return new Promise((resolve) => { | |
this.list.push((...args) => fn(...args).then(resolve)); | |
this.run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useRef } from "react"; | |
const ImageGallery = (props) => { | |
const { | |
type = "lazy-load", | |
list = [], | |
width = 300, | |
height = 300, | |
size = 3, | |
emitHeight = 150, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* chooseGeo.js | |
* | |
* 小程序 wx.chooseLocation() 获取地理位置,会先请求授权, | |
* 如果用户未授权,那么再次调用,嗯 ... 就没反应了,还以为微信 API 有问题,崩溃了。 | |
* | |
* 这当然是微信为了良好的用户体验,就不让你问了, | |
* 当然,开发的体验就非常不好了,因为这个 API 干了一半的活,罢工了, | |
* 然后,当然就得自己封装一个了。 | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isObj = (val) => Object.prototype.toString.call(val) === '[object Object]'; | |
const isArr = (val) => Array.isArray(val); | |
const isNum = (val) => val === `${+val}`; | |
const strToNum = (str) => (isNum(str) ? +str : str); | |
/** | |
* Used to change undefined keys to object or array, similar effect to the 'optional chaining'. | |
* Received an object and an array of keys, returns a formatted object. | |
* | |
* e.g. |