Skip to content

Instantly share code, notes, and snippets.

View coodoo's full-sized avatar

Jeremy Lu coodoo

  • Right here, right now.
View GitHub Profile
// sample code copied from the blog:
// https://blog.axlight.com/posts/you-might-not-need-react-query-for-jotai/
const idAtom = atom(1)
const dataAtom = atom(
// read
async (get) => {
const id = get(idAtom)
const res = await fetch(`https://reqres.in/api/posts/${id}`)
const data = await res.json()
return data
@coodoo
coodoo / a.js
Last active February 21, 2022 06:54
async function foo() {
// throw new Error('bb')
return new Promise((res, rej) => {
setTimeout(_ => {
// throw new Error('bb')
rej('cc')
}, 1000)
})
}
@coodoo
coodoo / a1.js
Last active January 14, 2022 07:11
async sample
// 前情提要:async/await 只是 Promise 的語法糖
// 只要精熟 Promise 則使用 async/await 就心無罣礙
// 範例 1 - 錯誤示範
// main 為同步執行,不會等待 forEach 內三件 async 工作完成即先結束
async function async_log(time) {
// 模擬這支 fn 經過 300ms 後才回應,因此是非同步函式
// 想像成是 fetch() 就行
return new Promise((resolve, reject) => {
setTimeout(_ => {
@coodoo
coodoo / permutation.js
Created August 17, 2021 22:32
permutation in js with reducer
let arr = [
['a1', 'a2'],
['b1', 'b2', 'b3']
]
let result = arr.reduce(
// -----------
// 最外層 reduce 會依序遍歷
// ['a1', 'a2'] 與 ['b1', 'b2', 'b3']
const fetch = require('node-fetch')
const fs = require('fs-extra')
const { execSync } = require('child_process')
const main = async () => {
const query = `nasa,cycling,surfing,underwater,swimming`
const size = `1`
const order = `latest`
const orientation=`landscape`
const photoName = 'photo.png'
@coodoo
coodoo / example.js
Created March 8, 2021 07:11
一個十分鐘寫的土砲 hotkey manager
useHotkey(['Meta', 'c'], () => {
console.log( `要跑 1`, )
})
useHotkey(['ctrl', 'c'], () => {
console.log( `要跑 2`, )
})
@coodoo
coodoo / sizer.js
Last active December 5, 2020 04:49
import {
PixelRatio,
Dimensions,
useWindowDimensions
} from 'react-native'
const to = n => Number(n.toFixed(2))
const { width, height } = Dimensions.get('window')
// 將短的那邊視為 width
@coodoo
coodoo / a.js
Last active November 27, 2020 06:35
Rotation matrix - https://jsfiddle.net/pq62hx0t/4/
function matrixArrayToCssMatrix(array) {
return "matrix3d(" + array.join(',') + ")";
}
function multiplyMatrixAndPoint(matrix, point) {
//Give a simple variable name to each part of the matrix, a column and row number
var c0r0 = matrix[0],
c1r0 = matrix[1],
c2r0 = matrix[2],
@coodoo
coodoo / _final.md
Last active November 7, 2020 13:42

前言

這是 Oct. 28 ~ Nov. 01, 2020 間於推特上舉辦的趣味面試挑戰,四天期間共 517 人次閱讀、42 人 fork sandbox,最後收到九份回答,如果這是正式面試,將錄取 5 人、備取 1 人、拒絕 3 人,也就是成功率約六成。

先說結論

  • 面試時別急著跳進去回答問題,先拉高層次觀察問題的形狀,搞清楚 root cause 後再思考答案與解法

  • 過程中多詢問主考官真正想達到的目地、目前為何採取此手法、現在的做法是否遇到困難、有無解法上的限制(time, mem, cpu bound)

@coodoo
coodoo / karabiner.json
Last active August 9, 2020 06:10
Toggle vi_mode mode with caps_lock so that hjkl keys function as arrow keys and u/i as delete and backspace.
{
"global":
{
"check_for_updates_on_startup": true,
"show_in_menu_bar": false,
"show_profile_name_in_menu_bar": false
},
"profiles":
[
{