Skip to content

Instantly share code, notes, and snippets.

@kurt-liao
kurt-liao / .prettierrc.json
Created October 13, 2022 15:00
My prettier settings
{
"useTabs": false,
"tabWidth": 2,
"semi": false,
"trailingComma": "none",
"endOfLine": "lf",
"printWidth": 100,
"singleQuote": false
}
@kurt-liao
kurt-liao / axios.ts
Created October 12, 2022 06:03
Axios template in typescript
import axios, { Method, AxiosInstance, AxiosRequestConfig } from 'axios'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import qs from 'qs'
export const FORM_METHOD: Method[] = ['POST', 'post', 'PUT', 'put']
export const QUERY_METHOD: Method[] = ['GET', 'get', 'DELETE', 'delete']
let http: AxiosInstance = axios.create({
baseURL: '/'
@kurt-liao
kurt-liao / helper.ts
Created October 12, 2022 07:04
常用的 helper methods
/**
* 判斷變數值是否是 Primitive
*/
const isPrimitive = (val: any) => {
if (val == null) return true
if (typeof val === 'object' || typeof val === 'function') return false
return true
}
const str = 'bbabcddd'
const regex = /(abc)/gd;
const matched = regex.exec(str);
console.log(matched);
// [
// "abc",
// "abc",
// groups: undefined,
// usage.mjs
import { output } from "./awaiting.mjs";
export function outputPlusValue(value) { return output + value }
console.log(outputPlusValue(100));
setTimeout(() => console.log(outputPlusValue(100), 1000));
class Person {
name = 'Kurt'
#age = 25
test() {
console.log('Hi, ')
this.#ttt()
}
#ttt() {
console.log(this.name)
}
// awaiting.mjs
import { process } from "./some-module.mjs";
const dynamic = import(computedModuleSpecifier);
const data = fetch(url);
export const output = process((await dynamic).default, await data);
// usage.mjs
import promise, { output } from "./awaiting.mjs";
export function outputPlusValue(value) { return output + value }
promise.then(() => {
console.log(outputPlusValue(100));
setTimeout(() => console.log(outputPlusValue(100), 1000);
});
// awaiting.mjs
import { process } from "./some-module.mjs";
let output;
export default (async () => {
const dynamic = await import(computedModuleSpecifier);
const data = await fetch(url);
output = process(dynamic.default, data);
})();
export { output };
// awaiting.mjs
import { process } from "./some-module.mjs";
let output;
async function main() {
const dynamic = await import(computedModuleSpecifier);
const data = await fetch(url);
output = process(dynamic.default, data);
}
main();
export { output };