Skip to content

Instantly share code, notes, and snippets.

View Ray-56's full-sized avatar
🌴
On vacation

56 Ray-56

🌴
On vacation
View GitHub Profile
@Ray-56
Ray-56 / useUaParser.test.tsx
Last active March 12, 2024 05:24
Vitest 修改 window 相关属性
import { renderHook } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi, expectTypeOf } from 'vitest';
import { useUaParser } from '../src';
describe('useUaParser', () => {
const userAgent =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.123 Safari/537.36';
beforeEach(() => {
const originalNavigator = { ...window.navigator };
vi.spyOn(window, 'navigator', 'get').mockImplementation(() => ({
@Ray-56
Ray-56 / stuns
Created January 12, 2024 06:29 — forked from yetithefoot/stuns
STUN+TURN servers list
{url:'stun:stun01.sipphone.com'},
{url:'stun:stun.ekiga.net'},
{url:'stun:stun.fwdnet.net'},
{url:'stun:stun.ideasip.com'},
{url:'stun:stun.iptel.org'},
{url:'stun:stun.rixtelecom.se'},
{url:'stun:stun.schlund.de'},
{url:'stun:stun.l.google.com:19302'},
{url:'stun:stun1.l.google.com:19302'},
{url:'stun:stun2.l.google.com:19302'},
@Ray-56
Ray-56 / snippets.typescriptreact.json
Last active August 26, 2022 07:54
Some snippets of Vscode
"Function Component": {
"prefix": "fc",
"body": [
"interface $1Props {",
"\t$2",
"}",
" ",
"const $1: React.FC<$1Props> = (props) => {",
"\tconst {} = props;",
"\treturn ($0);",
@Ray-56
Ray-56 / tool-types.ts
Last active January 19, 2022 01:26
Typescript 类型工具集
// 反转对象 key value
type ReverseObject<T extends Record<keyof T, keyof any>> = {
[P in T[keyof T]]: {
[K in keyof T]: T[K] extends P ? K : never;
}[keyof T];
};
@Ray-56
Ray-56 / async-await-forEach-alternatives.md
Created December 22, 2021 12:49 — forked from joeytwiddle/async-await-forEach-alternatives.md
Do not use forEach with async-await

Do not use forEach with async-await

TLDR: Use for...of instead of forEach in asynchronous code.

The problem

Array.prototype.forEach is not designed for asynchronous code. (It was not suitable for promises, and it is not suitable for async-await.)

For example, the following forEach loop might not do what it appears to do:

@Ray-56
Ray-56 / Input.tsx
Created December 1, 2021 10:21
React Component 受空|非受控;value, defaultValue
import { useEffect, useRef, useState } from 'react';
// 解决“受控组件*******”报错
function fixControlledValue(value: string) {
if (typeof value === 'undefined' || value === null) return '';
return value;
}
interface InputProps {
/** 不受控 */
@Ray-56
Ray-56 / commonly.md
Last active May 21, 2022 11:59
常用词组

常用词

  • buggy: adj. 多虫的(有bug的)。eg: This is buggy code. This function is buggy.
  • code motion。eg: 可以使用在代码优化中的 commit message 中
  • pair: n. 一对,一双。eg:常用在成对出现的变量命名中

词组

  • allowList、denyList
  • source(源)、destination(目的)
@Ray-56
Ray-56 / FormItemRangePicker.tsx
Last active June 22, 2022 10:21
AntdV4 Form RangePicker 拆分
import React from 'react';
import { Button, DatePicker, Form, Input } from 'antd';
import moment from 'moment';
const DemoPage: React.FC = () => {
const [form] = Form.useForm();
const handleOk = (vals: any) => {
console.log(vals);
};
return (