Skip to content

Instantly share code, notes, and snippets.

View SevenOutman's full-sized avatar
🈺
再营业

Doma SevenOutman

🈺
再营业
View GitHub Profile
@SevenOutman
SevenOutman / README.md
Last active July 20, 2023 06:59
require.context polyfill
@SevenOutman
SevenOutman / handlers.ts
Created May 30, 2023 14:34
This gist demonstrates how you can manage Mock Service Worker handlers in Next.js API Routes style in your Vite project
import { rest } from "msw";
// https://vitejs.dev/guide/features.html#glob-import
const imports = import.meta.glob("./api/**/*.ts", { eager: true });
export const handlers = Object.entries(imports).reduce(
(allHandlers, [path, handlers]) => {
// map "./path/[to]/handlers.js" -> "/api/path/:to/handlers"
const apiPath =
"/" +
@SevenOutman
SevenOutman / CHEATSHEET.md
Last active March 28, 2023 13:11
My cheatsheet

Extend yup methods in TypeScript

jquense/yup#312 (comment)

Delete merged branches

To delete all local branches that are already merged into the currently checked out branch:

git branch --merged | egrep -v "(^\*|master|main|dev)" | xargs git branch -d
// Expected output:
//
// const oa = createBuilder('/oa')
// oa() /oa
// oa.meetingroom() /oa/meetingroom
// oa.meetingroom.add() /oa/meetingroom/add
//
// Yep it's a endpoint builder and can evolve to be a api caller
@SevenOutman
SevenOutman / 📊 Weekly development breakdown
Last active January 6, 2024 00:01
Weekly development breakdown
TypeScript 26 mins ████████████████████▎ 96.8%
CSS 0 secs ▌░░░░░░░░░░░░░░░░░░░░ 2.6%
JSON 0 secs ░░░░░░░░░░░░░░░░░░░░░ 0.6%
@SevenOutman
SevenOutman / useAbortableAsyncCallback.js
Created August 14, 2019 10:13
React hook for async callback that automatically aborts on component unmount.
export default function useAbortableAsyncCallback(callback, inputs) {
const abortHandleRef = useRef();
const runAbortHandle = useCallback(() => {
if (abortHandleRef.current) {
abortHandleRef.current();
}
}, []);
@SevenOutman
SevenOutman / useAsTween.js
Created June 24, 2019 03:19
React tween state hooks
// Hook for use props as a tween state
// Usage.
// function Component({ propA, ...props }) {
// const tweenA = useAsTween(propA);
// // ...
// }
//
import { useEffect } from 'react';
import useTweenState from './useTweenState';
@SevenOutman
SevenOutman / createRadarChart.js
Created May 17, 2018 03:19
D3-based Radar Chart, origins from the work by Nadieh Bremer, made d3@5 compatible
/* eslint-disable no-tabs,no-mixed-operators */
import * as d3 from 'd3';
/**
* @see http://bl.ocks.org/nbremer/21746a9668ffdf6d8242#radarChart.js
*/
function createRadarChart(selector, data, options) {
const defaultOptions = {
w: 600, // Width of the circle
h: 600, // Height of the circle
@SevenOutman
SevenOutman / Store.js
Created May 16, 2018 11:01
Use redux like React 16.3 Context API
import { connect } from 'react-redux';
export default connect(
state => ({ state }), // mapState2Props
)(
({ state, dispatch, children }) => children(state, dispatch), // <Store> component
);
const endpoints = registerEndpoints(
GET`/users`([User]),
GET`/users/:id`(User),
POST`/users`(User),
)
const users = await endpoints.getUsers.call()
const user1 = await endpoints.getUsersId.call({id: 1})