Last active: 2 years ago
Symbol.asyncIterator
class Foo {
#start = 0;
#end = 0;
constructor(end) {
this.#end = end;
}
async *[Symbol.asyncIterator]() {
while (this.#start <= this.#end) {
yield new Promise((resolve) => resolve(this.#start++));
}
}
}
const bar = new Foo(10);
for await (const i of bar) {
console.log(i);
}
Last active: 2 years ago
Get Promise return type.
// A = string
type A = Awaited<Promise<string>>;
// B = number
type B = Awaited<Promise<Promise<number>>>;
// C = boolean | number
type C = Awaited<boolean | Promise<number>>;
type APIResult = Awaited<ReturnType<typeof getUser>>
Last active: 2 years ago
Using Proxy to get query
const params = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, prop) => searchParams.get(prop),
});
// Get the value of "some_key" in eg "https://example.com/?some_key=some_value"
let value = params.some_key; // "some_value"
Last active: 2 years ago
Around with other objects
const target = new THREE.Object3D();
target.position.set(0, 10, 0);
const light = new THREE.SpotLight();
light.position.set(0, 70, 70);
light.angle = 0.4;
light.castShadow = true;
light.penumbra = 0.3;
light.target.add(target);
three.scene.add(light);
three.scene.add(light.target);
target.add(light);
three.scene.add(target);
const render = (time: DOMHighResTimeStamp) => {
three.controls.update();
target.rotation.y = time;
stats.update();
Last active: 2 years ago
gistpad-scratch
const target = new THREE.Object3D();
target.position.set(0, 10, 0);
const light = new THREE.SpotLight();
light.position.set(0, 70, 70);
light.angle = 0.4;
light.castShadow = true;
light.penumbra = 0.3;
light.target.add(target);
three.scene.add(light);
three.scene.add(light.target);
target.add(light);
three.scene.add(target);
const render = (time: DOMHighResTimeStamp) => {
three.controls.update();
target.rotation.y = time;
stats.update();
Last active: 2 years ago
OrbitControls rotate inversion.
// Set controls rotate inversion must be in constructor.
if (rotateInversion) this.controls.rotateSpeed *= -1;
Last active: 2 years ago
Sericalize tree data struct to a array.
/**
* 将场景列表打平为单维数组
* @param scene 主场景
*/
export const serializeScene = <T extends SceneInfo | State<SceneInfo>>(
scene: T
) => {
let sceneList: T[] = [];
scene.children.forEach((s) => {
sceneList.push(s as T);
if (s.children.length)
sceneList = [...sceneList, ...serializeScene(s)] as T[];
});
return sceneList;
};
Last active: 2 years ago
Open file upload when click any element
const handleAvatar = useCallback(() => {
if (avatarLoading) return;
const input = document.createElement('input');
input.type = 'file';
input.accept = 'image/*';
input.click();
const form = new FormData();
input.addEventListener('change', async (e) => {
if (!input.files?.length) return;
form.append('headimg', input.files[0]);
setLoad();
try {
const result = await editUserInfo(form);
message.success(result);
} catch (e) {
console.log(e);
message.warn('请求失败');
Last active: 2 years ago
CSS one line ellipsis
.username {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Last active: 2 years ago
Sum with an arbitray amount of brackets
function sum(a) {
let currentSum = a;
function f(b) {
currentSum += b;
return f;
}
f.toString = function () {
return currentSum;
};
f[Symbol.toPrimitive] = function () {
return currentSum;
};
return f;
}
console.log(sum(1)(2).toString());
Last active: 2 years ago
Sum with an arbitray amount of brackets
function sum(a) {
sum.current = (sum.current ?? 0) + a;
sum.toString = () => sum.current;
return sum;
}
console.log(sum(1)(2).toString());
Last active: 2 years ago
Sum with an arbitray amount of brackets
function sum(a) {
function f(b) {
return sum(a + b);
}
f.toString = () => a
return f;
}
console.log(sum(1)(2).toString());