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
interface User { | |
name: string; | |
age: number; | |
address: string; | |
} | |
type PartialByKeys<T, K = keyof T> = Merge< | |
{ | |
[P in keyof T as P extends K ? P : never]?: T[P]; | |
} & { |
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
interface User { | |
name?: string | |
age?: number | |
address?: string | |
} | |
type RequiredByKeys<T, K = keyof T> = Merge< | |
{ | |
[P in keyof T as P extends K ? P : never]-?: T[P] | |
} & { |
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 UserBuilder { | |
public username!: string; | |
public sex!: string; | |
public age!: number; | |
public photo!: string; | |
public email!: string; | |
setUserName(name: string) { | |
this.username = name; | |
return this; |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Synchronized login status with BroadcastChannel API</title> | |
</head> | |
<body> | |
<h3>Current status: <span id="status">signed in</span></h3> |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Background Color Sync with BroadcastChannel API</title> | |
</head> | |
<body> | |
<input type="color" id="bgColorPicker" /> |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Axios request retry example (adapter)</title> | |
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script> | |
</head> | |
<body> |
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
function retryAdapterEnhancer(adapter, options) { | |
const { times = 0, delay = 300 } = options; | |
return async (config) => { | |
const { retryTimes = times, retryDelay = delay } = config; | |
let __retryCount = 0; | |
const request = async () => { | |
try { | |
return await adapter(config); | |
} catch (err) { |
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
axios.interceptors.response.use(null, (err) => { | |
let config = err.config; | |
if (!config || !config.retryTimes) return Promise.reject(err); | |
const { __retryCount = 0, retryDelay = 300, retryTimes } = config; | |
config.__retryCount = __retryCount; | |
// Check whether the number of retries has been exceeded | |
if (__retryCount >= retryTimes) { | |
return Promise.reject(err); | |
} | |
// Increase the number of retries |
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
type EventHandler = (...args: any[]) => any; | |
class EventEmitter { | |
private c = new Map<string, EventHandler[]>(); | |
subscribe(topic: string, ...handlers: EventHandler[]) { | |
let topics = this.c.get(topic); | |
if (!topics) { | |
this.c.set(topic, (topics = [])); | |
} |
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 MAX_WIDTH = 800; | |
function compress(base64, quality, mimeType) { | |
let canvas = document.createElement("canvas"); | |
let img = document.createElement("img"); | |
img.crossOrigin = "anonymous"; | |
return new Promise((resolve, reject) => { | |
img.src = base64; | |
img.onload = () => { | |
let targetWidth, targetHeight; |