Skip to content

Instantly share code, notes, and snippets.

View ts-type-lisp-eval-tail-call-version.ts
// 基本运算
export type NArray<T, N extends number> = N extends N ? (number extends N ? T[] : _NArray<T, N, []>) : never
type _NArray<T, N extends number, R extends unknown[]> = R['length'] extends N ? R : _NArray<T, N, [T, ...R]>
type NArrayNumber<L extends number> = NArray<number, L>
// 加法
export type Add<M extends number, N extends number> = [...NArrayNumber<M>, ...NArrayNumber<N>]['length']
// 减法
export type Subtract<M extends number, N extends number> =
View ts-type-lisp-eval.ts
// ===================== 解析器 ====================
// 分成三步,避免爆栈
type Reject = never;
type Node = Token | Node[];
type Token = string | number;
type Spaces = ' ' | '\n';
type Pairs = '(' | ')';
type Splits = Spaces | Pairs;
View traverser.ts
import { VisitorKeys } from 'estraverse';
import * as ESTree from 'estree';
type Cursor = [number, number];
function init(node: ESTree.Node): Cursor | null {
const keys: string[] | undefined = (VisitorKeys as any)[node.type];
if (!keys || keys.length <= 0) {
return null;
}
View guard.ts
function guard<T0, T1>(
v: T0,
...conditions: [(v: T0) => boolean, (V: T0) => T1][]
) {
for (const [condition, func] of conditions) {
if (condition(v)) {
return func(v);
}
}
throw new Error('Condition not match');
View modify-object.ts
type CommandFunc = (
target: any,
key: string,
value: any,
commands: Commands,
) => object;
type Commands = {
[key: string]: CommandFunc;
};
View fake-hooks.4.1.jsx
/* eslint-disable */
import React, { useMemo, useState } from "react";
function createHooksFromClass(_class) {
return function (props) {
const instance = useMemo(() => new _class(props), []);
const [state, setState] = useState(instance.state);
instance.state = state;
instance.props = props;
instance.setState = (newState) => setState(oldState => ({...oldState, ...newState}));
View cacnelable-async.js
const acorn = require("acorn");
const astring = require("astring");
const estraverse = require("estraverse");
function withCancel(func) {
const signalName = "__$isCancel__";
const shouldCancelName = "__$shouldCancel__";
function transform(node) {
return estraverse.replace(node, {
View log-demo.tsx
import React, { createContext, useContext, useEffect } from 'react';
import './App.css';
interface LogData {
[key: string]: number | string;
}
interface LogLayerData {
name?: string;
log: LogData;
}
View lark-doc-lock.user.js
// ==UserScript==
// @name lark-doc-lock
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://bytedance.feishu.cn/*
// @require https://github.com/bramblex/MonkeyBox/raw/master/monkey-box.user.js
// ==/UserScript==
View qrcode.js
/**
* @fileoverview
* - Using the 'QRCode for Javascript library'
* - Fixed dataset of 'QRCode for Javascript library' for support full-spec.
* - this library has no dependencies.
*
* @author davidshimjs
* @see <a href="http://www.d-project.com/" target="_blank">http://www.d-project.com/</a>
* @see <a href="http://jeromeetienne.github.com/jquery-qrcode/" target="_blank">http://jeromeetienne.github.com/jquery-qrcode/</a>
*/